diff --git a/exercise.sln b/exercise.sln new file mode 100644 index 0000000..05b3067 --- /dev/null +++ b/exercise.sln @@ -0,0 +1,41 @@ + +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}") = "exercise", "exercise\exercise.vcxproj", "{75B1B104-E546-467E-A588-61634BCF80CB}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "test\test.vcxproj", "{8D67B919-8D14-4146-A6DD-6DCA39FC443B}" +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 + {75B1B104-E546-467E-A588-61634BCF80CB}.Debug|x64.ActiveCfg = Debug|x64 + {75B1B104-E546-467E-A588-61634BCF80CB}.Debug|x64.Build.0 = Debug|x64 + {75B1B104-E546-467E-A588-61634BCF80CB}.Debug|x86.ActiveCfg = Debug|Win32 + {75B1B104-E546-467E-A588-61634BCF80CB}.Debug|x86.Build.0 = Debug|Win32 + {75B1B104-E546-467E-A588-61634BCF80CB}.Release|x64.ActiveCfg = Release|x64 + {75B1B104-E546-467E-A588-61634BCF80CB}.Release|x64.Build.0 = Release|x64 + {75B1B104-E546-467E-A588-61634BCF80CB}.Release|x86.ActiveCfg = Release|Win32 + {75B1B104-E546-467E-A588-61634BCF80CB}.Release|x86.Build.0 = Release|Win32 + {8D67B919-8D14-4146-A6DD-6DCA39FC443B}.Debug|x64.ActiveCfg = Debug|x64 + {8D67B919-8D14-4146-A6DD-6DCA39FC443B}.Debug|x64.Build.0 = Debug|x64 + {8D67B919-8D14-4146-A6DD-6DCA39FC443B}.Debug|x86.ActiveCfg = Debug|Win32 + {8D67B919-8D14-4146-A6DD-6DCA39FC443B}.Debug|x86.Build.0 = Debug|Win32 + {8D67B919-8D14-4146-A6DD-6DCA39FC443B}.Release|x64.ActiveCfg = Release|x64 + {8D67B919-8D14-4146-A6DD-6DCA39FC443B}.Release|x64.Build.0 = Release|x64 + {8D67B919-8D14-4146-A6DD-6DCA39FC443B}.Release|x86.ActiveCfg = Release|Win32 + {8D67B919-8D14-4146-A6DD-6DCA39FC443B}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FBDE9BFE-4D82-4425-8527-F91DC4CC7627} + EndGlobalSection +EndGlobal diff --git a/exercise/block.cpp b/exercise/block.cpp new file mode 100644 index 0000000..3a69976 --- /dev/null +++ b/exercise/block.cpp @@ -0,0 +1,152 @@ +#include "pch.h" +#include "block.h" + +class block_type +{ +public: + static const int identity = 1; + static const int addition = 2; + static const int multiplication = 3; + static const int power = 4; + static const int limit = 5; + static const int condition = 6; +}; + +block::~block() +{ +} + +void block::input(double input) +{ + in = input; +} + +std::ostream& operator<<(std::ostream& os, const block& block) +{ + block.print(os); + return os; +} + +void identity::print(std::ostream& os) const +{ + os << block_type::identity << " "; +} + +double identity::output () const +{ + return in; +} + +void addition::print(std::ostream& os) const +{ + os << block_type::addition << " " << constant << " "; +} + +double addition::output() const +{ + return in + constant; +} + +void multiplication::print(std::ostream& os) const +{ + os << block_type::multiplication << " " << constant << " "; +} + +double multiplication::output() const +{ + return in * constant; +} + +void power::print(std::ostream& os) const +{ + os << block_type::power << " " << constant << " "; +} + +double power::output() const +{ + return std::pow(in, constant); +} + +void limit::print(std::ostream& os) const +{ + os << block_type::limit << " " << minimum << " " << maximum << " "; +} + +double limit::output() const +{ + return std::clamp(in, minimum, maximum); +} + +void condition::print(std::ostream& os) const +{ + os << block_type::condition << " " << constant << " "; +} + +double condition::output() const +{ + if (in > constant) + { + return 1; + } + else if (in < constant) + { + return -1; + } + else + { + return 0; + } +} + +std::unique_ptr create_block(int type, std::istream& stream) +{ + switch (type) + { + case block_type::identity: + { + return std::make_unique(); + } + case block_type::addition: + { + double constant; + if (stream >> constant) + { + return std::make_unique(constant); + } + } + case block_type::multiplication: + { + double constant; + if (stream >> constant) + { + return std::make_unique(constant); + } + } + case block_type::power: + { + double constant; + if (stream >> constant) + { + return std::make_unique(constant); + } + } + case block_type::limit: + { + double min, max; + if (stream >> min >> max) + { + return std::make_unique(min, max); + } + } + case block_type::condition: + { + double constant; + if (stream >> constant) + { + return std::make_unique(constant); + } + } + default: + return nullptr; + } +} diff --git a/exercise/block.h b/exercise/block.h new file mode 100644 index 0000000..cef742e --- /dev/null +++ b/exercise/block.h @@ -0,0 +1,87 @@ +#pragma once + +class block +{ +protected: + double in; + virtual void print(std::ostream& os) const = 0; +public: + block() : in(0.){} + void input(double input); + virtual double output() const = 0; + virtual ~block() = 0; + friend std::ostream& operator<<(std::ostream& os, const block& block); +}; + +class block_with_constant : public block +{ +protected: + double constant; +public: + block_with_constant(double constant = 0) : constant(constant) {} +}; + +// returns input +class identity : public block +{ +protected: + void print(std::ostream& os) const override; +public: + double output() const override; +}; + +// returns input + constant +class addition : public block_with_constant +{ +protected: + void print(std::ostream& os) const override; +public: + addition(double constant = 0) : block_with_constant(constant) {} + double output() const override; +}; + +// returns input * constant +class multiplication : public block_with_constant +{ +protected: + void print(std::ostream& os) const override; +public: + multiplication(double constant = 0) : block_with_constant(constant) {} + double output() const override; +}; + +// returns pow(input, constant) +class power : public block_with_constant +{ +protected: + void print(std::ostream& os) const override; +public: + power(double constant = 0) : block_with_constant(constant) {} + double output() const override; +}; + +// returns input constrained by min and max +class limit : public block +{ +private: + double minimum; + double maximum; +protected: + void print(std::ostream& os) const override; +public: + limit(double minimum = 0, double maximum = 0) : minimum(minimum), maximum(maximum) {} + double output() const override; +}; + +// returns -1, 0, 1 based on comparison with constant +class condition : public block_with_constant +{ +protected: + void print(std::ostream& os) const override; +public: + condition(double constant = 0) : block_with_constant(constant) {} + double output() const override; +}; + +// factory function +std::unique_ptr create_block(int type, std::istream& stream); diff --git a/exercise/command.cpp b/exercise/command.cpp new file mode 100644 index 0000000..f838001 --- /dev/null +++ b/exercise/command.cpp @@ -0,0 +1,183 @@ +#include "pch.h" +#include "command.h" +#include "sequence.h" + +command::~command() +{ +} + +bool help::execute() const +{ + std::cout + << "Sequence Help" << std::endl + << "Commands:" << std::endl + << "exercise --help" << std::endl + << "exercise --add " << std::endl + << "exercise --remove " << std::endl + << "exercise --front " << std::endl + << "exercise --print" << std::endl + << "exercise --eval " << std::endl + << "exercise --eval " << std::endl; + return true; +} + +const std::string file_name = "local.txt"; + +bool add_block::execute() const +{ + sequence s(file_name); + return s.add(type, parameters); +} + +bool remove_block::execute() const +{ + sequence s(file_name); + return s.remove(index); +} + +bool front_block::execute() const +{ + sequence s(file_name); + return s.front(index); +} + +bool print_seq::execute() const +{ + sequence s(file_name); + return s.print(std::cout); +} + +bool eval_value::execute() const +{ + sequence s(file_name); + return s.eval(value, std::cout); +} + +bool eval_file::execute() const +{ + std::ifstream in(input); + std::ofstream out(output); + + sequence s(file_name); + return s.eval(in, out); +} + +std::optional parse_int(const std::string& s) +{ + try + { + return std::stoi(s); + } + catch (...) + { + return std::nullopt; + } +} + +std::optional parse_double(const std::string& s) +{ + try + { + return std::stod(s); + } + catch (...) + { + return std::nullopt; + } +} + +std::unique_ptr create_add_block(int argc, char* argv[]) +{ + // get type + const std::string type_s(argv[2]); + std::optional type = parse_int(type_s); + if(!type.has_value()) + { + return nullptr; + } + + // get parameters + std::string parameters; + for (int i = 3; i < argc; i++) + { + parameters.append(" "); + parameters.append(argv[i]); + } + + return std::make_unique(type.value(), parameters); +} + +std::unique_ptr create_remove_block(char* argv[]) +{ + // get index + const std::string index_s(argv[2]); + std::optional index = parse_int(index_s); + return index.has_value() ? std::make_unique(index.value()) : nullptr; +} + +std::unique_ptr create_front_block(char* argv[]) +{ + // get index + const std::string index_s(argv[2]); + std::optional index = parse_int(index_s); + return index.has_value() ? std::make_unique(index.value()) : nullptr; +} + +std::unique_ptr create_eval_value(char* argv[]) +{ + // get value + const std::string value_s(argv[2]); + std::optional value = parse_double(value_s); + return value.has_value() ? std::make_unique(value.value()) : nullptr; +} + +std::unique_ptr create_eval_file(char* argv[]) +{ + // get file paths + std::string input(argv[2]); + std::string output(argv[3]); + + return std::make_unique(input, output); +} + +std::unique_ptr create_command(int argc, char* argv[]) +{ + if (argc < 2) + { + return nullptr; + } + + std::string command(argv[1]); + if (argc == 2 && command.compare("--help") == 0) + { + return std::make_unique(); + } + else if (argc >= 3 && command.compare("--add") == 0) + { + return create_add_block(argc, argv); + } + else if (argc == 3 && command.compare("--remove") == 0) + { + return create_remove_block(argv); + } + else if (argc == 3 && command.compare("--front") == 0) + { + return create_front_block(argv); + } + else if (argc == 2 && command.compare("--print") == 0) + { + return std::make_unique(); + } + else if (argc == 3 && command.compare("--eval") == 0) + { + return create_eval_value(argv); + } + else if (argc == 4 && command.compare("--eval") == 0) + { + return create_eval_file(argv); + } + else + { + return nullptr; + } +} diff --git a/exercise/command.h b/exercise/command.h new file mode 100644 index 0000000..1196fee --- /dev/null +++ b/exercise/command.h @@ -0,0 +1,70 @@ +#pragma once + +class command +{ +public: + virtual bool execute() const = 0; + virtual ~command() = 0; +}; + +class help : public command +{ +public: + bool execute() const override; +}; + +class add_block : public command +{ +private: + int type; + std::string parameters; +public: + add_block(int type, std::string parameters) : type(type), parameters(parameters) {}; + bool execute() const override; +}; + +class remove_block : public command +{ +private: + int index; +public: + remove_block(int index) : index(index) {}; + bool execute() const override; +}; + +class front_block : public command +{ +private: + int index; +public: + front_block(int index) : index(index) {}; + bool execute() const override; +}; + +class print_seq : public command +{ +public: + bool execute() const override; +}; + +class eval_value : public command +{ +private: + double value; +public: + eval_value(double value) : value(value) {}; + bool execute() const override; +}; + +class eval_file : public command +{ +private: + std::string input; + std::string output; +public: + eval_file(std::string input, std::string output) : input(input), output(output) {}; + bool execute() const override; +}; + +std::unique_ptr create_command(int argc, char* argv[]); + diff --git a/exercise/exercise.cpp b/exercise/exercise.cpp new file mode 100644 index 0000000..d110b80 Binary files /dev/null and b/exercise/exercise.cpp differ diff --git a/exercise/exercise.vcxproj b/exercise/exercise.vcxproj new file mode 100644 index 0000000..5c3cb23 --- /dev/null +++ b/exercise/exercise.vcxproj @@ -0,0 +1,178 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {75B1B104-E546-467E-A588-61634BCF80CB} + Win32Proj + exercise + 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 + stdcpp17 + + + Console + true + + + + + Use + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + stdcpp17 + + + Console + true + + + + + Use + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + stdcpp17 + + + Console + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + stdcpp17 + + + Console + true + true + true + + + + + + + + + + + + + + Create + Create + Create + Create + + + + + + + \ No newline at end of file diff --git a/exercise/exercise.vcxproj.filters b/exercise/exercise.vcxproj.filters new file mode 100644 index 0000000..b76f1f2 --- /dev/null +++ b/exercise/exercise.vcxproj.filters @@ -0,0 +1,48 @@ + + + + + {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 + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/exercise/pch.cpp b/exercise/pch.cpp new file mode 100644 index 0000000..a38f8a3 Binary files /dev/null and b/exercise/pch.cpp differ diff --git a/exercise/pch.h b/exercise/pch.h new file mode 100644 index 0000000..9f45857 Binary files /dev/null and b/exercise/pch.h differ diff --git a/exercise/sequence.cpp b/exercise/sequence.cpp new file mode 100644 index 0000000..384f99c --- /dev/null +++ b/exercise/sequence.cpp @@ -0,0 +1,125 @@ +#include "pch.h" +#include "sequence.h" + +sequence::sequence(std::string file) : file(file) +{ + std::ifstream fs(file); + + int type; + while(fs >> type) + { + blocks.push_back(create_block(type, fs)); + } +} + +sequence::~sequence() +{ + std::ofstream fs(file); + for (auto it = blocks.begin(); it != blocks.end(); ++it) + { + fs << **it; + } +} + +bool sequence::add(int type, const std::string& parameters) +{ + bool result = false; + + std::stringstream ss(parameters); + std::unique_ptr block = create_block(type, ss); + if (block != nullptr) + { + blocks.push_back(std::move(block)); + result = true; + } + + return result; +} + +bool sequence::check_index(size_t index) const +{ + return 0 <= index && index <= blocks.size() - 1; +} + +bool sequence::remove(size_t index) +{ + bool result = false; + + if (check_index(index)) + { + auto it = blocks.begin(); + std::advance(it, index); + blocks.erase(it); + result = true; + } + + return result; +} + +bool sequence::front(size_t index) +{ + bool result = false; + + if (check_index(index)) + { + auto it = blocks.begin(); + std::advance(it, index); + std::unique_ptr block = std::move(*it); + blocks.erase(it); + blocks.insert(blocks.begin(), std::move(block)); + result = true; + } + + return result; +} + +bool sequence::print(std::ostream& os) const +{ + for (auto it = blocks.begin(); it != blocks.end(); ++it) + { + os << **it; + } + + return true; +} + +double sequence::eval_impl(double input) const +{ + double result = input; + for (auto it = blocks.begin(); it != blocks.end(); ++it) + { + (*it)->input(result); + result = (*it)->output(); + } + return result; +} + +bool sequence::eval(double value, std::ostream& os) const +{ + bool result = false; + + if (blocks.size() > 0) + { + os << eval_impl(value); + result = true; + } + + return result; +} + +bool sequence::eval(std::istream& is, std::ostream& os) const +{ + bool result = false; + + if (blocks.size() > 0) + { + double value; + while (is >> value) + { + os << eval_impl(value) << " "; + } + result = true; + } + + return result; +} \ No newline at end of file diff --git a/exercise/sequence.h b/exercise/sequence.h new file mode 100644 index 0000000..073e5ae --- /dev/null +++ b/exercise/sequence.h @@ -0,0 +1,21 @@ +#pragma once + +#include "block.h" + +class sequence +{ +private: + std::string file; + std::vector > blocks; + bool check_index(size_t index) const; + double eval_impl(double) const; +public: + sequence(std::string file); + ~sequence(); + bool add(int type, const std::string& parameters); + bool remove(size_t index); + bool front(size_t index); + bool print(std::ostream& os) const; + bool eval(double value, std::ostream& os) const; + bool eval(std::istream& is, std::ostream& os) const; +}; diff --git a/test/block_test.cpp b/test/block_test.cpp new file mode 100644 index 0000000..6e75358 --- /dev/null +++ b/test/block_test.cpp @@ -0,0 +1,202 @@ +#include "stdafx.h" +#include "../exercise/block.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace test +{ + TEST_CLASS(block_test) + { + public: + TEST_METHOD(identity_output) + { + identity block; + block.input(4.5); + double out = block.output(); + Assert::AreEqual(4.5, out); + } + TEST_METHOD(addition_output) + { + addition block(4.5); + block.input(1); + double out = block.output(); + Assert::AreEqual(5.5, out); + } + TEST_METHOD(multiplication_output) + { + multiplication block(3.5); + block.input(2); + double out = block.output(); + Assert::AreEqual(7.0, out); + } + TEST_METHOD(power_output) + { + power block(3.0); + block.input(2); + double out = block.output(); + Assert::AreEqual(8.0, out); + } + TEST_METHOD(limit_output_bellow_range) + { + limit block(3.5, 8.7); + block.input(2); + double out = block.output(); + Assert::AreEqual(3.5, out); + } + TEST_METHOD(limit_output_in_range) + { + limit block(3.5, 8.7); + block.input(5.5); + double out = block.output(); + Assert::AreEqual(5.5, out); + } + TEST_METHOD(limit_output_above_range) + { + limit block(3.5, 8.7); + block.input(9.3); + double out = block.output(); + Assert::AreEqual(8.7, out); + } + TEST_METHOD(condition_output_bellow_limit) + { + condition block(5.0); + block.input(2); + double out = block.output(); + Assert::AreEqual(-1.0, out); + } + TEST_METHOD(condition_output_at_limit) + { + condition block(5.0); + block.input(5); + double out = block.output(); + Assert::AreEqual(0.0, out); + } + TEST_METHOD(condition_output_above_limit) + { + condition block(5.0); + block.input(7); + double out = block.output(); + Assert::AreEqual(1.0, out); + } + TEST_METHOD(print_identity) + { + std::stringstream ss; + identity block; + ss << block; + Assert::AreEqual(0, ss.str().compare("1 ")); + } + TEST_METHOD(print_addition) + { + std::stringstream ss; + addition block(4.5); + ss << block; + Assert::AreEqual(0, ss.str().compare("2 4.5 ")); + } + TEST_METHOD(print_multiplication) + { + std::stringstream ss; + multiplication block(3.5); + ss << block; + Assert::AreEqual(0, ss.str().compare("3 3.5 ")); + } + TEST_METHOD(print_power) + { + std::stringstream ss; + power block(3.0); + ss << block; + Assert::AreEqual(0, ss.str().compare("4 3 ")); + } + TEST_METHOD(print_limit) + { + std::stringstream ss; + limit block(3.5, 8.7); + ss << block; + Assert::AreEqual(0, ss.str().compare("5 3.5 8.7 ")); + } + TEST_METHOD(print_condition) + { + std::stringstream ss; + condition block(5.0); + ss << block; + Assert::AreEqual(0, ss.str().compare("6 5 ")); + } + TEST_METHOD(create_non_existing_block) + { + std::stringstream ss; + std::unique_ptr block = create_block(100, ss); + Assert::IsTrue(nullptr == block); + } + TEST_METHOD(create_identity) + { + std::stringstream ss; + std::unique_ptr block = create_block(1, ss); + Assert::IsTrue(nullptr != block); + Assert::IsTrue(std::type_index(typeid(*block)) == std::type_index(typeid(identity))); + } + TEST_METHOD(create_addition_from_valid_parameters) + { + std::stringstream ss("4"); + std::unique_ptr block = create_block(2, ss); + Assert::IsTrue(nullptr != block); + Assert::IsTrue(std::type_index(typeid(*block)) == std::type_index(typeid(addition))); + } + TEST_METHOD(create_addition_from_invalid_parameters) + { + std::stringstream ss; + std::unique_ptr block = create_block(2, ss); + Assert::IsTrue(nullptr == block); + } + TEST_METHOD(create_multiplication_from_valid_parameters) + { + std::stringstream ss("4"); + std::unique_ptr block = create_block(3, ss); + Assert::IsTrue(nullptr != block); + Assert::IsTrue(std::type_index(typeid(*block)) == std::type_index(typeid(multiplication))); + } + TEST_METHOD(create_multiplication_from_invalid_parameters) + { + std::stringstream ss; + std::unique_ptr block = create_block(3, ss); + Assert::IsTrue(nullptr == block); + } + TEST_METHOD(create_power_from_valid_parameters) + { + std::stringstream ss("4"); + std::unique_ptr block = create_block(4, ss); + Assert::IsTrue(nullptr != block); + Assert::IsTrue(std::type_index(typeid(*block)) == std::type_index(typeid(power))); + } + TEST_METHOD(create_power_from_invalid_parameters) + { + std::stringstream ss; + std::unique_ptr block = create_block(4, ss); + Assert::IsTrue(nullptr == block); + } + TEST_METHOD(create_limit_from_valid_parameters) + { + std::stringstream ss("4 6"); + std::unique_ptr block = create_block(5, ss); + Assert::IsTrue(nullptr != block); + Assert::IsTrue(std::type_index(typeid(*block)) == std::type_index(typeid(limit))); + } + TEST_METHOD(create_limit_from_invalid_parameters) + { + std::stringstream ss; + std::unique_ptr block = create_block(5, ss); + Assert::IsTrue(nullptr == block); + } + TEST_METHOD(create_condition_from_valid_parameters) + { + std::stringstream ss("4"); + std::unique_ptr block = create_block(6, ss); + Assert::IsTrue(nullptr != block); + Assert::IsTrue(std::type_index(typeid(*block)) == std::type_index(typeid(condition))); + } + TEST_METHOD(create_condition_from_invalid_parameters) + { + std::stringstream ss; + std::unique_ptr block = create_block(6, ss); + Assert::IsTrue(nullptr == block); + } + }; +} \ No newline at end of file diff --git a/test/sequence_test.cpp b/test/sequence_test.cpp new file mode 100644 index 0000000..7c6f4b4 --- /dev/null +++ b/test/sequence_test.cpp @@ -0,0 +1,84 @@ +#include "stdafx.h" +#include "../exercise/sequence.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace test +{ + const std::string file_name = "test.txt"; + + TEST_CLASS(sequnece_test) + { + public: + TEST_METHOD_CLEANUP(remove_sequence_file) + { + std::remove(file_name.c_str()); + } + TEST_METHOD(add_block) + { + sequence s(file_name); + s.add(1, ""); + s.add(2, "2.5"); + s.add(3, "3.5"); + s.add(4, "2"); + s.add(5, "-3.5 8.4"); + s.add(6, "5.5"); + + std::stringstream ss; + s.print(ss); + + Assert::AreEqual(0, ss.str().compare("1 2 2.5 3 3.5 4 2 5 -3.5 8.4 6 5.5 ")); + } + TEST_METHOD(remove_block) + { + sequence s(file_name); + s.add(1, ""); + s.add(2, "2.5"); + s.add(3, "3.5"); + s.remove(0); + + std::stringstream ss; + s.print(ss); + + Assert::AreEqual(0, ss.str().compare("2 2.5 3 3.5 ")); + } + TEST_METHOD(front_block) + { + sequence s(file_name); + s.add(1, ""); + s.add(2, "2.5"); + s.add(3, "3.5"); + s.front(2); + + std::stringstream ss; + s.print(ss); + + Assert::AreEqual(0, ss.str().compare("3 3.5 1 2 2.5 ")); + } + TEST_METHOD(eval_value) + { + sequence s(file_name); + s.add(1, ""); + s.add(2, "2.5"); + s.add(3, "3.5"); + + std::stringstream ss; + s.eval(3.5, ss); + + Assert::AreEqual(0, ss.str().compare("21")); + } + TEST_METHOD(eval_stream) + { + sequence s(file_name); + s.add(1, ""); + s.add(2, "2.5"); + s.add(3, "3.5"); + + std::stringstream input("1.0 2.3 4.5 7.9"); + std::stringstream output; + s.eval(input, output); + + Assert::AreEqual(0, output.str().compare("12.25 16.8 24.5 36.4 ")); + } + }; +} \ No newline at end of file diff --git a/test/stdafx.cpp b/test/stdafx.cpp new file mode 100644 index 0000000..ab6cf71 --- /dev/null +++ b/test/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : source file that includes just the standard includes +// test.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "stdafx.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff --git a/test/stdafx.h b/test/stdafx.h new file mode 100644 index 0000000..68eb05d --- /dev/null +++ b/test/stdafx.h @@ -0,0 +1,16 @@ +// stdafx.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" +#include +#include +#include + +// TODO: reference additional headers your program requires here diff --git a/test/targetver.h b/test/targetver.h new file mode 100644 index 0000000..87c0086 --- /dev/null +++ b/test/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 diff --git a/test/test.vcxproj b/test/test.vcxproj new file mode 100644 index 0000000..1bcc276 --- /dev/null +++ b/test/test.vcxproj @@ -0,0 +1,188 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {8D67B919-8D14-4146-A6DD-6DCA39FC443B} + Win32Proj + test + 10.0.17763.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 + $(ProjectDir)$(Configuration)\ + + + true + $(ProjectDir)$(Platform)\$(Configuration)\ + + + true + $(ProjectDir)$(Configuration)\ + + + true + $(ProjectDir)$(Platform)\$(Configuration)\ + + + + Use + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + + + Windows + ../exercise/$(Configuration);$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + block.obj;command.obj;pch.obj;sequence.obj;%(AdditionalDependencies) + + + + + Use + Level3 + Disabled + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + _DEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + + + Windows + ../exercise/$(Platform)/$(Configuration);$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + block.obj;command.obj;pch.obj;sequence.obj;%(AdditionalDependencies) + + + + + Level3 + Use + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + + + Windows + true + true + ../exercise/$(Configuration);$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + block.obj;command.obj;pch.obj;sequence.obj;%(AdditionalDependencies) + + + + + Level3 + Use + MaxSpeed + true + true + $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) + NDEBUG;%(PreprocessorDefinitions) + true + stdcpp17 + + + Windows + true + true + ../exercise/$(Platform)/$(Configuration);$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + block.obj;command.obj;pch.obj;sequence.obj;%(AdditionalDependencies) + + + + + + + + + + Create + Create + Create + Create + + + + + + {75b1b104-e546-467e-a588-61634bcf80cb} + + + + + + \ No newline at end of file diff --git a/test/test.vcxproj.filters b/test/test.vcxproj.filters new file mode 100644 index 0000000..79f34d0 --- /dev/null +++ b/test/test.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 + + + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file