Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions exercise/01/color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "stdafx.h"
#include "color.h"
#include <algorithm>
#include <windows.h> // RGB


color::color(double red, double green, double blue) :
red(get_valid_number(red)),
green(get_valid_number(green)),
blue(get_valid_number(blue)) { }

double color::get_valid_number(double value) const
{
return std::clamp(value, 0., 1.);
}

double color::get_red() const
{
return red;
}

double color::get_green() const
{
return green;
}

double color::get_blue() const
{
return blue;
}

void color::set_red(double red)
{
this->red = get_valid_number(red);
}

void color::set_green(double green)
{
this->green = get_valid_number(green);
}

void color::set_blue(double blue)
{
this->blue = get_valid_number(blue);
}

unsigned long color::get_color_ref() const
{
return RGB(red * 255, green * 255, blue * 255);
}

double color::get_luminance() const
{
return (0.2126 * red + 0.7152 * green + 0.0722 * blue);
}

bool operator==(const color& c1, const color& c2)
{
return c1.get_color_ref() == c2.get_color_ref();
}
29 changes: 29 additions & 0 deletions exercise/01/color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


class color
{
double red;
double green;
double blue;

double get_valid_number(double value) const;

public:
color(double red = 0, double green = 0, double blue = 0);
color(const color& other) = default;
~color() = default;

double get_red() const;
double get_green() const;
double get_blue() const;

void set_red(double red);
void set_green(double green);
void set_blue(double blue);

unsigned long get_color_ref() const; // COLORREF
double get_luminance() const;
};


bool operator==(const color& c1, const color& c2);
49 changes: 49 additions & 0 deletions exercise/02/array.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
#include "array.h"
#include <stdexcept> // std::out_of_range

array::array(int size, double value)
{
if (size < 0) return;
n = size;
p = new double[n];
for (int i = 0; i < n; ++i)
p[i] = value;
}

array::array(const array& other) : n(other.n), p(new double[other.n])
{
std::copy(other.p, other.p + other.n, p);
}

array::~array()
{
if (!p)
return;
delete[] p;
}

array& array::operator=(const array & other)
{
n = other.n;
if (p)
delete[] p;
p = new double[n];
std::copy(other.p, other.p + n, p);
return *this;
}

array::array(array&& other) noexcept :
n(std::exchange(other.n, 0)),
p(std::exchange(other.p, nullptr)) { }

int array::size()
{
return n;
}

double array::at(int index) const
{
if (index < 0 || index > n)
throw std::out_of_range("Index out of range.");

return p[index];
}
10 changes: 10 additions & 0 deletions exercise/02/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ class array
double* p;
int n;
public:
array(int size = 0, double value = 0);
array(const array& other);
~array();

array& operator=(const array& other);

array(array&& other) noexcept;

int size();
double at(int index) const;
};
28 changes: 14 additions & 14 deletions exercise/02/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,73 @@ TEST_CLASS(test_array)

TEST_METHOD(array_default_constructor)
{
/*

array a;
Assert::AreEqual(0, a.size());
*/

}

TEST_METHOD(array_constructor)
{
/*

array a(size, value);
Assert::AreEqual(size, a.size());
for(int i=0; i<a.size(); ++i)
Assert::AreEqual(value, a.at(i));
*/

}

TEST_METHOD(array_index_invalid_large)
{
/*

array a(size, value);
auto func = [a]() { a.at(100); };
Assert::ExpectException<std::out_of_range>(func);
*/

}

TEST_METHOD(array_index_invalid_negative)
{
/*

array a(size, value);
auto func = [a]() { a.at(-2); };
Assert::ExpectException<std::out_of_range>(func);
*/

}

TEST_METHOD(array_assignment)
{
/*

array a(size, value);
array b;
b = a;
Assert::AreEqual(size, b.size());
for (int i = 0; i < b.size(); ++i)
Assert::AreEqual(value, b.at(i));
*/

}

TEST_METHOD(array_copy_constructor)
{
/*

array a(size, value);
array b(a);
Assert::AreEqual(size, b.size());
for (int i = 0; i < b.size(); ++i)
Assert::AreEqual(value, b.at(i));
*/

}

TEST_METHOD(array_move_constructor)
{
/*

array a(size, value);
array b = std::move(a);
Assert::AreEqual(0, a.size());
Assert::AreEqual(size, b.size());
for (int i = 0; i < b.size(); ++i)
Assert::AreEqual(value, b.at(i));
*/

}

};
15 changes: 15 additions & 0 deletions exercise/03/point.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
#include "point.h"

point::point(int x, int y) : x{x}, y{y} { }

bool point::operator==(const point& other)
{
return (this->x == other.x) && (this->y == other.y);
}

bool point::operator!=(const point& other)
{
return (this->x != other.x) || (this->y != other.y);
}
bool point::operator<(const point& other)
{
return (this->x < other.x) || (this->x == other.x && this->y < other.y);
}
14 changes: 14 additions & 0 deletions exercise/03/point.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


class point
{
int x, y;

public:
point(int x = 0, int y = 0);
~point() = default;

bool operator==(const point& other);
bool operator!=(const point& other);
bool operator<(const point& other);
};
16 changes: 8 additions & 8 deletions exercise/03/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ TEST_CLASS(test_1_operators)

TEST_METHOD(point_default_constructor)
{
/*

Assert::IsTrue(point(0, 0) == point());
*/

}
TEST_METHOD(points_equal)
{
/*

Assert::IsTrue(point(1, 2) == point(1, 2));
Assert::IsTrue(point(-12, -34) == point(-12, -34));
*/

}
TEST_METHOD(points_not_equal)
{
/*

Assert::IsTrue(point(1, 2) != point(2, 1));
Assert::IsTrue(point(-1, -2) != point(1, 2));
*/

}
TEST_METHOD(points_sorted_by_first_coordinate_then_second)
{
/*

Assert::IsTrue(point(1, 2) < point(1, 3));
Assert::IsTrue(point(1, 2) < point(2, 2));
Assert::IsFalse(point(1, 2) < point(1, 2));
*/

}
};