diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp new file mode 100644 index 0000000..a543430 --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,66 @@ +#include "stdafx.h" +#include "color.h" +#include +#include + + + +color::color(double const & red, double const & green, double const & blue) noexcept : + red(std::clamp(red, 0., 1.)), + green(std::clamp(green, 0., 1.)), + blue(std::clamp(blue, 0., 1.)) +{ +} + +double color::get_red() const noexcept +{ + return red; +} + +double color::get_green() const noexcept +{ + return green; +} + +double color::get_blue() const noexcept +{ + return blue; +} + +double color::get_luminance() const noexcept +{ + return 0.2126 * red + 0.7152 * green + 0.0722 * blue; +} + +COLORREF color::get_color_ref() const noexcept +{ + return RGB(char(red * 255), char(green * 255), char(blue * 255)); +} + +void color::set_red(double const & red) noexcept +{ + this->red = std::clamp(red, 0., 1.); +} + +void color::set_green(double const & green) noexcept +{ + this->green = std::clamp(green, 0., 1.); +} + +void color::set_blue(double const & blue) noexcept +{ + this->blue = std::clamp(blue, 0., 1.); +} + +bool color::operator==(color const & other) const noexcept +{ + return red == other.red && blue == other.blue && green == other.green; +} + +color const & color::operator=(color const & other) noexcept +{ + this->blue = other.blue; + this->red = other.red; + this->green = other.green; + return *this; +} diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..868a517 --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,24 @@ +#pragma once +#include + +struct color +{ + color() noexcept = default; + color(double const & red, double const & green, double const & blue) noexcept; + color(color const &) noexcept = default; + color(color&&) noexcept = default; + double get_red() const noexcept; + double get_green() const noexcept; + double get_blue() const noexcept; + double get_luminance() const noexcept; + COLORREF get_color_ref() const noexcept; + void set_red(double const & red) noexcept; + void set_green(double const & green) noexcept; + void set_blue(double const & blue) noexcept; + bool operator==(color const&) const noexcept; + color const & operator=(color const &) noexcept; +private: + double red{0}; + double green{0}; + double blue{0}; +}; \ No newline at end of file diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..d8ea959 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,73 @@ #include "array.h" +#include +#include +#include + +array::array(const int size, const double element) +{ + if (size < 0) + return; + p = new double[size]; + n = size; + std::generate_n(p, n, [&element]() {return element; }); +} + +array::array(array const & other) +{ + if (!other.size()) + return; + p = new double[other.size()]; + n = other.size(); + std::copy(other.p, other.p + other.size(), p); +} + +array::array(array && other) noexcept : + p(std::exchange(other.p, nullptr)), + n(std::exchange(other.n, 0)) +{ +} + +std::size_t array::size() const noexcept +{ + return std::size_t(n); +} + +array::~array() noexcept +{ + if (!p) + return; + + delete[] p; +} + +double const & array::at(int i) const +{ + if (i < 0 || i > n || size() == 0) + throw std::out_of_range("index out of range"); + return p[i]; +} + +array const & array::operator=(array const & other) +{ + if (std::addressof(other) == this) + return *this; + + array tmp = other; + *this = std::move(tmp); + return *this; +} + +array & array::operator=(array && other) noexcept +{ + if (std::addressof(other) == this) + return *this; + + if (p) + delete [] p; + + p = std::exchange(other.p, nullptr); + n = std::exchange(other.n, 0); + return *this; +} + + \ No newline at end of file diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..3993b4f 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -1,7 +1,17 @@ // do not use standard container as member or base class +#include class array { - double* p; - int n; + double* p{nullptr}; + int n{0}; public: + array() noexcept = default; + array(const int size, const double element); + array(array const &); + array(array&&) noexcept; + ~array() noexcept; + std::size_t size() const noexcept; + double const & at(int i) const; + array const & operator=(array const &); + array& operator=(array && other) noexcept; }; diff --git a/exercise/02/array.vcxproj b/exercise/02/array.vcxproj index 16d226c..8e5db52 100644 --- a/exercise/02/array.vcxproj +++ b/exercise/02/array.vcxproj @@ -1,95 +1,97 @@ - - - - - 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 + stdcpp17 + + + Windows + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + NotUsing + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + + + Windows + true + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + + + + + + + + \ No newline at end of file diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..5e5e876 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -5,80 +5,73 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; TEST_CLASS(test_array) { - const int size = 10; + const std::size_t size = 10; const double value = 1.5; public: TEST_METHOD(array_default_constructor) { - /* + array a; - Assert::AreEqual(0, a.size()); - */ + Assert::AreEqual(std::size_t(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) + for (std::size_t 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) + for (std::size_t 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(std::size_t(0), a.size()); Assert::AreEqual(size, b.size()); - for (int i = 0; i < b.size(); ++i) + for (std::size_t 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..b8a5228 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,21 @@ #include "point.h" +point::point(int x, int y) noexcept : + x(x), y(y) +{ +} + +bool point::operator!=(point const & right) const noexcept +{ + return !(*this == right); +} + +bool point::operator==(point const & right) const noexcept +{ + return right.x == x && right.y == y; +} + +bool point::operator<(point const & right) const noexcept +{ + return x != right.x ? x < right.x : y < right.y; +} diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..0950bc1 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,12 @@ +#pragma once + +struct point +{ + explicit point(int x = 0, int y = 0) noexcept; + bool operator==(point const &) const noexcept; + bool operator!=(point const &) const noexcept; + bool operator<(point const &) const noexcept; +private: + int x{ 0 }, y{0}; + +}; \ No newline at end of file diff --git a/exercise/03/test.cpp b/exercise/03/test.cpp index d8e6669..b40cf5d 100644 --- a/exercise/03/test.cpp +++ b/exercise/03/test.cpp @@ -9,30 +9,30 @@ 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)); - */ + } };