diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp new file mode 100644 index 0000000..bfca93b --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,84 @@ +#include "stdafx.h" +#include "color.h" + + +double const color::m_MinValue = 0.0; +double const color::m_MaxValue = 1.0; + +color::color() +{ + m_fRed = 0.0; + m_fGreen = 0.0; + m_fBlue = 0.0; +} + +color::color(double red, double green, double blue) +{ + set_red(red); + set_green(green); + set_blue(blue); +} + +color::color(const color& Color) +{ + m_fRed = Color.get_red(); + m_fGreen = Color.get_green(); + m_fBlue = Color.get_blue(); +} + +color::~color() +{ +} + +bool color::operator==(const color& second) const +{ + return + ((m_fRed == second.get_blue()) && + (m_fGreen == second.get_green()) && + (m_fBlue == second.get_blue()) ); +} + +double color::get_red() const +{ + return m_fRed; +} + +double color::get_green() const +{ + return m_fGreen; +} + +double color::get_blue() const +{ + return m_fBlue; +} + + +void color::set_red(double red) +{ + m_fRed = std::clamp(red, m_MinValue, m_MaxValue); +} + +void color::set_green(double green) +{ + m_fGreen = std::clamp(green, m_MinValue, m_MaxValue); +} + +void color::set_blue(double blue) +{ + m_fBlue = std::clamp(blue, m_MinValue, m_MaxValue); +} + +COLORREF color::get_color_ref() +{ + unsigned long red = (unsigned long)(m_fRed * 255); + unsigned long green = (unsigned long)(m_fGreen * 255); + unsigned long blue = (unsigned long)(m_fBlue * 255); + + return RGB(red, green, blue); +} + +double color::get_luminance() +{ + return (double)(0.2126 * m_fRed + 0.7152 * m_fGreen + 0.0722 * m_fBlue); +} \ No newline at end of file diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..df367ef --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,32 @@ +#pragma once + +class color +{ +public: + color(); + color(double red, double green, double blue); + color(const color& Color); + ~color(); + + bool operator==(const color& second) 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(); + double get_luminance(); + +private: + double m_fRed; + double m_fGreen; + double m_fBlue; + + static double const m_MinValue; + static double const m_MaxValue; +}; + diff --git a/exercise/01/color_test.cpp b/exercise/01/color_test.cpp index 1af693e..d3c89b1 100644 --- a/exercise/01/color_test.cpp +++ b/exercise/01/color_test.cpp @@ -1,16 +1,25 @@ #include "stdafx.h" #include "color.h" -#include // RGB + +#include using namespace Microsoft::VisualStudio::CppUnitTestFramework; -namespace Microsoft { namespace VisualStudio { namespace CppUnitTestFramework { -template<> static std::wstring ToString(const color& c) { - std::wostringstream ss; - ss << c.get_red() << L',' << c.get_green() << L',' << c.get_blue(); - return ss.str(); +namespace Microsoft +{ + namespace VisualStudio + { + namespace CppUnitTestFramework + { + template<> static std::wstring ToString(const color& c) + { + std::wostringstream ss; + ss << c.get_red() << L',' << c.get_green() << L',' << c.get_blue(); + return ss.str(); + } + } + } } -}}} namespace color_test { 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..87818fb 100644 --- a/exercise/01/stdafx.h +++ b/exercise/01/stdafx.h @@ -1,4 +1,7 @@ #pragma once +#include +#include + #include -#include "CppUnitTest.h" +#include "CppUnitTest.h" \ No newline at end of file diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..d98899e 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,82 @@ #include "array.h" + + +array::array(): + m_iSize(0), + m_pToArray(nullptr) +{ +} + + +array::array(int size, double value): + m_iSize(0), + m_pToArray(nullptr) +{ + if (size < 0) + { + throw std::out_of_range("Array size can't be negative! Aborting..."); + } + + m_pToArray = new double[size]; + m_iSize = size; + + for (int i = 0; i < size; i++) + { + m_pToArray[i] = value; + } +} + + +array::array(const array& Array): + m_iSize(0), + m_pToArray(nullptr) +{ + if (Array.m_iSize > 0) + { + m_pToArray = new double[Array.m_iSize]; + m_iSize = Array.m_iSize; + + for (int i = 0; i < m_iSize; i++) + { + m_pToArray[i] = Array.m_pToArray[i]; + } + } +} + + +array::~array() +{ + if (m_pToArray) + { + delete[] m_pToArray; + m_pToArray = nullptr; + } +} + + +int array::size() +{ + return m_iSize; +} + + +double array::at(int index) const +{ + if (index < 0 || index > m_iSize) + { + throw std::out_of_range("Index of array is out of range! Aborting..."); + } + + return m_pToArray[index]; +} + + +array& array::operator=(const array& second) +{ + array temp(second); + + std::swap(m_pToArray, temp.m_pToArray); + std::swap(m_iSize, temp.m_iSize); + + return *this; +} \ No newline at end of file diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..897f57a 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -1,7 +1,21 @@ // do not use standard container as member or base class + +#include + class array { - double* p; - int n; public: + array(); + array(int size, double value); + array(const array& Array); + ~array(); + + array& operator=(const array&); + + int size(); + double at(int index) const; + +private: + double* m_pToArray; + int m_iSize; }; diff --git a/exercise/02/array.vcxproj b/exercise/02/array.vcxproj index 16d226c..75f95c3 100644 --- a/exercise/02/array.vcxproj +++ b/exercise/02/array.vcxproj @@ -1,95 +1,96 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - - {BB15329E-10D8-4740-94BC-64C50BA4EE12} - Win32Proj - array - 10.0.17134.0 - - - - DynamicLibrary - true - v141 - Unicode - false - - - DynamicLibrary - false - v141 - true - Unicode - false - - - - - - - - - - - - - true - - - true - - - - NotUsing - Level3 - Disabled - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;_DEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - Level3 - NotUsing - MaxSpeed - true - true - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;NDEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - true - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + + {BB15329E-10D8-4740-94BC-64C50BA4EE12} + Win32Proj + array + 10.0.17134.0 + + + + DynamicLibrary + true + v141 + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + + + + + + + + + + + true + + + true + + + + NotUsing + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + + + Windows + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + NotUsing + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + stafx.h + + + Windows + true + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + + + + + + + + \ No newline at end of file diff --git a/exercise/02/array.vcxproj.filters b/exercise/02/array.vcxproj.filters index 682ea6d..69e770e 100644 --- a/exercise/02/array.vcxproj.filters +++ b/exercise/02/array.vcxproj.filters @@ -1,24 +1,24 @@ - - - - - {f2ada0ee-91e7-4bbd-b33d-837567340243} - - - {3cc0fd48-6585-4969-94bd-406daa5f786b} - - - - - test - - - src - - - - - src - - + + + + + {f2ada0ee-91e7-4bbd-b33d-837567340243} + + + {3cc0fd48-6585-4969-94bd-406daa5f786b} + + + + + test + + + src + + + + + src + + \ No newline at end of file diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..7f999c7 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -12,73 +12,61 @@ 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(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)); - */ } }; diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 5c7c45f..019f7cc 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,32 @@ #include "point.h" +point::point(): + m_x(0), + m_y(0) +{ +} + +point::point(int x, int y): + m_x(x), + m_y(y) +{ +} + +point::~point() +{ +} + +bool point::operator==(const point& second) +{ + return (m_x == second.m_x && m_y == second.m_y); +} + +bool point::operator!=(const point& second) +{ + return (m_x != second.m_x || m_y != second.m_y); +} + +bool point::operator<(const point& second) +{ + return (m_x <= second.m_x && m_y < second.m_y) || (m_x < second.m_x && m_y <= second.m_y); +} diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..6b19323 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,16 @@ + +class point +{ +public: + point(); + point(int x, int y); + ~point(); + + bool operator==(const point& second); + bool operator!=(const point& second); + bool operator<(const point& second); + +private: + int m_x; + int m_y; +}; \ No newline at end of file diff --git a/exercise/03/test.cpp b/exercise/03/test.cpp index d8e6669..abb842a 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)); - */ } };