diff --git a/FinalExercise/FinalExercise.sln b/FinalExercise/FinalExercise.sln new file mode 100644 index 0000000..96918f6 --- /dev/null +++ b/FinalExercise/FinalExercise.sln @@ -0,0 +1,41 @@ + +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}") = "FinalExercise", "FinalExercise\FinalExercise.vcxproj", "{49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FinalExerciseTests", "FinalExerciseTests\FinalExerciseTests.vcxproj", "{D57382B1-FE9F-45E6-92E9-926D43932666}" +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 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Debug|x64.ActiveCfg = Debug|x64 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Debug|x64.Build.0 = Debug|x64 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Debug|x86.ActiveCfg = Debug|Win32 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Debug|x86.Build.0 = Debug|Win32 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Release|x64.ActiveCfg = Release|x64 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Release|x64.Build.0 = Release|x64 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Release|x86.ActiveCfg = Release|Win32 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E}.Release|x86.Build.0 = Release|Win32 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Debug|x64.ActiveCfg = Debug|x64 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Debug|x64.Build.0 = Debug|x64 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Debug|x86.ActiveCfg = Debug|Win32 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Debug|x86.Build.0 = Debug|Win32 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Release|x64.ActiveCfg = Release|x64 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Release|x64.Build.0 = Release|x64 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Release|x86.ActiveCfg = Release|Win32 + {D57382B1-FE9F-45E6-92E9-926D43932666}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FAA11F85-3C1D-4CC6-BF9D-8D356B566A41} + EndGlobalSection +EndGlobal diff --git a/FinalExercise/FinalExercise/FinalExercise.cpp b/FinalExercise/FinalExercise/FinalExercise.cpp new file mode 100644 index 0000000..cd5191f Binary files /dev/null and b/FinalExercise/FinalExercise/FinalExercise.cpp differ diff --git a/FinalExercise/FinalExercise/FinalExercise.vcxproj b/FinalExercise/FinalExercise/FinalExercise.vcxproj new file mode 100644 index 0000000..2920baa --- /dev/null +++ b/FinalExercise/FinalExercise/FinalExercise.vcxproj @@ -0,0 +1,170 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {49A8615B-EDC6-46F3-A8A2-F2D66BD85F9E} + Win32Proj + FinalExercise + 10.0.17134.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Use + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + + + + + Use + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + + + + + Use + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + + + Create + Create + Create + Create + + + + + + + + + + \ No newline at end of file diff --git a/FinalExercise/FinalExercise/FinalExercise.vcxproj.filters b/FinalExercise/FinalExercise/FinalExercise.vcxproj.filters new file mode 100644 index 0000000..8eac141 --- /dev/null +++ b/FinalExercise/FinalExercise/FinalExercise.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/FinalExercise/FinalExercise/mathblocks.cpp b/FinalExercise/FinalExercise/mathblocks.cpp new file mode 100644 index 0000000..fe2195b --- /dev/null +++ b/FinalExercise/FinalExercise/mathblocks.cpp @@ -0,0 +1,359 @@ +#include "pch.h" +#include "mathblocks.h" +#include +#include +#include +#include + +const std::string identity_block::block_name = "Identity"; +const std::string addition_block::block_name = "Addition"; +const std::string multiplication_block::block_name = "Multiplication"; +const std::string power_block::block_name = "Power"; +const std::string limit_block::block_name = "Limit"; +const std::string condition_block::block_name = "Condition"; + +template +std::shared_ptr block_factory::create_block(std::vector& evaluationParameters) +{ + if (evaluationParameters.size() == 0) + { + return std::make_shared(BlockType()); + } + + return std::make_shared(BlockType(evaluationParameters)); +} + +void block_saver::save_blocks_to_file(const std::string& filePath, mathblocks& mathblocks) +{ + std::ofstream file; + file.open(filePath); + + if (file.is_open()) + { + auto sequence = mathblocks.get_sequence(); + for (auto& block : sequence) + { + file << block->get_block_name() << "="; + auto evaluation_constants = block->get_evaluation_constants(); + for (size_t i = 0; i < evaluation_constants.size(); ++i) + { + if (i == 0) + { + file << evaluation_constants[i]; + } + else + { + file << "," << evaluation_constants[i]; + } + } + file << std::endl; + } + + file.close(); + } +} + +void block_saver::load_blocks_from_file(const std::string& filePath, mathblocks& mathblocks) +{ + std::ifstream file; + file.open(filePath); + + if (file.is_open()) + { + std::string line; + + while (std::getline(file, line)) + { + //Extract Block Name + const std::size_t nameDelimiterIndex = line.find_first_of("="); + std::string blockName = line.substr(0, nameDelimiterIndex); + + //Extract Block Evaluation Parameters + std::string evaluationParametersStr = line.substr(nameDelimiterIndex + 1, line.length()); + std::vector evaluationParameters; + + if (!evaluationParametersStr.empty()) + { + std::size_t lastDelimiterIndex = 0; + std::size_t delimiterIndex; + while ((delimiterIndex = evaluationParametersStr.find_first_of(",", lastDelimiterIndex)) != std::string::npos) + { + double parameter = std::stod(evaluationParametersStr.substr(lastDelimiterIndex, delimiterIndex)); + evaluationParameters.push_back(parameter); + lastDelimiterIndex = delimiterIndex; + } + double parameter = std::stod(evaluationParametersStr.substr(lastDelimiterIndex, evaluationParametersStr.length())); + evaluationParameters.push_back(parameter); + } + + if (blockName._Equal(identity_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + else if (blockName._Equal(addition_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + else if (blockName._Equal(multiplication_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + else if (blockName._Equal(power_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + else if (blockName._Equal(limit_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + else if (blockName._Equal(condition_block::block_name)) + { + mathblocks.add_block(block_factory::create_block(evaluationParameters)); + } + } + + file.close(); + } +} + +void mathblocks::add_block(std::shared_ptr block) +{ + sequence.push_back(block); +} + +void mathblocks::delete_block(int blockIndex) +{ + try + { + if (sequence.at(blockIndex)) + { + sequence.erase(sequence.begin() + blockIndex); + } + } + catch (const std::exception& e) + { + std::cout << "There are no blocks at given index"; + } +} + +void mathblocks::move_block_to_beginning(int blockIndex) +{ + try + { + if (sequence.at(blockIndex)) + { + std::rotate(sequence.begin(), sequence.begin() + blockIndex, sequence.begin() + blockIndex + 1); + } + } + catch (const std::exception& e) + { + std::cout << "There are no blocks at given index"; + } +} + +void mathblocks::print_sequence() +{ + if (sequence.size() == 0) + { + std::cout << "No blocks in the sequence" << std::endl; + return; + } + + for (auto& block : sequence) + { + std::cout << block->get_block_name() << "("; + std::vector evaluation_constants = block->get_evaluation_constants(); + + for(size_t i = 0; i < evaluation_constants.size(); ++i) + { + if (i == 0) + { + std::cout << evaluation_constants[i]; + } + else + { + std::cout << "," << evaluation_constants[i]; + } + } + + std::cout << ")" << std::endl; + } + + std::cout << std::endl; +} + +double mathblocks::run_sequence(double value) +{ + double lastValue = value; + + for (auto& block : sequence) + { + lastValue = block->evaluate(lastValue); + } + + return lastValue; +} + +int mathblocks::get_sequence_size() const +{ + return sequence.size(); +} + +std::vector> mathblocks::get_sequence() const +{ + return sequence; +} + +identity_block::identity_block(std::vector& evaluation_constants) +{ + //Do nothing, used only for template block_factory::create_block + //TODO: Look into "constexpr if" for possible solution to avoid this +} + +double identity_block::evaluate(double input) +{ + return input; +} + +std::vector identity_block::get_evaluation_constants() +{ + return std::vector{}; +} + +std::string identity_block::get_block_name() +{ + return block_name; +} + +addition_block::addition_block(std::vector& evaluation_constants) +{ + std::swap(this->evaluation_constants, evaluation_constants); +} + +double addition_block::evaluate(double input) +{ + for (auto value : evaluation_constants) + { + input += value; + } + + return input; +} + +std::vector addition_block::get_evaluation_constants() +{ + return evaluation_constants; +} + +std::string addition_block::get_block_name() +{ + return block_name; +} + +multiplication_block::multiplication_block(std::vector& evaluation_constants) +{ + std::swap(this->evaluation_constants, evaluation_constants); +} + +double multiplication_block::evaluate(double input) +{ + for (auto value : evaluation_constants) + { + input *= value; + } + + return input; +} + +std::vector multiplication_block::get_evaluation_constants() +{ + return evaluation_constants; +} + +std::string multiplication_block::get_block_name() +{ + return block_name; +} + +power_block::power_block(std::vector& evaluation_constants) +{ + std::swap(this->evaluation_constants, evaluation_constants); +} + +double power_block::evaluate(double input) +{ + for (auto value : evaluation_constants) + { + input = pow(input, value); + } + + return input; +} + +std::vector power_block::get_evaluation_constants() +{ + return evaluation_constants; +} + +std::string power_block::get_block_name() +{ + return block_name; +} + +limit_block::limit_block(std::vector& evaluation_constants) +{ + std::swap(this->evaluation_constants, evaluation_constants); +} + +double limit_block::evaluate(double input) +{ + if (input < evaluation_constants[0]) + { + return evaluation_constants[0]; + } + if (input > evaluation_constants[1]) + { + return evaluation_constants[1]; + } + + return input; +} + +std::vector limit_block::get_evaluation_constants() +{ + return evaluation_constants; +} + +std::string limit_block::get_block_name() +{ + return block_name; +} + +condition_block::condition_block(std::vector& evaluation_constants) +{ + std::swap(this->evaluation_constants, evaluation_constants); +} + +double condition_block::evaluate(double input) +{ + if (input < evaluation_constants[0]) + { + return -1; + } + if (input > evaluation_constants[0]) + { + return 1; + } + + return 0; +} + +std::vector condition_block::get_evaluation_constants() +{ + return evaluation_constants; +} + +std::string condition_block::get_block_name() +{ + return block_name; +} diff --git a/FinalExercise/FinalExercise/mathblocks.h b/FinalExercise/FinalExercise/mathblocks.h new file mode 100644 index 0000000..29e95a2 --- /dev/null +++ b/FinalExercise/FinalExercise/mathblocks.h @@ -0,0 +1,130 @@ +#pragma once +#include +#include + +class mathblocks; +class base_block; + +class block_factory +{ +public: + template + static std::shared_ptr create_block(std::vector& evaluationParameters); +}; + +class block_saver +{ +public: + static void save_blocks_to_file(const std::string& filePath, mathblocks& mathblocks); + static void load_blocks_from_file(const std::string& filePath, mathblocks& mathblocks); +}; + +class mathblocks +{ +public: + void add_block(std::shared_ptr block); + void delete_block(int blockIndex); + void move_block_to_beginning(int blockIndex); + void print_sequence(); + double run_sequence(double value); + int get_sequence_size() const; + std::vector> get_sequence() const; +private: + std::vector> sequence; +}; + +class base_block +{ +public: + virtual ~base_block() = default; + virtual double evaluate(double input) = 0; + virtual std::vector get_evaluation_constants() = 0; + virtual std::string get_block_name() = 0; +}; + +class identity_block final : public base_block +{ +public: + static const std::string block_name; + + identity_block() = default; + identity_block(std::vector& evaluation_constants); + double evaluate(double input) override; + std::vector get_evaluation_constants() override; + std::string get_block_name() override; +}; + +class addition_block final : public base_block +{ +public: + static const int constants_amount = 1; + static const std::string block_name; + + addition_block() = default; + addition_block(std::vector& evaluation_constants); + double evaluate(double input) override; + std::vector get_evaluation_constants() override; + std::string get_block_name() override; +private: + std::vector evaluation_constants; +}; + +class multiplication_block final : public base_block +{ +public: + static const int constants_amount = 1; + static const std::string block_name; + + multiplication_block() = default; + multiplication_block(std::vector& evaluation_constants); + double evaluate(double input) override; + std::vector get_evaluation_constants() override; + std::string get_block_name() override; +private: + std::vector evaluation_constants; +}; + +class power_block final : public base_block +{ +public: + static const int constants_amount = 1; + static const std::string block_name; + + power_block() = default; + power_block(std::vector& evaluation_constants); + double evaluate(double input) override; + std::vector get_evaluation_constants() override; + std::string get_block_name() override; +private: + std::vector evaluation_constants; +}; + +class limit_block final : public base_block +{ +public: + static const int constants_amount = 2; + static const std::string block_name; + + limit_block() = default; + limit_block(std::vector& evaluation_constants); + double evaluate(double input) override; + std::vector get_evaluation_constants() override; + std::string get_block_name() override; +private: + std::vector evaluation_constants; +}; + +class condition_block final : public base_block +{ +public: + static const int constants_amount = 1; + static const std::string block_name; + + condition_block() = default; + condition_block(std::vector& evaluation_constants); + double evaluate(double input) override; + std::vector get_evaluation_constants() override; + std::string get_block_name() override; +private: + std::vector evaluation_constants; +}; diff --git a/FinalExercise/FinalExercise/pch.cpp b/FinalExercise/FinalExercise/pch.cpp new file mode 100644 index 0000000..a38f8a3 Binary files /dev/null and b/FinalExercise/FinalExercise/pch.cpp differ diff --git a/FinalExercise/FinalExercise/pch.h b/FinalExercise/FinalExercise/pch.h new file mode 100644 index 0000000..d87432d Binary files /dev/null and b/FinalExercise/FinalExercise/pch.h differ diff --git a/FinalExercise/FinalExercise/public.h b/FinalExercise/FinalExercise/public.h new file mode 100644 index 0000000..9820cbd --- /dev/null +++ b/FinalExercise/FinalExercise/public.h @@ -0,0 +1,7 @@ +#ifdef DYNAMICLIBRARYWITHIMPLIB_EXPORTS +#define EXPORT _declspec(dllexport) +#else +#define EXPORT _declspec(dllimport) +#endif + +EXPORT double identity(double value); diff --git a/FinalExercise/FinalExerciseTests/FinalExerciseTests.vcxproj b/FinalExercise/FinalExerciseTests/FinalExerciseTests.vcxproj new file mode 100644 index 0000000..11e0dc4 --- /dev/null +++ b/FinalExercise/FinalExerciseTests/FinalExerciseTests.vcxproj @@ -0,0 +1,179 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {D57382B1-FE9F-45E6-92E9-926D43932666} + Win32Proj + FinalExerciseTests + 10.0.17134.0 + NativeUnitTestProject + + + + DynamicLibrary + true + v141 + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + DynamicLibrary + true + v141 + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + + + + + + + + + + + + + + + + + + + true + + + true + + + true + + + true + + + + Use + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + pch.h + + + Windows + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + %(AdditionalDependencies) + + + + + Use + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + _DEBUG;%(PreprocessorDefinitions) + true + + + Windows + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + Use + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + + + Windows + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + Level3 + Use + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + NDEBUG;%(PreprocessorDefinitions) + true + + + Windows + true + true + $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + + + + + + + + + + + Create + Create + Create + Create + + + + + + {49a8615b-edc6-46f3-a8a2-f2d66bd85f9e} + + + + + + \ No newline at end of file diff --git a/FinalExercise/FinalExerciseTests/FinalExerciseTests.vcxproj.filters b/FinalExercise/FinalExerciseTests/FinalExerciseTests.vcxproj.filters new file mode 100644 index 0000000..96bb64b --- /dev/null +++ b/FinalExercise/FinalExerciseTests/FinalExerciseTests.vcxproj.filters @@ -0,0 +1,39 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/FinalExercise/FinalExerciseTests/mathblockstest.cpp b/FinalExercise/FinalExerciseTests/mathblockstest.cpp new file mode 100644 index 0000000..b255f3b --- /dev/null +++ b/FinalExercise/FinalExerciseTests/mathblockstest.cpp @@ -0,0 +1,139 @@ +#include "pch.h" +#include "CppUnitTest.h" +#include "../FinalExercise/mathblocks.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace FinalExerciseTests +{ + TEST_CLASS(UnitTest1) + { + public: + + TEST_METHOD(TestIdentity) + { + identity_block ib; + Assert::AreEqual(12.5, ib.evaluate(12.5)); + Assert::AreEqual(5.0, ib.evaluate(5.0)); + Assert::AreEqual(-7.2, ib.evaluate(-7.2)); + Assert::AreEqual(0.0, ib.evaluate(0.0)); + } + + TEST_METHOD(TestAddition) + { + addition_block ab(std::vector{15.0}); + Assert::AreEqual(20.0, ab.evaluate(5.0)); + addition_block ab2(std::vector{11.25}); + Assert::AreEqual(12.5, ab2.evaluate(1.25)); + addition_block ab3(std::vector{-1.25}); + Assert::AreEqual(-2.5, ab3.evaluate(-1.25)); + } + + TEST_METHOD(TestMultiplication) + { + multiplication_block mb(std::vector{7}); + Assert::AreEqual(49.0, mb.evaluate(7)); + multiplication_block mb2(std::vector{2.5}); + Assert::AreEqual(-7.5, mb2.evaluate(-3)); + multiplication_block mb3(std::vector{12.5}); + Assert::AreEqual(12.5, mb3.evaluate(1)); + multiplication_block mb4(std::vector{17.5}); + Assert::AreEqual(0.0, mb4.evaluate(0)); + } + + TEST_METHOD(TestPower) + { + power_block pb(std::vector{3}); + Assert::AreEqual(125.0, pb.evaluate(5)); + power_block pb2(std::vector{2}); + Assert::AreEqual(25.0, pb2.evaluate(5)); + power_block pb3(std::vector{0}); + Assert::AreEqual(1.0, pb3.evaluate(15)); + } + + TEST_METHOD(TestLimit) + { + limit_block lb(std::vector{5,10}); + Assert::AreEqual(5.0, lb.evaluate(3)); + limit_block lb2(std::vector{5, 10}); + Assert::AreEqual(7.0, lb2.evaluate(7)); + limit_block lb3(std::vector{-10, -5}); + Assert::AreEqual(-5.0, lb3.evaluate(15)); + } + + TEST_METHOD(TestCondition) + { + condition_block cb(std::vector{9}); + Assert::AreEqual(-1.0, cb.evaluate(1)); + condition_block cb2(std::vector{5.5}); + Assert::AreEqual(1.0, cb2.evaluate(10.5)); + condition_block cb3(std::vector{7.5}); + Assert::AreEqual(0.0, cb3.evaluate(7.5)); + } + + TEST_METHOD(TestAddBlock) + { + mathblocks mb; + identity_block ib; + mb.add_block(std::make_shared(ib)); + Assert::AreEqual(1, mb.get_sequence_size()); + } + + TEST_METHOD(TestRunSequence) + { + mathblocks mb; + addition_block ab(std::vector{5.0}); + identity_block ib; + power_block pb(std::vector{2.0}); + mb.add_block(std::make_shared(ab)); + mb.add_block(std::make_shared(pb)); + mb.add_block(std::make_shared(ib)); + Assert::AreEqual(49.0, mb.run_sequence(2)); + } + + TEST_METHOD(TestDeleteBlock) + { + mathblocks mb; + addition_block ab(std::vector{5.0}); + identity_block ib; + power_block pb(std::vector{2.0}); + mb.add_block(std::make_shared(ab)); + mb.add_block(std::make_shared(pb)); + mb.add_block(std::make_shared(ib)); + mb.delete_block(0); + Assert::AreEqual(2, mb.get_sequence_size()); + Assert::AreEqual(25.0, mb.run_sequence(5)); + } + + TEST_METHOD(TestMove) + { + mathblocks mb; + addition_block ab(std::vector{5.0}); + identity_block ib; + power_block pb(std::vector{2.0}); + mb.add_block(std::make_shared(ab)); + mb.add_block(std::make_shared(ib)); + mb.add_block(std::make_shared(pb)); + mb.move_block_to_beginning(1); + Assert::AreEqual(std::string("Identity"), mb.get_sequence()[0]->get_block_name()); + } + + TEST_METHOD(TestSaveAndLoadSequence) + { + mathblocks mb; + addition_block ab(std::vector{5.0}); + identity_block ib; + power_block pb(std::vector{2.0}); + mb.add_block(std::make_shared(ab)); + mb.add_block(std::make_shared(pb)); + mb.add_block(std::make_shared(ib)); + Assert::AreEqual(3, mb.get_sequence_size()); + Assert::AreEqual(100.0, mb.run_sequence(5)); + block_saver::save_blocks_to_file("test", mb); + mathblocks mb2; + block_saver::load_blocks_from_file("test", mb2); + Assert::AreEqual(3, mb2.get_sequence_size()); + Assert::AreEqual(100.0, mb2.run_sequence(5)); + } + }; +} \ No newline at end of file diff --git a/FinalExercise/FinalExerciseTests/pch.h b/FinalExercise/FinalExerciseTests/pch.h new file mode 100644 index 0000000..5105641 --- /dev/null +++ b/FinalExercise/FinalExerciseTests/pch.h @@ -0,0 +1,13 @@ +// pch.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +#include "targetver.h" + +// Headers for CppUnitTest +#include "CppUnitTest.h" + +// TODO: reference additional headers your program requires here diff --git a/FinalExercise/FinalExerciseTests/stdafx.cpp b/FinalExercise/FinalExerciseTests/stdafx.cpp new file mode 100644 index 0000000..5669aba --- /dev/null +++ b/FinalExercise/FinalExerciseTests/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : source file that includes just the standard includes +// FinalExerciseTests.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "pch.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff --git a/FinalExercise/FinalExerciseTests/targetver.h b/FinalExercise/FinalExerciseTests/targetver.h new file mode 100644 index 0000000..87c0086 --- /dev/null +++ b/FinalExercise/FinalExerciseTests/targetver.h @@ -0,0 +1,8 @@ +#pragma once + +// Including SDKDDKVer.h defines the highest available Windows platform. + +// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and +// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. + +#include