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

color::color()
{
red_ = 0.;
blue_ = 0.;
green_ = 0.;
}


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

color::color(const color& color)
{
red_ = color.red_;
blue_ = color.blue_;
green_ = color.green_;
}

double color::get_red() const
{
return std::clamp(red_, 0.0, 1.0);
}

double color::get_green() const
{
return std::clamp(green_, 0.0, 1.0);
}

double color::get_blue() const
{
return std::clamp(blue_, 0.0, 1.0);
}

void color::set_red(double red)
{
red_ = red;
}
void color::set_blue(double blue)
{
blue_ = blue;
}
void color::set_green(double green)
{
green_ = green;
}

COLORREF color::get_color_ref()
{
return RGB(red_ * 255, green_ * 255, blue_ * 255);
}
double color::get_luminance()
{
return 0.2126 * red_ + 0.7152 * green_ + 0.0722* blue_;
}

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




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


class color
{
double red_, blue_, green_ ;
public:
color();
color(double red, double blue, double green);
color(const color& color);
double get_red() const;
double get_green() const;
double get_blue() const;
void set_red(double red);
void set_blue(double blue);
void set_green(double green);
COLORREF get_color_ref();
bool operator==(const color& other) const;
double get_luminance();
};
2 changes: 2 additions & 0 deletions exercise/01/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

#include <SDKDDKVer.h>
#include "CppUnitTest.h"
#include <Windows.h>
#include <algorithm>
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>

array::array(int size, double value) : n_(size), value_(value)
{
p = new double[n_];
for (int i=0; i<n_; i++)
{
*(p + i) = value;
}
}
array::array(const array& secondArray)
{
p = new double[secondArray.n_];
n_ = secondArray.n_;
value_ = secondArray.value_;
for (int i = 0; i < secondArray.n_; i++)
{
*(p + i) = secondArray.value_;
}
}
array::array() : n_(0), value_(0)
{
p = new double[n_];
}

array::~array()
{
delete [] p;
}
int array::size()
{
return n_;
}
double& array::at(int i) const
{
if (i < 0 || i >= n_)
throw std::out_of_range("invalid index");
return p[i];
}
array& array::operator=(const array& secondArray)
{
n_ = secondArray.n_;
value_ = secondArray.value_;
p = new double[n_];
for (int i = 0; i < secondArray.n_; i++)
{
*(p + i) = secondArray.value_;
}
return *this;
}

array::array(array&& second) noexcept
{
p = second.p;
n_ = second.n_;
second.p = nullptr;
second.n_ = 0;
}


15 changes: 13 additions & 2 deletions exercise/02/array.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
// do not use standard container as member or base class
#pragma once

class array
{
double* p;
int n;
double* p, value_;
int n_;

public:
array();
array(int size, double value);
array(const array& secondArray);
array(array && second) noexcept;
~array();
int size();
double& at(int i) const;
array& operator=(const array& secondArray);
};
16 changes: 2 additions & 14 deletions exercise/02/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,61 @@ 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));
*/
}

};
25 changes: 25 additions & 0 deletions exercise/03/point.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
#include "point.h"
point::point(int x, int y)
{
x_ = x;
y_ = y;
}


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

bool point::operator!=(const point& p)
{
return x_ != p.x_ || y_ != p.y_;
}
bool point::operator < (const point &p)
{
if (x_ < p.x_) return true;
if(p.x_==x_)
{
return y_ < p.y_;
}
return false;
}

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
{
private:
int x_, y_;
public:
point(int x = 0, int y = 0);
bool operator==(const point &p);
bool operator!=(const point &p);
bool operator < (const point &p);
};
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));
*/

}
};