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

namespace
{
double value_cutoff(double value)
{
return value > 0 ? std::min(value, 1.) : std::max(value, 0.);
}

long scale_color(double value)
{
return 255 * value;
}
}

color::color() : color{0, 0, 0}
{
}

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

void color::set_red(double red)
{
red_ = value_cutoff(red);
}

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

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

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

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

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

unsigned long color::get_color_ref() const
{
return scale_color(red_) + scale_color(green_) * 256 + scale_color(blue_) * 65536;
}

double color::get_luminance() const
{
return 0.2126 * red_ + 0.7152 * green_ + 0.0722 * blue_;
}

bool operator==(const color& lhs, const color& rhs)
{
return lhs.red_ == rhs.red_ && lhs.green_ == rhs.green_ && lhs.blue_ == rhs.blue_;
}
28 changes: 28 additions & 0 deletions exercise/01/color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

class color
{
private:
double red_;
double green_;
double blue_;

public:
color();
color(double red, double green, double blue);

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

unsigned long get_color_ref() const;
double get_luminance() const;

friend bool operator==(const color&, const color&);
};

bool operator==(const color& lhs, const color& rhs);
7 changes: 7 additions & 0 deletions exercise/01/color_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ namespace color_test

TEST_METHOD(test_conversion_colorref)
{
auto t1 = RGB(255, 0, 0);
auto t2 = RGB(0, 255, 0);
auto t3 = RGB(0, 0, 255);
auto t4 = RGB(255, 255, 255);
auto t5 = RGB(127, 127, 127);
long t6 = 255 * 0.5;

Assert::AreEqual(RGB(255, 0, 0), color( 1, 0, 0 ).get_color_ref());
Assert::AreEqual(RGB( 0, 255, 0), color( 0, 1, 0 ).get_color_ref());
Assert::AreEqual(RGB( 0, 0, 255), color( 0, 0, 1 ).get_color_ref());
Expand Down
31 changes: 31 additions & 0 deletions exercise/01/color_test.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2050
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "color_test", "color_test.vcxproj", "{F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x64.ActiveCfg = Debug|x64
{F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x64.Build.0 = Debug|x64
{F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x86.ActiveCfg = Debug|Win32
{F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Debug|x86.Build.0 = Debug|Win32
{F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x64.ActiveCfg = Release|x64
{F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x64.Build.0 = Release|x64
{F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x86.ActiveCfg = Release|Win32
{F6408CAA-4C86-4D98-8D13-9AF4DF77D23B}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {770F20AC-2CA1-418A-9FEF-E30F914E55E6}
EndGlobalSection
EndGlobal
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 <algorithm>
66 changes: 66 additions & 0 deletions exercise/02/array.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,67 @@
#include "array.h"
#include <stdexcept>
#include <algorithm>

array::array() : p_{nullptr}, n_{0}
{
}

array::array(int size, double value) : p_{new double[size]}, n_{size}
{
std::fill_n(p_, size, value);
}

array::array(const array& other) : array(other.n_, other.p_ != nullptr ? other.at(0) : 0)
{
}

array::array(array&& other) noexcept : p_{other.p_}, n_{other.n_}
{
other.p_ = nullptr;
other.n_ = 0;
}

array::~array()
{
delete[] p_;
p_ = nullptr;
}

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

double array::at(int index) const
{
if (index < 0 || index >= n_) throw std::out_of_range("Index is out of range");
return p_[index];
}

array& array::operator=(const array& other)
{
if (this != &other)
{
delete[] p_;
n_ = other.n_;
p_ = new double[n_];

std::fill_n(p_, n_, other.at(0));
}

return *this;
}

array& array::operator=(array&& other) noexcept
{
if (this != &other)
{
delete[] p_;
p_ = other.p_;
n_ = other.n_;

other.p_ = nullptr;
other.n_ = 0;
}
return *this;
}
19 changes: 17 additions & 2 deletions exercise/02/array.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
#pragma once

// do not use standard container as member or base class
class array
{
double* p;
int n;
private:
double* p_;
int n_;

public:
array();
array(int size, double value);
array(const array& other);
array(array&& other) noexcept;
~array();

int size() const;
double at(int index) const;

array& operator=(const array& other);
array& operator=(array&& other) noexcept;
};
25 changes: 25 additions & 0 deletions exercise/02/array.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2050
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "array", "array.vcxproj", "{BB15329E-10D8-4740-94BC-64C50BA4EE12}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BB15329E-10D8-4740-94BC-64C50BA4EE12}.Debug|x86.ActiveCfg = Debug|Win32
{BB15329E-10D8-4740-94BC-64C50BA4EE12}.Debug|x86.Build.0 = Debug|Win32
{BB15329E-10D8-4740-94BC-64C50BA4EE12}.Release|x86.ActiveCfg = Release|Win32
{BB15329E-10D8-4740-94BC-64C50BA4EE12}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8FF17F18-E60F-4D76-B3B6-97439B571F92}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions exercise/02/array.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<AdditionalIncludeDirectories>$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<UseFullPaths>true</UseFullPaths>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
Loading