From c341cedfaccc60a74534135e086f161ab98a25c8 Mon Sep 17 00:00:00 2001 From: tmalkoc Date: Tue, 13 Nov 2018 16:29:15 +0100 Subject: [PATCH 1/7] Tvrtko Malkoc - day2 - exercise 01 --- exercise/01/color.cpp | 67 +++++ exercise/01/color.h | 27 ++ exercise/01/color_test.sln | 31 +++ exercise/01/color_test.vcxproj | 348 ++++++++++++------------- exercise/01/color_test.vcxproj.filters | 72 ++--- exercise/01/stdafx.h | 1 + 6 files changed, 336 insertions(+), 210 deletions(-) create mode 100644 exercise/01/color.cpp create mode 100644 exercise/01/color.h create mode 100644 exercise/01/color_test.sln diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp new file mode 100644 index 0000000..212b4b7 --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,67 @@ +#include "stdafx.h" +#include "color.h" + +namespace { + double to_double_range(double d) + { + return d < 0.0 ? 0.0 : 1.0 < d ? 1.0 : d; + } + + int to_char_range(double d) + { + return static_cast(255.0 * to_double_range(d)); + } +} + +color::color(double r, double g, double b) + : red_(to_double_range(r)), green_(to_double_range(g)), blue_(to_double_range(b)) +{ } + +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) +{ + red_ = to_double_range(red); +} + +void color::set_green(double green) +{ + green_ = to_double_range(green); +} + +void color::set_blue(double blue) +{ + blue_ = to_double_range(blue); +} + +COLORREF color::get_color_ref() const +{ + return RGB(to_char_range(red_), to_char_range(green_), to_char_range(blue_)); +} + +double color::get_luminance() const +{ + return 0.2126*red_ + 0.7152*green_ + 0.0722*blue_; +} + +bool operator==(const color& c1, const color& c2) +{ + const auto precision = 1. / 256.; + return + (c1.get_red() - c2.get_red() < precision) && + (c1.get_green() - c2.get_green() < precision) && + (c1.get_blue() - c2.get_blue() < precision); +} diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..ebb71cd --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,27 @@ +#pragma once + +class color +{ +public: + color(double r = 0.0, double g = 0.0, double b = 0.0); + + 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 red_; + double green_; + double blue_; + +}; + +bool operator ==(const color& c1, const color& c2); + diff --git a/exercise/01/color_test.sln b/exercise/01/color_test.sln new file mode 100644 index 0000000..b37280d --- /dev/null +++ b/exercise/01/color_test.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2036 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "color_test", "color_test.vcxproj", "{F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x64.ActiveCfg = Debug|x64 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x64.Build.0 = Debug|x64 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x86.ActiveCfg = Debug|Win32 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x86.Build.0 = Debug|Win32 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x64.ActiveCfg = Release|x64 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x64.Build.0 = Release|x64 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x86.ActiveCfg = Release|Win32 + {F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {CCD529C8-1AD1-4C1F-A86F-966D32A353D9} + EndGlobalSection +EndGlobal diff --git a/exercise/01/color_test.vcxproj b/exercise/01/color_test.vcxproj index 261fee9..c0588af 100644 --- a/exercise/01/color_test.vcxproj +++ b/exercise/01/color_test.vcxproj @@ -1,175 +1,175 @@ - - - - - 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) + + + + + + + + + + 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 diff --git a/exercise/01/stdafx.h b/exercise/01/stdafx.h index 6027cdf..6d45d18 100644 --- a/exercise/01/stdafx.h +++ b/exercise/01/stdafx.h @@ -1,4 +1,5 @@ #pragma once #include +#include // RGB #include "CppUnitTest.h" From 3d28c2e865ada80c4dbcbcea1b225b9d0e47f555 Mon Sep 17 00:00:00 2001 From: tmalkoc Date: Tue, 13 Nov 2018 16:37:24 +0100 Subject: [PATCH 2/7] renamed to_double_range -> to_allowed_color_range --- exercise/01/color.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp index 212b4b7..c9e94aa 100644 --- a/exercise/01/color.cpp +++ b/exercise/01/color.cpp @@ -2,19 +2,19 @@ #include "color.h" namespace { - double to_double_range(double d) + double to_allowed_color_range(double d) { return d < 0.0 ? 0.0 : 1.0 < d ? 1.0 : d; } int to_char_range(double d) { - return static_cast(255.0 * to_double_range(d)); + return static_cast(255.0 * to_allowed_color_range(d)); } } color::color(double r, double g, double b) - : red_(to_double_range(r)), green_(to_double_range(g)), blue_(to_double_range(b)) + : red_(to_allowed_color_range(r)), green_(to_allowed_color_range(g)), blue_(to_allowed_color_range(b)) { } double color::get_red() const @@ -34,17 +34,17 @@ double color::get_blue() const void color::set_red(double red) { - red_ = to_double_range(red); + red_ = to_allowed_color_range(red); } void color::set_green(double green) { - green_ = to_double_range(green); + green_ = to_allowed_color_range(green); } void color::set_blue(double blue) { - blue_ = to_double_range(blue); + blue_ = to_allowed_color_range(blue); } COLORREF color::get_color_ref() const From 74f7226e24191344e7d9434fd81eefbed4d6a2e2 Mon Sep 17 00:00:00 2001 From: tmalkoc Date: Tue, 13 Nov 2018 17:32:32 +0100 Subject: [PATCH 3/7] Tvrtko Malkoc - day2 - exercise 02 --- exercise/02/array.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++- exercise/02/array.h | 17 +++++++++++-- exercise/02/array.sln | 25 ++++++++++++++++++++ exercise/02/test.cpp | 14 ----------- 4 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 exercise/02/array.sln diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..1f124bf 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,54 @@ -#include "array.h" +#include "array.h" +#include +#include + +array::array(int size, double value) : + ptr_(new double[size]), + size_(size) +{ + for (int i = 0; i < size; i++) + ptr_[i] = value; +} + +array::array(const array& source) : + ptr_(new double[source.size_]), + size_(source.size_) +{ + for (int i = 0; i < size_; i++) + ptr_[i] = source.ptr_[i]; +} + +array::array(array&& source) noexcept : + ptr_(source.ptr_), + size_(source.size_) +{ + source.size_ = 0; + source.ptr_ = nullptr; +} + +array::~array() +{ + delete[] ptr_; +} + +int array::size() const +{ + return size_; +} + +double array::at(const int i) const +{ + if (i < 0 || size_ < i) + throw std::out_of_range("array::at(idx) - index is out of range. idx = " + std::to_string(i)); + return ptr_[i]; +} + +array& array::operator=(const array& other) +{ + delete[] ptr_; + size_ = other.size_; + ptr_ = new double[size_]; + for (int i = 0; i < size_; i++) + ptr_[i] = other.ptr_[i]; + return *this; +} diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..bb6bdeb 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: + array(int size = 0, double value = 0.0); + array(const array&); + array(array&&) noexcept; + ~array(); + + int size() const; + double at(int i) const; + array& operator=(const array&); + +private: + double* ptr_; + int size_; }; diff --git a/exercise/02/array.sln b/exercise/02/array.sln new file mode 100644 index 0000000..2d45924 --- /dev/null +++ b/exercise/02/array.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2036 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "array", "array.vcxproj", "{BB15329E-10D8-4740-94BC-64C50BA4EE12}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Debug|x86.ActiveCfg = Debug|Win32 + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Debug|x86.Build.0 = Debug|Win32 + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Release|x86.ActiveCfg = Release|Win32 + {BB15329E-10D8-4740-94BC-64C50BA4EE12}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {80ABAACF-7629-4CB8-92B8-A00DC6BF76A1} + EndGlobalSection +EndGlobal 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 5b4b01a409274543c9962690638d1e43dcc4fd85 Mon Sep 17 00:00:00 2001 From: tmalkoc Date: Tue, 13 Nov 2018 17:40:42 +0100 Subject: [PATCH 4/7] exercise/02/array.cpp - code cleanup --- exercise/02/array.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 1f124bf..664f301 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -29,6 +29,8 @@ array::array(array&& source) noexcept : array::~array() { delete[] ptr_; + ptr_ = nullptr; + size_ = 0; } int array::size() const @@ -39,14 +41,14 @@ int array::size() const double array::at(const int i) const { if (i < 0 || size_ < i) - throw std::out_of_range("array::at(idx) - index is out of range. idx = " + std::to_string(i)); + throw std::out_of_range("array::at(idx) - index is out of range. idx == " + std::to_string(i)); return ptr_[i]; } array& array::operator=(const array& other) { - delete[] ptr_; size_ = other.size_; + delete[] ptr_; ptr_ = new double[size_]; for (int i = 0; i < size_; i++) ptr_[i] = other.ptr_[i]; From 5996cb970a0a6ab4631613d43f3e50ba15b82009 Mon Sep 17 00:00:00 2001 From: tmalkoc Date: Wed, 14 Nov 2018 09:16:11 +0100 Subject: [PATCH 5/7] bugfix --- exercise/01/color.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp index c9e94aa..b026dfc 100644 --- a/exercise/01/color.cpp +++ b/exercise/01/color.cpp @@ -61,7 +61,7 @@ bool operator==(const color& c1, const color& c2) { const auto precision = 1. / 256.; return - (c1.get_red() - c2.get_red() < precision) && - (c1.get_green() - c2.get_green() < precision) && - (c1.get_blue() - c2.get_blue() < precision); + (std::fabs(c1.get_red() - c2.get_red()) < precision) && + (std::fabs(c1.get_green() - c2.get_green()) < precision) && + (std::fabs(c1.get_blue() - c2.get_blue()) < precision); } From bf7810e607c220ad072d271e87bed0d1ee3026a9 Mon Sep 17 00:00:00 2001 From: tmalkoc Date: Wed, 14 Nov 2018 10:33:02 +0100 Subject: [PATCH 6/7] code improvements --- exercise/01/color.cpp | 26 +++++++++++++++++--------- exercise/01/color.h | 16 +++++++++++++--- exercise/02/array.cpp | 15 +++++++++------ 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp index b026dfc..bc44efd 100644 --- a/exercise/01/color.cpp +++ b/exercise/01/color.cpp @@ -4,17 +4,22 @@ namespace { double to_allowed_color_range(double d) { - return d < 0.0 ? 0.0 : 1.0 < d ? 1.0 : d; + return + d < color::min_value ? color::min_value : + color::max_value < d ? color::max_value : + d; } int to_char_range(double d) { - return static_cast(255.0 * to_allowed_color_range(d)); + return static_cast((color::rgb_color_depth - 1) * to_allowed_color_range(d)); } } -color::color(double r, double g, double b) - : red_(to_allowed_color_range(r)), green_(to_allowed_color_range(g)), blue_(to_allowed_color_range(b)) +color::color(double red, double green, double blue) : + red_(to_allowed_color_range(red)), + green_(to_allowed_color_range(green)), + blue_(to_allowed_color_range(blue)) { } double color::get_red() const @@ -54,14 +59,17 @@ COLORREF color::get_color_ref() const double color::get_luminance() const { - return 0.2126*red_ + 0.7152*green_ + 0.0722*blue_; + return + luminance_red * red_ + + luminance_green * green_ + + luminance_blue * blue_; } bool operator==(const color& c1, const color& c2) { - const auto precision = 1. / 256.; + const auto precision = 1. / color::rgb_color_depth; return - (std::fabs(c1.get_red() - c2.get_red()) < precision) && - (std::fabs(c1.get_green() - c2.get_green()) < precision) && - (std::fabs(c1.get_blue() - c2.get_blue()) < precision); + std::fabs(c1.get_red() - c2.get_red()) < precision && + std::fabs(c1.get_green() - c2.get_green()) < precision && + std::fabs(c1.get_blue() - c2.get_blue()) < precision; } diff --git a/exercise/01/color.h b/exercise/01/color.h index ebb71cd..ceeb52c 100644 --- a/exercise/01/color.h +++ b/exercise/01/color.h @@ -3,7 +3,18 @@ class color { public: - color(double r = 0.0, double g = 0.0, double b = 0.0); + inline static const double min_value = 0.0; + inline static const double max_value = 1.0; + + inline static const double rgb_color_depth = 256.; + + inline static const double luminance_red = 0.2126; + inline static const double luminance_green = 0.7152; + inline static const double luminance_blue = 0.0722; + + color(double red = 0.0, double green = 0.0, double blue = 0.0); + color(const color&) = default; + ~color() = default; double get_red() const; double get_green() const; @@ -20,8 +31,7 @@ class color double red_; double green_; double blue_; - }; -bool operator ==(const color& c1, const color& c2); +bool operator==(const color& c1, const color& c2); diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 664f301..905629f 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -47,10 +47,13 @@ double array::at(const int i) const array& array::operator=(const array& other) { - size_ = other.size_; - delete[] ptr_; - ptr_ = new double[size_]; - for (int i = 0; i < size_; i++) - ptr_[i] = other.ptr_[i]; + if (this != &other) + { + size_ = other.size_; + delete[] ptr_; + ptr_ = new double[size_]; + for (int i = 0; i < size_; i++) + ptr_[i] = other.ptr_[i]; + } return *this; -} +} From 6bcee21a4ae6b1d6996faa32bc37d36be014df94 Mon Sep 17 00:00:00 2001 From: tmalkoc Date: Wed, 14 Nov 2018 11:07:44 +0100 Subject: [PATCH 7/7] Tvrtko Malkoc - day2 - exercise 03 --- exercise/03/operators.sln | 25 +++++++++++++++++++++++++ exercise/03/point.cpp | 21 +++++++++++++++++++++ exercise/03/point.h | 18 ++++++++++++++++++ exercise/03/test.cpp | 11 +++-------- 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 exercise/03/operators.sln diff --git a/exercise/03/operators.sln b/exercise/03/operators.sln new file mode 100644 index 0000000..f3371c1 --- /dev/null +++ b/exercise/03/operators.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2036 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "operators", "operators.vcxproj", "{86A75A8D-048F-4B18-A466-A62C44E92490}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {86A75A8D-048F-4B18-A466-A62C44E92490}.Debug|x86.ActiveCfg = Debug|Win32 + {86A75A8D-048F-4B18-A466-A62C44E92490}.Debug|x86.Build.0 = Debug|Win32 + {86A75A8D-048F-4B18-A466-A62C44E92490}.Release|x86.ActiveCfg = Release|Win32 + {86A75A8D-048F-4B18-A466-A62C44E92490}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1EBF3608-33AB-4668-B2D4-BBC03D73B310} + EndGlobalSection +EndGlobal diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 5c7c45f..7d4c936 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,23 @@ #include "point.h" +point::point(int x, int y): + x(x), y(y) +{ } + +bool operator==(const point& p1, const point& p2) +{ + return p1.x == p2.x && p1.y == p2.y; +} + +bool operator!=(const point& p1, const point& p2) +{ + return !(p1 == p2); +} + +bool operator<(const point& p1, const point& p2) +{ + return + p1.x != p2.x ? + p1.x < p2.x : + p1.y < p2.y; +} diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..900dfbb 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,18 @@ +#pragma once + +class point +{ + +public: + point(int x = 0, int y = 0); + point(const point&) = default; + ~point() = default; + + const int x; + const int y; +}; + +bool operator==(const point& p1, const point& p2); +bool operator!=(const point& p1, const point& p2); +bool operator<(const point& p1, const point& p2); + diff --git a/exercise/03/test.cpp b/exercise/03/test.cpp index d8e6669..0c11a42 100644 --- a/exercise/03/test.cpp +++ b/exercise/03/test.cpp @@ -9,30 +9,25 @@ 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)); - */ } };