From d931ac502ab88b55cc27181ed2023d4f19211e00 Mon Sep 17 00:00:00 2001 From: Marina Krcek Date: Tue, 13 Nov 2018 16:40:44 +0100 Subject: [PATCH 1/5] Day 2, Exercise 01, Color class - Implement color class - comment unit tests that require overloading operators --- exercise/01/color.cpp | 62 ++++++++++++++++++++++++++++++++++++++ exercise/01/color.h | 36 ++++++++++++++++++++++ exercise/01/color_test.cpp | 38 +++++++++++------------ 3 files changed, 117 insertions(+), 19 deletions(-) create mode 100644 exercise/01/color.cpp create mode 100644 exercise/01/color.h diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp new file mode 100644 index 0000000..1444b22 --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,62 @@ +#include "stdafx.h" +#include "color.h" +#include // RGB + + +color::color() : color(0, 0, 0) { } + +color::color(double red, double green, double blue) : + red(get_valid_number(red)), + green(get_valid_number(green)), + blue(get_valid_number(blue)) { } + +color::color(const color& other) : + color(other.red, other.green, other.blue) { } + +double color::get_valid_number(double value) const +{ + if (value < low_value) return low_value; + if (value > high_scaled_value) return high_scaled_value; + return value; +} + +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 * high_rgb_value, green * high_rgb_value, blue * high_rgb_value); +} + +double color::get_luminance() const +{ + return (rY * red + gY * green + bY * blue); +} \ No newline at end of file diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..c050666 --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,36 @@ + + +class color +{ + double red; + double green; + double blue; + + static const unsigned int high_rgb_value = 255; + static const unsigned int low_value = 0; + static const unsigned int high_scaled_value = 1; + + // sRGB luminance(Y) values + const double rY = 0.2126; // 0.212655; + const double gY = 0.7152; // 0.715158; + const double bY = 0.0722; // 0.072187; + + double get_valid_number(double value) const; + +public: + color(); + color(double red, double green, double blue); + color(const color& other); + + 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; +}; \ No newline at end of file diff --git a/exercise/01/color_test.cpp b/exercise/01/color_test.cpp index 1af693e..feec926 100644 --- a/exercise/01/color_test.cpp +++ b/exercise/01/color_test.cpp @@ -18,11 +18,11 @@ namespace color_test { public: - TEST_METHOD(test_default_value) - { - color c; - Assert::AreEqual(color(0, 0, 0), c); - } + //TEST_METHOD(test_default_value) + //{ + // color c; + // Assert::AreEqual(color(0, 0, 0), c); + //} TEST_METHOD(test_construction) { @@ -51,21 +51,21 @@ namespace color_test Assert::AreEqual(1., c.get_blue()); } - TEST_METHOD(test_color_copy) - { - color a(12, 13, 14); - color b(a); - Assert::AreEqual(a, b); - } + //TEST_METHOD(test_color_copy) + //{ + // color a(12, 13, 14); + // color b(a); + // Assert::AreEqual(a, b); + //} - TEST_METHOD(test_color_assign) - { - color a(12, 13, 14); - color b, c; - c = b = a; - Assert::AreEqual(a, b); - Assert::AreEqual(a, c); - } + //TEST_METHOD(test_color_assign) + //{ + // color a(12, 13, 14); + // color b, c; + // c = b = a; + // Assert::AreEqual(a, b); + // Assert::AreEqual(a, c); + //} TEST_METHOD(test_conversion_colorref) { From af8e81d3aad415a898ce4aeef1208270eaffba4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marina=20Kr=C4=8Dek?= Date: Wed, 14 Nov 2018 00:14:07 +0100 Subject: [PATCH 2/5] Day 2, Exercise 02, Array class - Implement array class - copy and assignment unit tests pass with default - std::move fails --- exercise/02/array.cpp | 25 +++++++++++++++++++++++++ exercise/02/array.h | 5 +++++ exercise/02/test.cpp | 40 ++++++++++++++++++++-------------------- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..4181815 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,26 @@ #include "array.h" +#include // std::out_of_range + +array::array() : array(0, 0) { } + +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; +} + +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]; +} \ No newline at end of file diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..c5ff7a3 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -4,4 +4,9 @@ class array double* p; int n; public: + array(); + array(int size, double value); + + int size(); + double at(int index) const; }; diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..5856eff 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -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(func); - */ + } TEST_METHOD(array_index_invalid_negative) { - /* + array a(size, value); auto func = [a]() { a.at(-2); }; Assert::ExpectException(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)); - */ + + //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)); + // } }; From eea6213a5f94b84a04de0633b611b3295eba3cef Mon Sep 17 00:00:00 2001 From: Marina Krcek Date: Wed, 14 Nov 2018 11:03:23 +0100 Subject: [PATCH 3/5] Day 2, Exercise 03, Point class, operator overloading - overloading operators ==, !=, < --- exercise/03/point.cpp | 15 +++++++++++++++ exercise/03/point.h | 14 ++++++++++++++ exercise/03/test.cpp | 16 ++++++++-------- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 5c7c45f..4cc89e1 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -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); +} \ No newline at end of file diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..e9e94fb 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -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); +}; \ No newline at end of file diff --git a/exercise/03/test.cpp b/exercise/03/test.cpp index d8e6669..b40cf5d 100644 --- a/exercise/03/test.cpp +++ b/exercise/03/test.cpp @@ -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)); - */ + } }; From 219177a64be476b68ac7704e485565d4728a4531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marina=20Kr=C4=8Dek?= Date: Wed, 14 Nov 2018 18:23:32 +0100 Subject: [PATCH 4/5] Day 02, Exercise 01, Color class - add operator== - uncomment unit tests - use clamp - remove constants --- exercise/01/color.cpp | 20 +++++++++----------- exercise/01/color.h | 21 +++++++-------------- exercise/01/color_test.cpp | 38 +++++++++++++++++++------------------- 3 files changed, 35 insertions(+), 44 deletions(-) diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp index 1444b22..b35e1c5 100644 --- a/exercise/01/color.cpp +++ b/exercise/01/color.cpp @@ -1,23 +1,17 @@ #include "stdafx.h" #include "color.h" +#include #include // RGB -color::color() : color(0, 0, 0) { } - color::color(double red, double green, double blue) : red(get_valid_number(red)), green(get_valid_number(green)), blue(get_valid_number(blue)) { } -color::color(const color& other) : - color(other.red, other.green, other.blue) { } - double color::get_valid_number(double value) const { - if (value < low_value) return low_value; - if (value > high_scaled_value) return high_scaled_value; - return value; + return std::clamp(value, 0., 1.); } double color::get_red() const @@ -50,13 +44,17 @@ void color::set_blue(double blue) this->blue = get_valid_number(blue); } - unsigned long color::get_color_ref() const { - return RGB(red * high_rgb_value, green * high_rgb_value, blue * high_rgb_value); + return RGB(red * 255, green * 255, blue * 255); } double color::get_luminance() const { - return (rY * red + gY * green + bY * blue); + 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(); } \ No newline at end of file diff --git a/exercise/01/color.h b/exercise/01/color.h index c050666..3d45fbf 100644 --- a/exercise/01/color.h +++ b/exercise/01/color.h @@ -6,21 +6,12 @@ class color double green; double blue; - static const unsigned int high_rgb_value = 255; - static const unsigned int low_value = 0; - static const unsigned int high_scaled_value = 1; - - // sRGB luminance(Y) values - const double rY = 0.2126; // 0.212655; - const double gY = 0.7152; // 0.715158; - const double bY = 0.0722; // 0.072187; - double get_valid_number(double value) const; public: - color(); - color(double red, double green, double blue); - color(const color& other); + 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; @@ -30,7 +21,9 @@ class color void set_green(double green); void set_blue(double blue); - unsigned long get_color_ref() const; // COLORREF double get_luminance() const; -}; \ No newline at end of file +}; + + +bool operator==(const color& c1, const color& c2); \ No newline at end of file diff --git a/exercise/01/color_test.cpp b/exercise/01/color_test.cpp index feec926..1af693e 100644 --- a/exercise/01/color_test.cpp +++ b/exercise/01/color_test.cpp @@ -18,11 +18,11 @@ namespace color_test { public: - //TEST_METHOD(test_default_value) - //{ - // color c; - // Assert::AreEqual(color(0, 0, 0), c); - //} + TEST_METHOD(test_default_value) + { + color c; + Assert::AreEqual(color(0, 0, 0), c); + } TEST_METHOD(test_construction) { @@ -51,21 +51,21 @@ namespace color_test Assert::AreEqual(1., c.get_blue()); } - //TEST_METHOD(test_color_copy) - //{ - // color a(12, 13, 14); - // color b(a); - // Assert::AreEqual(a, b); - //} + TEST_METHOD(test_color_copy) + { + color a(12, 13, 14); + color b(a); + Assert::AreEqual(a, b); + } - //TEST_METHOD(test_color_assign) - //{ - // color a(12, 13, 14); - // color b, c; - // c = b = a; - // Assert::AreEqual(a, b); - // Assert::AreEqual(a, c); - //} + TEST_METHOD(test_color_assign) + { + color a(12, 13, 14); + color b, c; + c = b = a; + Assert::AreEqual(a, b); + Assert::AreEqual(a, c); + } TEST_METHOD(test_conversion_colorref) { From c3e9853afb92a551d4c20cbcbc138825e51a7fa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marina=20Kr=C4=8Dek?= Date: Wed, 14 Nov 2018 19:27:42 +0100 Subject: [PATCH 5/5] Day 02, Exercise 02, array - add assignment operator - add move constructor - add copy constructor --- exercise/02/array.cpp | 28 ++++++++++++++++++++++++++-- exercise/02/array.h | 9 +++++++-- exercise/02/test.cpp | 14 +++++++------- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 4181815..bb00c2d 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1,8 +1,6 @@ #include "array.h" #include // std::out_of_range -array::array() : array(0, 0) { } - array::array(int size, double value) { if (size < 0) return; @@ -12,6 +10,32 @@ array::array(int size, double value) 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; diff --git a/exercise/02/array.h b/exercise/02/array.h index c5ff7a3..41b760d 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -4,8 +4,13 @@ class array double* p; int n; public: - array(); - array(int size, double value); + 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; diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 5856eff..afe9366 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -72,13 +72,13 @@ TEST_CLASS(test_array) 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)); - // + 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)); + } };