From 4d816aa20096d26deaeb442d6b3a45852564923c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Kri=C5=BEan?= Date: Tue, 13 Nov 2018 16:24:21 +0100 Subject: [PATCH 1/5] First exercise done --- exercise/01/color.cpp | 83 ++++++ exercise/01/color.h | 35 +++ exercise/01/color_test.vcxproj | 353 +++++++++++++------------ exercise/01/color_test.vcxproj.filters | 72 ++--- 4 files changed, 333 insertions(+), 210 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..db9cb1b --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,83 @@ +#include "color.h" +#include + +const double color::min_color_allowed_value = 0.; +const double color::max_color_allowed_value = 1.; + +color::color(double red, double green, double blue) +{ + m_red = std::clamp(red, min_color_allowed_value, max_color_allowed_value); + m_green = std::clamp(green, min_color_allowed_value, max_color_allowed_value); + m_blue = std::clamp(blue, min_color_allowed_value, max_color_allowed_value); +} + +color::color(color const& other) +{ + // trivial, but still here + m_red = other.m_red; + m_green = other.m_green; + m_blue = other.m_blue; +} + + +color& color::operator=(color other) +{ + using std::swap; + swap(m_red, other.m_red); + swap(m_green, other.m_green); + swap(m_blue, other.m_blue); + return *this; +} + +bool color::operator==(color const& other) const +{ + return m_red == other.m_red && m_green == other.m_green && m_blue == other.m_blue; +} + +double color::get_red() const +{ + return m_red; +} + +double color::get_green() const +{ + return m_green; +} + +double color::get_blue() const +{ + return m_blue; +} + +void color::set_red(double red) +{ + m_red = std::clamp(red, min_color_allowed_value, max_color_allowed_value); +} + +void color::set_green(double green) +{ + m_green = std::clamp(green, min_color_allowed_value, max_color_allowed_value); +} + +void color::set_blue(double blue) +{ + m_blue = std::clamp(blue, min_color_allowed_value, max_color_allowed_value); +} + +COLORREF color::get_color_ref() const +{ + return RGB(static_cast(m_red * 255), static_cast(m_green * 255), static_cast(m_blue * 255)); +} + +double color::get_luminance() const +{ + // magic constants + return 0.2126 * m_red + 0.7152 * m_green + 0.0722 * m_blue; + +} + + + + + + diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..05ffcf9 --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,35 @@ +#pragma once +#include // RGB + +class color +{ +public: + explicit color(double red=0., double green=0., double blue=0.); + color(color const&); + color& operator=(color); + + ~color() = default; + + + bool operator==(color const&) const; + + + 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); + + COLORREF get_color_ref() const; + double get_luminance() const; + +private: + double m_red; + double m_green; + double m_blue; + + static const double min_color_allowed_value; + static const double max_color_allowed_value; +}; diff --git a/exercise/01/color_test.vcxproj b/exercise/01/color_test.vcxproj index 261fee9..81987c0 100644 --- a/exercise/01/color_test.vcxproj +++ b/exercise/01/color_test.vcxproj @@ -1,175 +1,180 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 15.0 - {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B} - Win32Proj - color_test - 10.0.17134.0 - NativeUnitTestProject - - - - DynamicLibrary - true - v141 - Unicode - false - - - DynamicLibrary - false - v141 - true - Unicode - false - - - DynamicLibrary - true - v141 - Unicode - false - - - DynamicLibrary - false - v141 - true - Unicode - false - - - - - - - - - - - - - - - - - - - - - true - - - true - - - true - - - true - - - - Use - Level3 - Disabled - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;_DEBUG;%(PreprocessorDefinitions) - true - stdcpp17 - - - Windows - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - Use - Level3 - Disabled - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - _DEBUG;%(PreprocessorDefinitions) - true - stdcpp17 - - - Windows - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - Level3 - Use - MaxSpeed - true - true - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;NDEBUG;%(PreprocessorDefinitions) - true - stdcpp17 - - - Windows - true - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - Level3 - Use - MaxSpeed - true - true - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - NDEBUG;%(PreprocessorDefinitions) - true - stdcpp17 - - - Windows - true - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - - - - - - Create - Create - Create - Create - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B} + Win32Proj + color_test + 10.0.17134.0 + NativeUnitTestProject + + + + DynamicLibrary + true + v141 + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + DynamicLibrary + true + v141 + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + + + + + + + + + + + + + + + + + + + true + + + true + + + true + + + true + + + + Use + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + + + Windows + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Use + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + _DEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + + + Windows + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + Use + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + + + Windows + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + Use + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + NDEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + + + Windows + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + + + + + NotUsing + NotUsing + NotUsing + NotUsing + + + Create + Create + Create + Create + + + + + + \ No newline at end of file diff --git a/exercise/01/color_test.vcxproj.filters b/exercise/01/color_test.vcxproj.filters index 61bbd8e..53d8354 100644 --- a/exercise/01/color_test.vcxproj.filters +++ b/exercise/01/color_test.vcxproj.filters @@ -1,37 +1,37 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {3f0b361d-e959-4712-bd7f-b211a50b8b89} - - - {a5701a66-1845-4643-b359-7bc01a47fe2d} - - - {ec9c9ba0-fde3-42a6-ad15-1d2221ab37fb} - - - - - Source Files\test - - - Source Files\precompiled - - - Source Files\source - - - - - Source Files\precompiled - - - Source Files\source - - + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {3f0b361d-e959-4712-bd7f-b211a50b8b89} + + + {a5701a66-1845-4643-b359-7bc01a47fe2d} + + + {ec9c9ba0-fde3-42a6-ad15-1d2221ab37fb} + + + + + Source Files\test + + + Source Files\precompiled + + + Source Files\source + + + + + Source Files\precompiled + + + Source Files\source + + \ No newline at end of file From a1a0083a516b77e20a536c856f3d08de910061a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Kri=C5=BEan?= Date: Tue, 13 Nov 2018 17:51:37 +0100 Subject: [PATCH 2/5] Finished second exercise --- exercise/02/array.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++ exercise/02/array.h | 13 +++++++++ exercise/02/test.cpp | 14 ---------- 3 files changed, 77 insertions(+), 14 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..4fed707 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,65 @@ #include "array.h" +#include +#include + +array::array(int size, double value) : p(nullptr), n(0) +{ + if (size < 0) + throw std::invalid_argument("size must be an integer greater or equal to zero"); + n = size; + if (n > 0) + { + p = new double[size]; + std::generate(p, p + n, [value]() { return value; }); + } +} + +array::array(array const& other) : p(nullptr), n(0) +{ + n = other.size(); + if (n > 0) + { + p = new double[n]; + std::copy(other.p, other.p + other.n, p); + } +} + +array& array::operator=(array const& other) +{ + array tmp(other); + std::swap(n, tmp.n); + std::swap(p, tmp.p); + return *this; +} + +array::array(array&& other) noexcept : p(nullptr), n(0) +{ + std::swap(p, other.p); + std::swap(n, other.n); +} + +array::~array() +{ + if (p != nullptr) + { + delete[] p; + p = nullptr; + n = 0; + } +} + + +int array::size() const noexcept +{ + return n; +} + +double array::at(const int idx) const +{ + if (idx < 0 || idx >= size()) + { + throw std::out_of_range("array index out of range"); + } + return p[idx]; +} + diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..d90aa79 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -1,7 +1,20 @@ +#pragma once + // do not use standard container as member or base class class array { double* p; int n; + public: + + explicit array(int size = 0, double value = 0); + array(array const& other); + array& operator=(array const& other); + array(array && other) noexcept; + + ~array(); + + int size() const noexcept; + double at(int idx) const; }; diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..55ed1b2 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -12,73 +12,59 @@ 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)); - */ } }; From 688c46e88a10c1415935972f17c2dc4a690d8e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Kri=C5=BEan?= Date: Wed, 14 Nov 2018 09:37:20 +0100 Subject: [PATCH 3/5] Removed copy constructor (exercise 1) - using default copy constructor for color class and simplified assignment --- exercise/01/color.cpp | 12 +----------- exercise/01/color.h | 4 ++-- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp index db9cb1b..9e92c5b 100644 --- a/exercise/01/color.cpp +++ b/exercise/01/color.cpp @@ -11,21 +11,11 @@ color::color(double red, double green, double blue) m_blue = std::clamp(blue, min_color_allowed_value, max_color_allowed_value); } -color::color(color const& other) +color& color::operator=(color const& other) { - // trivial, but still here m_red = other.m_red; m_green = other.m_green; m_blue = other.m_blue; -} - - -color& color::operator=(color other) -{ - using std::swap; - swap(m_red, other.m_red); - swap(m_green, other.m_green); - swap(m_blue, other.m_blue); return *this; } diff --git a/exercise/01/color.h b/exercise/01/color.h index 05ffcf9..5ab5aca 100644 --- a/exercise/01/color.h +++ b/exercise/01/color.h @@ -5,8 +5,8 @@ class color { public: explicit color(double red=0., double green=0., double blue=0.); - color(color const&); - color& operator=(color); + color(color const&) = default; + color& operator=(color const&); ~color() = default; From c5192e1efa0b1ebec8ab4b2d20055511ac96227b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Kri=C5=BEan?= Date: Wed, 14 Nov 2018 10:50:38 +0100 Subject: [PATCH 4/5] Finished 3rd exercise --- exercise/03/point.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++ exercise/03/point.h | 16 +++++++++++++++ exercise/03/test.cpp | 10 +--------- 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 5c7c45f..ec715d2 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,47 @@ #include "point.h" +point::point(int x, int y) : x(x), y(y) {} + +bool operator==(point const& lhs, point const& rhs) +{ + return lhs.x == rhs.x && lhs.y == rhs.y; +} + +bool operator!=(point const& lhs, point const& rhs) +{ + return !(lhs == rhs); +} + +bool operator<(point const& lhs, point const& rhs) +{ + return lhs.x < rhs.x || lhs.y < rhs.y; +} + +bool operator<=(point const& lhs, point const& rhs) +{ + return (lhs < rhs) || (lhs == rhs); +} + +bool operator>(point const& lhs, point const& rhs) +{ + return !(lhs <= rhs); +} + +bool operator>=(point const& lhs, point const& rhs) +{ + return !(lhs < rhs); +} + + + + + + + + + + + + + + diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..594bc01 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,16 @@ +#pragma once + +struct point +{ + const int x; + const int y; + + point(int x = 0, int y = 0); +}; + +bool operator==(point const& lhs, point const& rhs); +bool operator!=(point const& lhs, point const& rhs); +bool operator<(point const& lhs, point const& rhs); +bool operator<=(point const& lhs, point const& rhs); +bool operator>(point const& lhs, point const& rhs); +bool operator>=(point const& lhs, point const& rhs); \ No newline at end of file diff --git a/exercise/03/test.cpp b/exercise/03/test.cpp index d8e6669..bb2ea35 100644 --- a/exercise/03/test.cpp +++ b/exercise/03/test.cpp @@ -9,30 +9,22 @@ 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)); - */ + Assert::IsFalse(point(1, 2) < point(1, 2)); } }; From c6cad547ba8107452d183e86c56750de322cff56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Kri=C5=BEan?= Date: Wed, 14 Nov 2018 10:59:35 +0100 Subject: [PATCH 5/5] Fixed incorrect implementation of operator< --- exercise/03/point.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index ec715d2..d586e94 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -14,7 +14,7 @@ bool operator!=(point const& lhs, point const& rhs) bool operator<(point const& lhs, point const& rhs) { - return lhs.x < rhs.x || lhs.y < rhs.y; + return (lhs.x == rhs.x) ? (lhs.y < rhs.y) : (lhs.x < rhs.x); } bool operator<=(point const& lhs, point const& rhs)