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

namespace {
// Convert to [0, 1.0] range
double to_correct_value(const double input) {
if (input > 1.0) return 1.0;
if (input < 0.0) return 0.0;
return input;
}
}

color::color(double red, double green, double blue) {
red_ = to_correct_value(red);
green_ = to_correct_value(green);
blue_ = to_correct_value(blue);
}

bool color::operator==(const color& other) const {
return red_ == other.red_ && green_ == other.green_ && blue_ == other.blue_;
}

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 red) {
red_ = to_correct_value(red);
}

void color::set_green(const double green) {
green_ = to_correct_value(green);
}

void color::set_blue(const double blue) {
blue_ = to_correct_value(blue);
}

COLORREF color::get_color_ref() const {
return RGB(static_cast<BYTE>(255 * red_), static_cast<BYTE>(255 * green_), static_cast<BYTE>(255 * blue_));
}

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

class color {
public:
color(double red = 0.0, double green = 0.0, double blue = 0.0);
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_;
};
348 changes: 174 additions & 174 deletions exercise/01/color_test.vcxproj

Large diffs are not rendered by default.

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

namespace {
void populate_array(double* array, int size, double value) {
for (int index = 0; index < size; index++)
array[index] = value;
}

void copy_array(double* source, double* target, int size) {
for (int index = 0; index < size; index++)
target[index] = source[index];
}
}

array::array() {
n = 0;
p = nullptr;
}

array::array(int size, double value) {
n = size;
p = new double[size];
populate_array(p, n, value);
}

// Needed since we have to copy internal array
array::array(const array& other) {
n = other.n;
p = new double[n];
copy_array(other.p, p, n);
}

array::array(array&& other) {
n = other.n;
p = other.p;
other.p = nullptr;
other.n = 0;
}

array::~array() {
delete [] p;
}

array& array::operator=(const array& other) {
delete[] p;
n = other.n;
p = new double[n];
copy_array(other.p, p, n);
return *this;
}

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

double array::at(int index) const {
if (index < 0 || index >= n)
throw std::out_of_range("Invalid index");
return p[index];
}
10 changes: 9 additions & 1 deletion 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(int size, double value);
array(const array& other);
array(array&& other);
~array();
array& operator=(const array& other);
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));
*/
}

};
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): x_(x), y_(y)
{
}

bool point::operator==(const point& other)
{
return x_ == other.x_ && y_ == other.y_;
}

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

bool point::operator<(const point& other)
{
if (other.x_ != x_)
return _x < other.x_;
return y_ < other.y_;
}
12 changes: 12 additions & 0 deletions exercise/03/point.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class point
{
int _x;
int _y;
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);
};
10 changes: 1 addition & 9 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));
*/
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));
*/
}
};