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

const double color::max_color_value = 1.;
const double color::min_color_value = 0.;

color::color()
{
set_red(0.);
set_green(0.);
set_blue(0.);
}

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

bool color::operator==(const color& colorCompare) const
{
return m_red == colorCompare.get_red() && m_green == colorCompare.get_green() && m_blue == colorCompare.get_blue();
}

void color::set_red(double red)
{
m_red = std::clamp(red, min_color_value, max_color_value);
}

void color::set_green(double input)
{
m_green = std::clamp(input, min_color_value, max_color_value);
}

void color::set_blue(double input)
{
m_blue = std::clamp(input, min_color_value, max_color_value);
}

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

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

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

COLORREF color::get_color_ref()
{
return RGB((int)(m_red*255),(int)(m_green*255), (int)(m_blue*255));
}

double color::get_luminance()
{
return 0.2126 * m_red + 0.7152 * m_green + 0.0722 * m_blue;
}
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();
color(double, double, double);

bool operator==(const color & color) const;

double get_red() const;
double get_green() const;
double get_blue() const;

void set_red(double);
void set_green(double);
void set_blue(double);

COLORREF get_color_ref();
double get_luminance();

private:
double m_red, m_green, m_blue;

static const double color::max_color_value;
static const double color::min_color_value;
};
71 changes: 71 additions & 0 deletions exercise/02/array.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,72 @@
#include "array.h"
#include <stdexcept>

array::array() :
n(0),
p(nullptr)
{
}

array::array(array&& array) :
p(nullptr), n(0)
{
std::swap(p, array.p);
std::swap(n, array.n);
}

array::array(const array &other) :
n(0),
p(nullptr)
{
if (other.n > 0)
{
n = other.n;
p = new double[n];
for (int i = 0; i < other.n; ++i)
{
p[i] = other.p[i];
}
}
}

array::array(const int size, const double value) :
n(0),
p(nullptr)
{
if (size > 0)
{
n = size;
p = new double[n];
for (int i = 0; i < size; ++i)
{
p[i] = value;
}
}
}

array array::operator=(array &copyArray)
{
n = copyArray.n;
p = copyArray.p;
copyArray.n = 0;
copyArray.p = nullptr;

return *this;
}

double array::at(const int index) const
{
if (index >= 0 && index < n)
{
return p[index];
}
else
{
throw std::out_of_range("Index is out of range.");
}
}

int array::size()
{
return n;
}
8 changes: 8 additions & 0 deletions exercise/02/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ class array
double* p;
int n;
public:
array();
array(const int size, const double value = 0);
array(const array &array);
array(array&&);
array operator=(array&);

double at(const int index) const;
int size();
};
16 changes: 1 addition & 15 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);
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));
*/
}

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

point::point(int x, int y)
{
m_x = x;
m_y = y;
}

bool point::operator==(point other)
{
return (m_x == other.m_x) && (m_y == other.m_y);
}

bool point::operator!=(point other)
{
return (m_x != other.m_x) || (m_y != other.m_y);
}

bool point::operator<(point other)
{
return (m_x < other.m_x) || (m_y < other.m_y);
}
10 changes: 10 additions & 0 deletions exercise/03/point.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class point {
public:
point(int x = 0, int y = 0);
bool operator==(point other);
bool operator!=(point other);
bool operator<(point other);
private:
int m_x;
int m_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));
*/
}
};