diff --git a/exercise/01/color.cpp b/exercise/01/color.cpp new file mode 100644 index 0000000..77b185b --- /dev/null +++ b/exercise/01/color.cpp @@ -0,0 +1,85 @@ +#include "stdafx.h" +#include "color.h" + + + +namespace +{ + double set_color(double v) + { + if (v < 0) + { + return 0.; + } + else if (v > 1) + { + return 1.; + } + + return v; + } +} + +color::color(double r, double g, double b) +{ + set_rgb(r, g, b); +} + +void color::set_rgb(double r, double g, double b) +{ + set_red(r); + set_green(g); + set_blue(b); +} + +const double color::get_red() const +{ + return r; +} + +const double color::get_green() const +{ + return g; +} + +const double color::get_blue() const +{ + return b; +} + +void color::set_red(const double r) +{ + this ->r = set_color(r); +} + +void color::set_green(const double g) +{ + this ->g = set_color(g); +} + +void color::set_blue(const double b) +{ + this ->b = set_color(b); +} + +unsigned long color::get_color_ref() const +{ + const double rgb_max_val = 255.0; + return RGB((int)(r*rgb_max_val), (int)(g*rgb_max_val), (int)(b * rgb_max_val)); +} + +double color::get_luminance() const +{ + return r * 0.2126 + g * 0.7152 + b * 0.0722; +} + +bool color::operator==(const color& other) const +{ + return (this->r == other.r && this->g == other.g && this->b == other.b); +} + +color color::operator=(const color& c) +{ + set_rgb(c.r, c.g, c.b); + return *this; +} \ No newline at end of file diff --git a/exercise/01/color.h b/exercise/01/color.h new file mode 100644 index 0000000..0c05134 --- /dev/null +++ b/exercise/01/color.h @@ -0,0 +1,26 @@ +#pragma once + +class color { +private: + double r; + double g; + double b; + +public: + color(double r = 0, double g = 0, double b = 0); + const double get_red() const; + const double get_green() const ; + const double get_blue() const; + void set_red(const double r); + void set_green(const double g); + void set_blue(const double b); + unsigned long get_color_ref() const; + double get_luminance() const; + + bool operator==(const color& color1) const; + color operator=(const color& c); + +private: + void set_rgb (double r, double g, double b); + +}; \ No newline at end of file diff --git a/exercise/01/color_test.cpp b/exercise/01/color_test.cpp index 1af693e..8527729 100644 --- a/exercise/01/color_test.cpp +++ b/exercise/01/color_test.cpp @@ -1,6 +1,5 @@ #include "stdafx.h" #include "color.h" -#include // RGB using namespace Microsoft::VisualStudio::CppUnitTestFramework; diff --git a/exercise/01/stdafx.h b/exercise/01/stdafx.h index 6027cdf..60d521f 100644 --- a/exercise/01/stdafx.h +++ b/exercise/01/stdafx.h @@ -2,3 +2,4 @@ #include #include "CppUnitTest.h" +#include diff --git a/exercise/02/array.cpp b/exercise/02/array.cpp index 9247ffd..30acf77 100644 --- a/exercise/02/array.cpp +++ b/exercise/02/array.cpp @@ -1 +1,88 @@ #include "array.h" +#include + +array::array() +{ + this ->p = nullptr; + this ->n = 0; +} + +array::array(const array& other) +{ + alloc_array(other.size()); + for (int i = 0; i < this->n; ++i) + { + p[i] = other.at(i); + } +} + +array::array(const int size, const double value):array() +{ + alloc_array(size); + for (int i = 0; i < size; i++) + { + p[i] = value; + } + +} + +array::~array() +{ + if (this->p != nullptr) + { + delete[] this->p; this->p = nullptr; + } + this->n = 0; +} + +void array::alloc_array(const int size) +{ + this->p = new double[size]; + if (this->p == nullptr) + { + throw; + } + + this->n = size; +} + +int array::size() const +{ + return this->n; +} + +double array::at(const int i) const +{ + if (! (i >= 0 && i < n)) + { + throw std::out_of_range("Value of array out of range"); + } + return p[i]; +} + +bool array::operator== (const array& other) const +{ + if (this->size() != other.size()) + { + return 0; + } + + for (int i = 0; i < size(); i++) + { + if (at(i) != other.at(i)) + return 0; + } + + return 1; + +} + +const array array::operator= (const array& other) +{ + alloc_array(other.size()); + for (int i = 0; i < this->n; i++) + { + p[i] = other.at(i); + } + return (*this); +} \ No newline at end of file diff --git a/exercise/02/array.h b/exercise/02/array.h index 20489df..8431e23 100644 --- a/exercise/02/array.h +++ b/exercise/02/array.h @@ -3,5 +3,17 @@ class array { double* p; int n; + +private: + void alloc_array(const int size); public: + array(); + array(const array& other); + array(const int size, const double value); + ~array(); + int size() const; + double at(const int i) const; + + bool operator==(const array& other) const; + const array operator= (const array& other); }; diff --git a/exercise/02/test.cpp b/exercise/02/test.cpp index 1adae67..91a9d50 100644 --- a/exercise/02/test.cpp +++ b/exercise/02/test.cpp @@ -12,73 +12,68 @@ 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)); - */ + } }; diff --git a/exercise/03/point.cpp b/exercise/03/point.cpp index 5c7c45f..f6baf8e 100644 --- a/exercise/03/point.cpp +++ b/exercise/03/point.cpp @@ -1,2 +1,30 @@ #include "point.h" +point::point(int x, int y) +{ + this->x = x; + this->y = y; +} + +bool point::operator==(const point& other) +{ + if (this->x == other.x && this->y == other.y) + return 1; + + return 0; +} + +bool point::operator!=(const point& other) +{ + return !(this == &other); +} + +bool point::operator<(const point& other) +{ + if (this->x < other.x) + return true; + else if (this->y < other.y) + return true; + + return false; +} \ No newline at end of file diff --git a/exercise/03/point.h b/exercise/03/point.h index e69de29..2aceee4 100644 --- a/exercise/03/point.h +++ b/exercise/03/point.h @@ -0,0 +1,11 @@ +class point +{ + 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..bab580b 100644 --- a/exercise/03/test.cpp +++ b/exercise/03/test.cpp @@ -9,30 +9,28 @@ 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)); - */ + } };