From d99258ec5c4bced0023c077d84caeb1864b0926a Mon Sep 17 00:00:00 2001 From: Mislav Date: Tue, 13 Nov 2018 16:54:35 +0100 Subject: [PATCH 1/7] Solution for day2 exercise 1 --- exercise/01/color.cpp | 71 ++++++++++++++++++++++++++++++++++++++ exercise/01/color.h | 28 +++++++++++++++ exercise/01/color_test.cpp | 7 ++++ exercise/01/stdafx.h | 1 + 4 files changed, 107 insertions(+) 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..c8ab5e5 --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,71 @@ +#include "stdafx.h" +#include "color.h" + +namespace +{ + double value_cutoff(double value) + { + return value > 0 ? std::min(value, 1.) : std::max(value, 0.); + } + + long scale_color(double value) + { + return 255 * value; + } +} + +color::color() : color{0, 0, 0} +{ +} + +color::color(const double red, const double green, const double blue) +{ + set_red(red); + set_green(green); + set_blue(blue); +} + +void color::set_red(double red) +{ + red_ = value_cutoff(red); +} + +void color::set_green(double green) +{ + green_ = value_cutoff(green); +} + +void color::set_blue(double blue) +{ + blue_ = value_cutoff(blue); +} + +double color::get_red() const +{ + return red_; +} + +double color::get_green() const +{ + return green_; +} + +double color::get_blue() const +{ + return blue_; +} + +unsigned long color::get_color_ref() const +{ + return scale_color(red_) + scale_color(green_) * 256 + scale_color(blue_) * 65536; +} + +double color::get_luminance() const +{ + return 0.2126 * red_ + 0.7152 * green_ + 0.0722 * blue_; +} + +bool operator==(const color& lhs, const color& rhs) +{ + return lhs.red_ == rhs.red_ && lhs.green_ == rhs.green_ && lhs.blue_ == rhs.blue_; +} diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..88efbfc --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,28 @@ +#pragma once + +class color +{ +private: + double red_; + double green_; + double blue_; + +public: + color(); + color(double red, double green, double blue); + + 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; + double get_luminance() const; + + friend bool operator==(const color&, const color&); +}; + +bool operator==(const color& lhs, const color& rhs); \ No newline at end of file diff --git a/exercise/01/color_test.cpp b/exercise/01/color_test.cpp index 1af693e..d007ac0 100644 --- a/exercise/01/color_test.cpp +++ b/exercise/01/color_test.cpp @@ -69,6 +69,13 @@ namespace color_test TEST_METHOD(test_conversion_colorref) { + auto t1 = RGB(255, 0, 0); + auto t2 = RGB(0, 255, 0); + auto t3 = RGB(0, 0, 255); + auto t4 = RGB(255, 255, 255); + auto t5 = RGB(127, 127, 127); + long t6 = 255 * 0.5; + Assert::AreEqual(RGB(255, 0, 0), color( 1, 0, 0 ).get_color_ref()); Assert::AreEqual(RGB( 0, 255, 0), color( 0, 1, 0 ).get_color_ref()); Assert::AreEqual(RGB( 0, 0, 255), color( 0, 0, 1 ).get_color_ref()); diff --git a/exercise/01/stdafx.h b/exercise/01/stdafx.h index 6027cdf..a268b31 100644 --- a/exercise/01/stdafx.h +++ b/exercise/01/stdafx.h @@ -2,3 +2,4 @@ #include #include "CppUnitTest.h" +#include From bc7989b579426574bc02c958d1ef1ab29d402d7d Mon Sep 17 00:00:00 2001 From: Mislav Date: Tue, 13 Nov 2018 17:48:39 +0100 Subject: [PATCH 2/7] Solution for day 2 exercise 2 --- exercise/01/color_test.sln | 31 ++++++++++ exercise/02/array.cpp | 52 ++++++++++++++++ exercise/02/array.h | 17 +++++- exercise/02/array.vcxproj | 1 + exercise/02/test.cpp | 119 ++++++++++++++++--------------------- 5 files changed, 151 insertions(+), 69 deletions(-) create mode 100644 exercise/01/color_test.sln diff --git a/exercise/01/color_test.sln b/exercise/01/color_test.sln new file mode 100644 index 0000000..3d9361a --- /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.2050 +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 = {770F20AC-2CA1-418A-9FEF-E30F914E55E6} + EndGlobalSection +EndGlobal diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..02ecbad 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,53 @@ #include "array.h" +#include +#include + +array::array() : p_{nullptr}, n_{0} +{ +} + +array::array(int size, double value) : p_{new double[size]}, n_{size} +{ + std::fill_n(p_, size, value); +} + +array::array(const array& other) : array(other.n_, other.at(0)) +{ +} + +array::array(array&& other) noexcept : p_{other.p_}, n_{other.n_} +{ + other.p_ = nullptr; + other.n_ = 0; +} + +array::~array() +{ + delete[] p_; + p_ = nullptr; +} + +int array::size() const +{ + return n_; +} + +double array::at(int index) const +{ + if (index < 0 || index >= n_) throw std::out_of_range("Index is out of range"); + return p_[index]; +} + +array& array::operator=(const array& other) +{ + if(this != &other) + { + delete[] p_; + n_ = other.n_; + p_ = new double[n_]; + + std::fill_n(p_, n_, other.at(0)); + }; + + return *this; +} diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..dd31f78 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; +private: + double* p_; + int n_; public: + array(); + array(int size, double value); + array(const array& other); + array(array&& other) noexcept; + ~array(); + + int size() const; + double at(int index) const; + + array& operator=(const array& other); }; diff --git a/exercise/02/array.vcxproj b/exercise/02/array.vcxproj index 16d226c..e2ab9cd 100644 --- a/exercise/02/array.vcxproj +++ b/exercise/02/array.vcxproj @@ -56,6 +56,7 @@ $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) WIN32;_DEBUG;%(PreprocessorDefinitions) true + stdcpp17 Windows diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..6f9ffd9 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -5,80 +5,65 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; TEST_CLASS(test_array) { - const int size = 10; - const double value = 1.5; + const int size = 10; + const double value = 1.5; public: - - 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_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_negative) - { - /* - array a(size, value); - auto func = [a]() { a.at(-2); }; - Assert::ExpectException(func); - */ - } + TEST_METHOD(array_index_invalid_large) + { + array a(size, value); + auto func = [a]() { a.at(100); }; + 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_index_invalid_negative) + { + array a(size, value); + auto func = [a]() { a.at(-2); }; + Assert::ExpectException(func); + } - 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_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_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)); - */ - } + 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 e3f6385123709b2843559c2ea6d6db32edb2e901 Mon Sep 17 00:00:00 2001 From: Mislav Date: Tue, 13 Nov 2018 18:02:32 +0100 Subject: [PATCH 3/7] Move assignment --- exercise/02/array.cpp | 22 ++++++++++++++++++---- exercise/02/array.h | 2 ++ exercise/02/array.sln | 25 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 exercise/02/array.sln diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 02ecbad..b5408f7 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -11,7 +11,7 @@ array::array(int size, double value) : p_{new double[size]}, n_{size} std::fill_n(p_, size, value); } -array::array(const array& other) : array(other.n_, other.at(0)) +array::array(const array& other) : array(other.n_, other.p_ != nullptr ? other.at(0) : 0) { } @@ -40,14 +40,28 @@ double array::at(int index) const array& array::operator=(const array& other) { - if(this != &other) + if (this != &other) { - delete[] p_; + delete[] p_; n_ = other.n_; p_ = new double[n_]; std::fill_n(p_, n_, other.at(0)); - }; + } return *this; } + +array& array::operator=(array&& other) noexcept +{ + if (this != &other) + { + delete[] p_; + p_ = other.p_; + n_ = other.n_; + + other.p_ = nullptr; + other.n_ = 0; + } + return *this; +} diff --git a/exercise/02/array.h b/exercise/02/array.h index dd31f78..88cbf44 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -6,6 +6,7 @@ class array private: double* p_; int n_; + public: array(); array(int size, double value); @@ -17,4 +18,5 @@ class array double at(int index) const; array& operator=(const array& other); + array& operator=(array&& other) noexcept; }; diff --git a/exercise/02/array.sln b/exercise/02/array.sln new file mode 100644 index 0000000..d4e341e --- /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.2050 +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 = {8FF17F18-E60F-4D76-B3B6-97439B571F92} + EndGlobalSection +EndGlobal From 2e42b325a9e576e0946daf9d7b26c901d29886c5 Mon Sep 17 00:00:00 2001 From: Mislav Date: Tue, 13 Nov 2018 21:31:11 +0100 Subject: [PATCH 4/7] Solution for exercise 3 --- exercise/03/point.cpp | 38 +++++++++++++++++++++++++++++++ exercise/03/point.h | 18 +++++++++++++++ exercise/03/test.cpp | 52 ++++++++++++++++++------------------------- 3 files changed, 78 insertions(+), 30 deletions(-) diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 5c7c45f..25dd202 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,40 @@ #include "point.h" +point::point() : point(0, 0) +{ +} + +point::point(int x, int y) : x_{x}, y_{y} +{ +} + +bool point::operator==(const point& other) const +{ + return x_ == other.x_ && y_ == other.y_; +} + +bool point::operator!=(const point& other) const +{ + return !(*this == other); +} + +bool point::operator<(const point& other) const +{ + if (x_ == other.x_) return y_ < other.y_; + return x_ < other.x_; +} + +bool point::operator>=(const point& other) const +{ + return !(*this < other); +} + +bool point::operator>(const point& other) const +{ + return !(*this < other || *this == other); +} + +bool point::operator<=(const point& other) const +{ + return !(*this > other); +} diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..3cdf9d2 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,18 @@ +class point +{ +private: + int x_; + int y_; + +public: + point(); + point(int x, int y); + + bool operator==(const point& other) const; + bool operator!=(const point& other) const; + bool operator<(const point& other) const; + bool operator>=(const point& other) const; + bool operator>(const point& other) const; + bool operator<=(const point& other) const; + +}; diff --git a/exercise/03/test.cpp b/exercise/03/test.cpp index d8e6669..ae1ebcf 100644 --- a/exercise/03/test.cpp +++ b/exercise/03/test.cpp @@ -1,38 +1,30 @@ #include "CppUnitTest.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; - #include "point.h" - TEST_CLASS(test_1_operators) { public: - - TEST_METHOD(point_default_constructor) - { - /* - Assert::IsTrue(point(0, 0) == point()); - */ - } + 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::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 94decba2d0901731e101fa96249caccc90eae686 Mon Sep 17 00:00:00 2001 From: Mislav Date: Tue, 13 Nov 2018 21:35:44 +0100 Subject: [PATCH 5/7] Tests for additional operators in exercise 3 --- exercise/03/operators.sln | 25 +++++++++++++++++++++++++ exercise/03/point.cpp | 6 +++--- exercise/03/point.h | 3 --- exercise/03/test.cpp | 24 ++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 6 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..304a907 --- /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.2050 +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 = {E20A7960-961B-4BC2-8654-7CFC0059B2E5} + EndGlobalSection +EndGlobal diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 25dd202..ac16577 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -26,15 +26,15 @@ bool point::operator<(const point& other) const bool point::operator>=(const point& other) const { - return !(*this < other); + return !(*this < other); } bool point::operator>(const point& other) const { - return !(*this < other || *this == other); + return !(*this < other || *this == other); } bool point::operator<=(const point& other) const { - return !(*this > other); + return !(*this > other); } diff --git a/exercise/03/point.h b/exercise/03/point.h index 3cdf9d2..0abeb99 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -3,16 +3,13 @@ class point private: int x_; int y_; - public: point(); point(int x, int y); - bool operator==(const point& other) const; bool operator!=(const point& other) const; bool operator<(const point& other) const; bool operator>=(const point& other) const; bool operator>(const point& other) const; bool operator<=(const point& other) const; - }; diff --git a/exercise/03/test.cpp b/exercise/03/test.cpp index ae1ebcf..7859c7a 100644 --- a/exercise/03/test.cpp +++ b/exercise/03/test.cpp @@ -27,4 +27,28 @@ TEST_CLASS(test_1_operators) Assert::IsTrue(point(1, 2) < point(2, 2)); Assert::IsFalse(point(1, 2) < point(1, 2)); } + + TEST_METHOD(points_less_or_equal) + { + Assert::IsTrue(point(3, 4) <= point(3, 4)); + Assert::IsTrue(point(3, 4) <= point(4, 4)); + Assert::IsTrue(point(2, 5) <= point(3, 4)); + Assert::IsTrue(point(3, 4) <= point(3, 5)); + } + + TEST_METHOD(points_greater_or_equal) + { + Assert::IsTrue(point(3, 4) >= point(3, 4)); + Assert::IsTrue(point(3, 4) >= point(2, 4)); + Assert::IsTrue(point(4, 3) >= point(3, 4)); + Assert::IsTrue(point(3, 4) >= point(3, 3)); + } + + TEST_METHOD(points_greater_than) + { + Assert::IsFalse(point(3, 4) > point(3, 4)); + Assert::IsTrue(point(3, 4) > point(2, 4)); + Assert::IsTrue(point(4, 3) > point(3, 4)); + Assert::IsTrue(point(3, 6) > point(3, 5)); + } }; From a25ff893bca323fc0ebfb223bfe5b81ae5b4f503 Mon Sep 17 00:00:00 2001 From: Mislav Date: Tue, 13 Nov 2018 21:45:47 +0100 Subject: [PATCH 6/7] Move ctor and move assignment tests --- exercise/02/test.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 6f9ffd9..8097b96 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -1,15 +1,11 @@ #include "CppUnitTest.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; - #include "array.h" - TEST_CLASS(test_array) { const int size = 10; const double value = 1.5; - public: - TEST_METHOD(array_default_constructor) { array a; @@ -20,8 +16,7 @@ TEST_CLASS(test_array) { array a(size, value); Assert::AreEqual(size, a.size()); - for (int i = 0; i < a.size(); ++i) - Assert::AreEqual(value, a.at(i)); + for (int i = 0; i < a.size(); ++i) Assert::AreEqual(value, a.at(i)); } TEST_METHOD(array_index_invalid_large) @@ -44,8 +39,7 @@ TEST_CLASS(test_array) array b; b = a; Assert::AreEqual(size, b.size()); - for (int i = 0; i < b.size(); ++i) - Assert::AreEqual(value, b.at(i)); + for (int i = 0; i < b.size(); ++i) Assert::AreEqual(value, b.at(i)); } TEST_METHOD(array_copy_constructor) @@ -53,17 +47,25 @@ TEST_CLASS(test_array) 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)); + for (int i = 0; i < b.size(); ++i) Assert::AreEqual(value, b.at(i)); } - TEST_METHOD(array_move_constructor) + TEST_METHOD(array_move_assignment) { 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)); + 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(size, b.size()); + Assert::AreEqual(0, a.size()); + Assert::ExpectException([a]() { a.at(0); }); + for (int i = 0; i < b.size(); ++i) Assert::AreEqual(value, b.at(i)); } }; From 67554b8e9a8e7d7ed5f241d8cd4b99742991f61b Mon Sep 17 00:00:00 2001 From: Mislav Date: Wed, 14 Nov 2018 10:59:05 +0100 Subject: [PATCH 7/7] One liner --- exercise/03/point.cpp | 3 +-- exercise/03/test.cpp | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index ac16577..68c5e3c 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -20,8 +20,7 @@ bool point::operator!=(const point& other) const bool point::operator<(const point& other) const { - if (x_ == other.x_) return y_ < other.y_; - return x_ < other.x_; + return x_ == other.x_ ? y_ < other.y_ : x_ < other.x_; } bool point::operator>=(const point& other) const diff --git a/exercise/03/test.cpp b/exercise/03/test.cpp index 7859c7a..6f4562a 100644 --- a/exercise/03/test.cpp +++ b/exercise/03/test.cpp @@ -42,6 +42,7 @@ TEST_CLASS(test_1_operators) Assert::IsTrue(point(3, 4) >= point(2, 4)); Assert::IsTrue(point(4, 3) >= point(3, 4)); Assert::IsTrue(point(3, 4) >= point(3, 3)); + } TEST_METHOD(points_greater_than)