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
85 changes: 85 additions & 0 deletions exercise/01/color.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
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

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);

};
1 change: 0 additions & 1 deletion exercise/01/color_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "stdafx.h"
#include "color.h"
#include <windows.h> // RGB

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

Expand Down
1 change: 1 addition & 0 deletions exercise/01/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

#include <SDKDDKVer.h>
#include "CppUnitTest.h"
#include <windows.h>
87 changes: 87 additions & 0 deletions exercise/02/array.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,88 @@
#include "array.h"
#include <stdexcept>

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);
}
12 changes: 12 additions & 0 deletions exercise/02/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
23 changes: 9 additions & 14 deletions exercise/02/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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));
*/

}

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

}
};