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
74 changes: 74 additions & 0 deletions exercise/01/color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "stdafx.h"
#include "color.h"

color::color(double red, double green, double blue)
{
set_red(red);
set_green(green);
set_blue(blue);
}

color & color::operator=(const color & other)
{
if (this != &other) {
red = other.red;
green = other.green;
blue = other.blue;
}
return *this;
}

bool color::operator==(const color & other) const
{
return this->red == other.red && this->green == other.green && this->blue == other.blue;
}

double color::get_red() const
{
return red;
}

double color::get_green() const
{
return green;
}

double color::get_blue() const
{
return blue;
}

namespace {

double convert(double col)
{
if (col < 0) return 0;
if (col > 1) return 1;
return col;
}
}

void color::set_red(double red)
{
this->red = convert(red);
}

void color::set_green(double green)
{
this->green = convert(green);
}

void color::set_blue(double blue)
{
this->blue = convert(blue);
}

COLORREF color::get_color_ref() const
{
return RGB(red * maxColorRange, green * maxColorRange, blue * maxColorRange);
}

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

class color {
public:
color() : red{0}, green{0}, blue{0} {};
color(double red, double green, double blue);
color& operator=(const color& other);
bool operator==(const color& other) 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() const;
double get_luminance() const;

private:
double red;
double green;
double blue;
const int maxColorRange = 255;
const double redIntensity = 0.2126;
const double greenIntensity = 0.7152;
const double blueIntensity = 0.0722;
};
53 changes: 53 additions & 0 deletions exercise/02/array.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
#include "array.h"
#include <algorithm>
#include <stdexcept>

array::array(int size, double value)
{
if (size < 0) throw std::out_of_range("Size is too small!");
n = size;
p = new double[size];
std::fill(p, p + size, value);
}

array::array(const array & other)
{
n = other.n;
p = new double[n];
std::copy(other.p, other.p + n, p);
}

array::array(array && other) noexcept : n(std::exchange(other.n, 0)), p(std::exchange(other.p, nullptr)) {}

array & array::operator=(array other)
{
std::swap(n, other.n);
std::swap(p, other.p);
return *this;
}

array & array::operator=(array && other)
{
n = std::move(other.n);
p = std::move(other.p);
return *this;
}

array::~array()
{
if (p == nullptr) return;
delete[] p;
p = nullptr;
n = 0;

}

int array::size() const
{
return n;
}

double array::at(int index) const
{
if (index > n || index < 0) throw std::out_of_range("Index out of range!");
return *(p + index);
}
10 changes: 10 additions & 0 deletions exercise/02/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ class array
double* p;
int n;
public:
array() : n(0), p(nullptr) {};
array(int size, double value);
array(const array& other);
array(array&& other) noexcept;
array& operator=(array other);
array& operator=(array&& other);
~array();

int size() const;
double at(int index) const;
};
14 changes: 0 additions & 14 deletions exercise/02/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,59 @@ 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));
*/
}

};
14 changes: 14 additions & 0 deletions exercise/03/point.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
#include "point.h"

bool point::operator==(const point & other) const
{
return x == other.x && y == other.y;
}

bool point::operator!=(const point & other) const
{
return !((*this)==other);
}

bool point::operator<(const point & other) const
{
return x <= other.x && y < other.y || x < other.x && y <= other.y;
}
19 changes: 19 additions & 0 deletions exercise/03/point.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

class point
{
public:
point() : x(0), y(0) {};
point(int x, int y) : x(x), y(y) {};
point(const point& other) = default;
point(point&& other) = default;
point& operator=(const point& other) = default;
point& operator=(point&& other) = default;
~point() = default;
bool operator==(const point& other) const;
bool operator!=(const point& other) const;
bool operator<(const point& other) const;

private:
int x, y;
};
8 changes: 0 additions & 8 deletions exercise/03/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
*/
}
};