Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions exercise/01/color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "stdafx.h"
#include "color.h"
#include <algorithm>

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<BYTE> (red * 255),
static_cast<BYTE> (green * 255),
static_cast<BYTE> (blue * 255));
}

double color::get_luminance() const
{
return 0.2126 * red + 0.7152 * green + 0.0722* blue;
}
26 changes: 26 additions & 0 deletions exercise/01/color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <windows.h>

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;
};
81 changes: 81 additions & 0 deletions exercise/02/array.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,82 @@
#include "array.h"
#include <exception>
#include <stdexcept>

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;
}
17 changes: 15 additions & 2 deletions exercise/02/array.h
Original file line number Diff line number Diff line change
@@ -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;
};
28 changes: 14 additions & 14 deletions exercise/02/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<a.size(); ++i)
Assert::AreEqual(value, a.at(i));
*/

}

TEST_METHOD(array_index_invalid_large)
{
/*

array a(size, value);
auto func = [a]() { a.at(100); };
Assert::ExpectException<std::out_of_range>(func);
*/

}

TEST_METHOD(array_index_invalid_negative)
{
/*

array a(size, value);
auto func = [a]() { a.at(-2); };
Assert::ExpectException<std::out_of_range>(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));
*/

}

};
19 changes: 19 additions & 0 deletions exercise/03/point.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
15 changes: 15 additions & 0 deletions exercise/03/point.h
Original file line number Diff line number Diff line change
@@ -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;
};
16 changes: 8 additions & 8 deletions exercise/03/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
*/

}
};