From 7f42259ddab06920ac64440c3c9e070220aa61bf Mon Sep 17 00:00:00 2001 From: Ljudevit Bracko Date: Tue, 13 Nov 2018 16:30:28 +0100 Subject: [PATCH 1/3] Rjesenja Ljudevit --- exercise/01/color.cpp | 53 +++++ exercise/01/color.h | 20 ++ exercise/01/color_test.vcxproj | 348 ++++++++++++++++----------------- exercise/02/array.cpp | 59 ++++++ exercise/02/array.h | 10 +- exercise/02/test.cpp | 12 -- 6 files changed, 315 insertions(+), 187 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..eceb6cd --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,53 @@ +#include "stdafx.h" +#include "color.h" + +namespace { + // Convert to [0, 1.0] range + double to_correct_value(const double input) { + if (input > 1.0) return 1.0; + if (input < 0.0) return 0.0; + return input; + } +} + +color::color(double red, double green, double blue) { + red_ = to_correct_value(red); + green_ = to_correct_value(green); + blue_ = to_correct_value(blue); +} + +bool color::operator==(const color& other) const { + return red_ == other.red_ && green_ == other.green_ && blue_ == other.blue_; +} + +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(const double red) { + red_ = to_correct_value(red); +} + +void color::set_green(const double green) { + green_ = to_correct_value(green); +} + +void color::set_blue(const double blue) { + blue_ = to_correct_value(blue); +} + +COLORREF color::get_color_ref() const { + return RGB(static_cast(255 * red_), static_cast(255 * green_), static_cast(255 * blue_)); +} + +double color::get_luminance() const { + return red_ * 0.2126 + green_ * 0.7152 + blue_ * 0.0722; +} \ No newline at end of file diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..58209e6 --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,20 @@ +#pragma once +#include + +class color { +public: + color(double red = 0.0, double green = 0.0, double blue = 0.0); + bool operator==(const color& other) 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 red_; + double green_; + double blue_; +}; \ No newline at end of file diff --git a/exercise/01/color_test.vcxproj b/exercise/01/color_test.vcxproj index 261fee9..574ba86 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/02/array.cpp b/exercise/02/array.cpp index 9247ffd..fe0921a 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,60 @@ #include "array.h" +#include + +namespace { + void populate_array(double* array, int size, double value) { + for (int index = 0; index < size; index++) + array[index] = value; + } + + void copy_array(double* source, double* target, int size) { + for (int index = 0; index < size; index++) + target[index] = source[index]; + } +} + +array::array() { + n = 0; + p = nullptr; +} + +array::array(int size, double value) { + n = size; + p = new double[size]; + populate_array(p, n, value); +} + +// Needed since we have to copy internal array +array::array(const array& other) { + n = other.n; + p = new double[n]; + copy_array(other.p, p, n); +} + +array::array(array&& other) { + n = other.n; + p = other.p; + other.p = nullptr; +} + +array::~array() { + delete [] p; +} + +array& array::operator=(const array& other) { + delete[] p; + n = other.n; + p = new double[n]; + copy_array(other.p, p, n); + return *this; +} + +int array::size() const { + return n; +} + +double array::at(int index) const { + if (index < 0 || index >= n) + throw std::out_of_range("Invalid index"); + return p[index]; +} \ No newline at end of file diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..d21a5a0 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -4,4 +4,12 @@ class array double* p; int n; public: -}; + array(); + array(int size, double value); + array(const array& other); + array(array&& other); + ~array(); + array& operator=(const array& other); + int size() const; + double at(int index) const; +}; \ No newline at end of file diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..dfe74bc 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -12,61 +12,49 @@ 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) From a67d16ef24d1ed1a876c213b28b85e923fa77ac3 Mon Sep 17 00:00:00 2001 From: Ljudevit Bracko Date: Tue, 13 Nov 2018 17:42:34 +0100 Subject: [PATCH 2/3] Fix setting size to 0 on move constructor --- exercise/02/array.cpp | 1 + exercise/02/test.cpp | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index fe0921a..0b6177f 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -35,6 +35,7 @@ array::array(array&& other) { n = other.n; p = other.p; other.p = nullptr; + other.n = 0; } array::~array() { diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index dfe74bc..55ed1b2 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -59,14 +59,12 @@ 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)); - */ } }; From 58ec2af503204fc8c06ece5af9c10257a6098e60 Mon Sep 17 00:00:00 2001 From: Ljudevit Bracko Date: Wed, 14 Nov 2018 10:50:28 +0100 Subject: [PATCH 3/3] 03 --- exercise/03/point.cpp | 20 ++++++++++++++++++++ exercise/03/point.h | 12 ++++++++++++ exercise/03/test.cpp | 10 +--------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 5c7c45f..86f2ff8 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,22 @@ #include "point.h" +point::point(int x, int y): x_(x), y_(y) +{ +} + +bool point::operator==(const point& other) +{ + return x_ == other.x_ && y_ == other.y_; +} + +bool point::operator!=(const point& other) +{ + return !(*this == other); +} + +bool point::operator<(const point& other) +{ + if (other.x_ != x_) + return _x < other.x_; + return y_ < other.y_; +} diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..13aa99e 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,12 @@ +class point +{ + int _x; + int _y; + int x_; + int y_; +public: + point(int x = 0, int y = 0); + 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..41a6655 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)); - */ + 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)); - */ } };