diff --git a/exercise/01/animal.cpp b/exercise/01/animal.cpp new file mode 100644 index 0000000..8d576da --- /dev/null +++ b/exercise/01/animal.cpp @@ -0,0 +1,51 @@ +#include "animal.h" + +std::unique_ptr animal_factory(const anuanimal_enum id) +{ + switch (id) + { + case anuanimal_enum::cockroach_id: return std::make_unique(); + case anuanimal_enum::sparrow_id: return std::make_unique(); + case anuanimal_enum::tarantula_id: return std::make_unique(); + } + return {}; +} + +animal_base::animal_base(unsigned int legs, const std::wstring& species) : + legs_(legs), + species_(species) +{ +} + +std::wstring animal_base::species() const +{ + return species_; +} + +unsigned animal_base::legs() const +{ + return legs_; +} + +spider::spider(const std::wstring& species) : + animal_base(8, species) +{} + +insect::insect(const std::wstring& species) : + animal_base(6, species) +{} + +bird::bird(const std::wstring& species) : + animal_base(2, species) +{} + +tarantula::tarantula() : + spider(L"tarantula") +{} + +cockroach::cockroach() : + insect(L"cockroach") +{} + +sparrow::sparrow() : bird(L"sparrow") +{} diff --git a/exercise/01/animal.h b/exercise/01/animal.h new file mode 100644 index 0000000..b62504b --- /dev/null +++ b/exercise/01/animal.h @@ -0,0 +1,72 @@ +#pragma once +#include +#include + +enum class anuanimal_enum +{ + cockroach_id = 1, + sparrow_id = 2, + tarantula_id = 3 +}; + +class animal +{ +public: + virtual std::wstring species() const = 0; + virtual unsigned int legs() const = 0; + virtual ~animal() = default; +}; + +std::unique_ptr animal_factory(anuanimal_enum id); + +class animal_base : public animal +{ +public: + animal_base(unsigned int legs, const std::wstring& species); + + std::wstring species() const override; + unsigned int legs() const override; + +private: + unsigned int legs_; + std::wstring species_; +}; + +class spider : public animal_base +{ +public: + spider(const std::wstring& species); +}; + +class insect : public animal_base +{ +public: + insect(const std::wstring& species); +}; + +class bird : public animal_base +{ +public: + bird(const std::wstring& species); +}; + +class tarantula : public spider +{ +public: + static const anuanimal_enum id = anuanimal_enum::tarantula_id; + tarantula(); +}; + +class cockroach : public insect +{ +public: + static const anuanimal_enum id = anuanimal_enum::cockroach_id; + cockroach(); +}; + +class sparrow : public bird +{ +public: + static const anuanimal_enum id = anuanimal_enum::sparrow_id; + sparrow(); +}; \ No newline at end of file diff --git a/exercise/01/animals.sln b/exercise/01/animals.sln new file mode 100644 index 0000000..4e0307e --- /dev/null +++ b/exercise/01/animals.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2036 +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 = {B60A737A-D29C-4A65-8FE9-3B9B772F7ED6} + EndGlobalSection +EndGlobal diff --git a/exercise/01/animals.vcxproj b/exercise/01/animals.vcxproj index 0c14cbf..47ff178 100644 --- a/exercise/01/animals.vcxproj +++ b/exercise/01/animals.vcxproj @@ -1,95 +1,97 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - - {BB15329E-10D8-4740-94BC-64C50BA4EE12} - Win32Proj - array - 10.0.17134.0 - - - - DynamicLibrary - true - v141 - Unicode - false - - - DynamicLibrary - false - v141 - true - Unicode - false - - - - - - - - - - - - - true - - - true - - - - NotUsing - Level3 - Disabled - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;_DEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - Level3 - NotUsing - MaxSpeed - true - true - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;NDEBUG;%(PreprocessorDefinitions) - true - - - Windows - true - true - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + + {BB15329E-10D8-4740-94BC-64C50BA4EE12} + Win32Proj + array + 10.0.17134.0 + + + + DynamicLibrary + true + v141 + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + + + + + + + + + + + true + + + true + + + + NotUsing + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + + + Windows + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + NotUsing + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + + + + + + + + + + \ No newline at end of file diff --git a/exercise/01/animals.vcxproj.filters b/exercise/01/animals.vcxproj.filters index ad4d9ab..232a3ad 100644 --- a/exercise/01/animals.vcxproj.filters +++ b/exercise/01/animals.vcxproj.filters @@ -1,24 +1,30 @@ - - - - - {f2ada0ee-91e7-4bbd-b33d-837567340243} - - - {3cc0fd48-6585-4969-94bd-406daa5f786b} - - - - - test - - - src - - - - - src - - + + + + + {f2ada0ee-91e7-4bbd-b33d-837567340243} + + + {3cc0fd48-6585-4969-94bd-406daa5f786b} + + + + + test + + + src + + + src + + + + + src + + + src + + \ No newline at end of file diff --git a/exercise/01/leg_counter.cpp b/exercise/01/leg_counter.cpp new file mode 100644 index 0000000..0ac4e7f --- /dev/null +++ b/exercise/01/leg_counter.cpp @@ -0,0 +1,14 @@ +#include "leg_counter.h" + +std::wstring leg_counter::add_animal(const std::unique_ptr& animal_ptr) +{ + if (animal_ptr == nullptr) + return L""; + sum_ += animal_ptr->legs(); + return animal_ptr->species(); +} + +unsigned leg_counter::legs() const +{ + return sum_; +} diff --git a/exercise/01/leg_counter.h b/exercise/01/leg_counter.h new file mode 100644 index 0000000..b009d45 --- /dev/null +++ b/exercise/01/leg_counter.h @@ -0,0 +1,14 @@ +#pragma once +#include "animal.h" +#include + +class leg_counter final +{ +public: + leg_counter() : sum_(0) {} + std::wstring add_animal(const std::unique_ptr&); + unsigned int legs() const; + +private: + int sum_; +}; diff --git a/exercise/01/test.cpp b/exercise/01/test.cpp index f7f2c1f..d1693a9 100644 --- a/exercise/01/test.cpp +++ b/exercise/01/test.cpp @@ -1,4 +1,5 @@ #include "CppUnitTest.h" +#include "leg_counter.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; #include "animal.h" @@ -35,17 +36,17 @@ TEST_CLASS(test_animal_hierarchy) 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(L"cockroach", lc.add_animal(animal_factory(anuanimal_enum::cockroach_id)).c_str()); + Assert::AreEqual(L"sparrow", lc.add_animal(animal_factory(anuanimal_enum::sparrow_id)).c_str()); + Assert::AreEqual(L"tarantula", lc.add_animal(animal_factory(anuanimal_enum::tarantula_id)).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)); + lc.add_animal(animal_factory(anuanimal_enum::sparrow_id)); + lc.add_animal(animal_factory(anuanimal_enum::sparrow_id)); Assert::AreEqual(4u, lc.legs()); } - };