From ae6f42d496c87433eca22c1d85cc8267bc035195 Mon Sep 17 00:00:00 2001 From: Dean Leibner Date: Tue, 13 Nov 2018 18:13:11 +0100 Subject: [PATCH 1/3] exercise 01 --- exercise/01/color.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++ exercise/01/color.h | 26 +++++++++++++++++++ 2 files changed, 85 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..e04ffb0 --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,59 @@ +#include "stdafx.h" +#include "color.h" +#include + +const double color_min = 0.0; +const double color_max = 1.0; + +color::color(double r, double g, double b) : + red(std::clamp(r, color_min, color_max)), + green(std::clamp(g, color_min, color_max)), + blue(std::clamp(b, color_min, color_max)) +{ } + +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 value) +{ + red = std::clamp(value, color_min, color_max); +} + +void color::set_green(const double value) +{ + green = std::clamp(value, color_min, color_max); +} + +void color::set_blue(const double value) +{ + blue = std::clamp(value, color_min, color_max); +} + +bool color::operator==(const color& other) const +{ + return red == other.red && green == other.green && blue == other.blue; +} + +COLORREF color::get_color_ref() const +{ + return RGB(static_cast (red * 255), + static_cast (green * 255), + static_cast (blue * 255)); +} + +double color::get_luminance() const +{ + return 0.2126 * red + 0.7152 * green + 0.0722* blue; +} diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..71a53a2 --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,26 @@ +#pragma once +#include + +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 value); + void set_green(double value); + void set_blue(double value); + + bool operator==(const color& other) const; + + COLORREF get_color_ref() const; + double get_luminance() const; + +private: + double red; + double green; + double blue; +}; From 0c51fd703543149474518f0d542f7f2e3efe2ad6 Mon Sep 17 00:00:00 2001 From: Dean Leibner Date: Tue, 13 Nov 2018 19:55:10 +0100 Subject: [PATCH 2/3] exercise 02 --- exercise/02/array.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++ exercise/02/array.h | 17 +++++++-- exercise/02/test.cpp | 28 +++++++-------- 3 files changed, 110 insertions(+), 16 deletions(-) diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..47cb07a 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,82 @@ #include "array.h" +#include +#include + +array::array() : + p(nullptr), n(0) +{ +} + +array::array(const int size, const double value) +{ + if (size > 0) + { + n = size; + p = new double[size]; + + for (int i = 0; i < size; ++i) + { + p[i] = value; + } + } + else + { + n = 0; + p = nullptr; + } +} + +array::array(const array& other) : + p(nullptr), n(0) +{ + n = other.n; + + if (n > 0) + { + p = new double[n]; + + for (int i = 0; i < n; ++i) + { + p[i] = other.p[i]; + } + } +} + +array::array(array&& other) noexcept : + p(nullptr), n(0) +{ + std::swap(p, other.p); + std::swap(n, other.n); +} + +array::~array() +{ + if (p) + { + delete[] p; + p = nullptr; + } +} + +int array::size() const +{ + return n; +} + +double array::at(const int index) const +{ + if (index < 0 || index >= n) + { + throw std::out_of_range("index is out of range"); + } + + return p[index]; +} + +array& array::operator=(array other) +{ + std::swap(p, other.p); + std::swap(n, other.n); + + return *this; +} diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..a728c27 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(); + array(int size, double value = 0); + array(const array& other); + array(array&& other) noexcept; + ~array(); + + int size() const; + double at(int index) const; + + array& operator=(array other); + +private: + double* p; + int n; }; diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..afe9366 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -12,73 +12,73 @@ 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 69dbae644dd5fa9a1875a8cb870bce67c7ac3e62 Mon Sep 17 00:00:00 2001 From: Dean Leibner Date: Wed, 14 Nov 2018 10:56:06 +0100 Subject: [PATCH 3/3] exercise 03 --- exercise/03/point.cpp | 19 +++++++++++++++++++ exercise/03/point.h | 15 +++++++++++++++ exercise/03/test.cpp | 16 ++++++++-------- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 5c7c45f..650e4cc 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,21 @@ #include "point.h" +point::point(int f, int s) : + first(f), second(s) +{ +} + +bool point::operator==(const point& other) const +{ + return first == other.first && second == other.second; +} + +bool point::operator !=(const point& other) const +{ + return !(first == other.first && second == other.second); +} + +bool point::operator <(const point& other) const +{ + return first < other.first || second < other.second; +} diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..2470238 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,15 @@ +#pragma once + +class point +{ +public: + point(int f = 0, int s = 0); + + bool operator==(const point& other) const; + bool operator !=(const point& other) const; + bool operator <(const point& other) const; + +private: + int first; + int second; +}; \ 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)); - */ + } };