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

animal::~animal() = default;

namespace species
{
enum { cockroach = 1, sparrow, tarantula };
}

unsigned insect::legs() const noexcept
{
return 6;
}

unsigned bird::legs() const noexcept
{
return 2;
}

unsigned spider::legs() const noexcept
{
return 8;
}

std::wstring cockroach::species() const noexcept
{
return L"cockroach";
}

std::wstring sparrow::species() const noexcept
{
return L"sparrow";
}

std::wstring tarantula::species() const noexcept
{
return L"tarantula";
}

std::wstring leg_counter::add_animal(std::unique_ptr<animal> animal)
{
std::wstring species{animal->species()};
animals_.push_back(std::move(animal));
return species;
}

unsigned leg_counter::legs() const
{
return std::accumulate(animals_.cbegin(), animals_.cend(), 0, [](int acc, auto& animal) { return acc + animal->legs(); });
}

std::unique_ptr<animal> animal_factory(const int animal)
{
switch (animal)
{
case species::cockroach: return std::make_unique<cockroach>(cockroach{});
case species::sparrow: return std::make_unique<sparrow>(sparrow{});
case species::tarantula: return std::make_unique<tarantula>(tarantula{});
default: throw std::invalid_argument{"Unknown animal species"};
}
}
55 changes: 55 additions & 0 deletions exercise/01/animal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once
#include <string>
#include <vector>

class animal
{
public:
virtual std::wstring species() const noexcept = 0;
virtual unsigned int legs() const noexcept = 0;
virtual ~animal() = 0;
};

class insect : public animal
{
unsigned legs() const noexcept override;
};

class bird : public animal
{
unsigned legs() const noexcept override;
};

class spider : public animal
{
unsigned legs() const noexcept override;
};

class cockroach : public insect
{
std::wstring species() const noexcept override;
};


class sparrow : public bird
{
std::wstring species() const noexcept override;
};


class tarantula : public spider
{
std::wstring species() const noexcept override;
};


class leg_counter
{
private:
std::vector<std::unique_ptr<animal>> animals_;
public:
std::wstring add_animal(std::unique_ptr<animal> animal);
unsigned legs() const;
};

std::unique_ptr<animal> animal_factory(int animal);
25 changes: 25 additions & 0 deletions exercise/01/animals.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}") = "animals", "animals.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 = {DBE656B2-233A-4E04-B832-CD3736F5233D}
EndGlobalSection
EndGlobal
6 changes: 3 additions & 3 deletions exercise/01/animals.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="animal.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="animal.cpp" />
<ClCompile Include="test.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="animal.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
4 changes: 4 additions & 0 deletions exercise/01/animals.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
79 changes: 40 additions & 39 deletions exercise/01/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,47 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework;
TEST_CLASS(test_animal_hierarchy)
{
public:
TEST_METHOD(concrete_insect)
{
cockroach c;
insect& i = c;
animal& a = c;
Assert::AreEqual(L"cockroach", a.species().c_str());
Assert::AreEqual(6u, a.legs());
}
TEST_METHOD(concrete_bird)
{
sparrow s;
bird& b = s;
animal& a = s;
Assert::AreEqual(L"sparrow", a.species().c_str());
Assert::AreEqual(2u, a.legs());
}
TEST_METHOD(concrete_insect)
{
cockroach c;
insect& i = c;
animal& a = c;
Assert::AreEqual(L"cockroach", a.species().c_str());
Assert::AreEqual(6u, a.legs());
}

TEST_METHOD(concrete_spider)
{
tarantula t;
spider& s = t;
animal& a = t;
Assert::AreEqual(L"tarantula", a.species().c_str());
Assert::AreEqual(8u, a.legs());
}
TEST_METHOD(concrete_bird)
{
sparrow s;
bird& b = s;
animal& a = s;
Assert::AreEqual(L"sparrow", a.species().c_str());
Assert::AreEqual(2u, a.legs());
}

TEST_METHOD(legg_counter_different_animals)
{
leg_counter lc;
Assert::AreEqual(L"cockroach", lc.add_animal(animal_factory(1)).c_str());
Assert::AreEqual(L"sparrow", lc.add_animal(animal_factory(2)).c_str());
Assert::AreEqual(L"tarantula", lc.add_animal(animal_factory(3)).c_str());
Assert::AreEqual(16u, lc.legs());
}
TEST_METHOD(legg_counter_same_animal)
{
leg_counter lc;
lc.add_animal(animal_factory(2));
lc.add_animal(animal_factory(2));
Assert::AreEqual(4u, lc.legs());
}
TEST_METHOD(concrete_spider)
{
tarantula t;
spider& s = t;
animal& a = t;
Assert::AreEqual(L"tarantula", a.species().c_str());
Assert::AreEqual(8u, a.legs());
}

TEST_METHOD(legg_counter_different_animals)
{
leg_counter lc;
Assert::AreEqual(L"cockroach", lc.add_animal(animal_factory(1)).c_str());
Assert::AreEqual(L"sparrow", lc.add_animal(animal_factory(2)).c_str());
Assert::AreEqual(L"tarantula", lc.add_animal(animal_factory(3)).c_str());
Assert::AreEqual(16u, lc.legs());
}

TEST_METHOD(legg_counter_same_animal)
{
leg_counter lc;
lc.add_animal(animal_factory(2));
lc.add_animal(animal_factory(2));
Assert::AreEqual(4u, lc.legs());
}
};