From d3fca0b3d0e9509fb91db7574df8785dda5bb5e4 Mon Sep 17 00:00:00 2001 From: marasi Date: Fri, 16 Nov 2018 10:09:56 +0100 Subject: [PATCH 1/7] created project --- exercise.sln | 31 ++++++ exercise/exercise.cpp | Bin 0 -> 1700 bytes exercise/exercise.vcxproj | 168 ++++++++++++++++++++++++++++++ exercise/exercise.vcxproj.filters | 30 ++++++ exercise/pch.cpp | Bin 0 -> 430 bytes exercise/pch.h | Bin 0 -> 1262 bytes 6 files changed, 229 insertions(+) create mode 100644 exercise.sln create mode 100644 exercise/exercise.cpp create mode 100644 exercise/exercise.vcxproj create mode 100644 exercise/exercise.vcxproj.filters create mode 100644 exercise/pch.cpp create mode 100644 exercise/pch.h diff --git a/exercise.sln b/exercise.sln new file mode 100644 index 0000000..9df518c --- /dev/null +++ b/exercise.sln @@ -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}") = "exercise", "exercise\exercise.vcxproj", "{75B1B104-E546-467E-A588-61634BCF80CB}" +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 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FBDE9BFE-4D82-4425-8527-F91DC4CC7627} + EndGlobalSection +EndGlobal diff --git a/exercise/exercise.cpp b/exercise/exercise.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b13bf3971924035b013e700a99a55c261c8bf94 GIT binary patch literal 1700 zcma)-U29W85QgVk@IS2aqKcB>N4=<}AllY~Sh11bc$1zarHm)(q!Y>(NuOFvIG<~!j&abMZg7B=8y zicVsG={Gxey*l0DbZkrR3vRDmqs!SRJXKb*4v@{>3na5tvI}gLYuf{C3|&&+#@pPn z-nV;1(KCHDAG%%v(k0SD+aZ<$d|hVeA2R!num*#y3faNhqGDWNCr9?k9{b%7UlTVP z(jCIsp0lH9lI=6(|2;M*jEZ09taJ85dthI&+_Bf33i;2q$H>cPXD@+%lQHGwt>c`q zd%`@jT~=95_6|=M=#}gbm}f{1xDlFjVpN_g=Xz5%xR%Z}`cv*RtD917Nbc&p!PV}o z@z)3C%(FaR#dHj&1w(ld#wMnbGal>(XUfDgGM(%THkAEbnP*s2O*B<@NZrpoE6Tap zyKx^wByU_(`5NG3NPQ{$Q)cN_b1&2zOv(52R^NSeqT27~Gx}?amU>4q2uaPl1ZUw0 zau2FDCR-?8w4ylm=x4sR$129enJP%VuC9w}$y&J=s}-+eO*K<9f}3qdUbbR6Bn}~$ z{s(-id)hI5=XT`zP>w=p+sCf^IL2P)DqfjyXaryP;xzsf#PI3bmVRw>lWRv+CY@Z{ Ofv$_^iQ~vTE22Mm@D&pP literal 0 HcmV?d00001 diff --git a/exercise/exercise.vcxproj b/exercise/exercise.vcxproj new file mode 100644 index 0000000..edf7bc6 --- /dev/null +++ b/exercise/exercise.vcxproj @@ -0,0 +1,168 @@ + + + + + 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 + + + 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/exercise/exercise.vcxproj.filters b/exercise/exercise.vcxproj.filters new file mode 100644 index 0000000..921adad --- /dev/null +++ b/exercise/exercise.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {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 + + + + + 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 0000000000000000000000000000000000000000..a38f8a37da858b0e7a4a577210e923c883ac97f3 GIT binary patch literal 430 zcma)(OAdlS5JYQj;vFWsRoUwaJO;ubLXfO} ziM!*RK?b#&)PqF4LX~Q_GVq=g$>^{f<-{7@$r|ykmqm|MwtC_%l{t|TJS9Jpalvn} ztpC7C!Dg_+`-h+El+KlNtsDc|GOvyF+iK(A-j!r1ot!0T2mdlziM{46Ghg8k=#H+V zM?tLvNCnoo3Y&aHb%nm~4lL&e!^r!5 zY>)Yt7?F2lcmBR1+QK#-XGNr7XZ8fr!cn3lZ5^Y)vUSfD^AwDN*h^Lw&)lPx++47# zd)SAA*b{C}iSS;`84M~$v>AMrgP7FJ8+gu~LAukhS6*EOJ`b`OdG>3c0~t!sZmv>H zn*HLA@+(*ivq2}#QbRtnCS6sZB|bGHY13EaXpqYpyv1DpS76m1eTdT|r_t-78YQ1T z#<62u5HD*r_L~oQCI!E8@(t$Ve*GNp63pmTN&3$4QJrKBuD-fJIu~wfYrcy;aeoU( z{gpDyo!duDy%mVwzd3}ObITpQ?&R3*fD>2bk}TRUp!6E5gQ6*el=X_*#+Q5I8?Z>{ zRy|dAN-mZA&0pz!j#b}5FeyBGiKSg%o7_9|!S#V|#ttsxpDyDUbdaS$838t Date: Fri, 16 Nov 2018 12:30:28 +0100 Subject: [PATCH 2/7] Added classes --- exercise/block.cpp | 73 +++++++++++++++++++++++++++++++ exercise/block.h | 73 +++++++++++++++++++++++++++++++ exercise/exercise.vcxproj | 6 +++ exercise/exercise.vcxproj.filters | 6 +++ 4 files changed, 158 insertions(+) create mode 100644 exercise/block.cpp create mode 100644 exercise/block.h diff --git a/exercise/block.cpp b/exercise/block.cpp new file mode 100644 index 0000000..227d0b5 --- /dev/null +++ b/exercise/block.cpp @@ -0,0 +1,73 @@ +#include "pch.h" +#include "block.h" + +block::~block() +{ +} + +void block::input(double input) +{ + in = input; +} + +double identity::output () const +{ + return in; +} + +double addition::output() const +{ + return in + constant; +} + +double multiplication::output() const +{ + return in * constant; +} + +double power::output() const +{ + return std::pow(in, constant); +} + +double limit::output() const +{ + return std::clamp(in, min, max); +} + +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, const std::vector& block_parameters) +{ + switch (type) + { + case 1: + return std::make_unique(); + case 2: + return std::make_unique(block_parameters[0]); + case 3: + return std::make_unique(block_parameters[0]); + case 4: + return std::make_unique(block_parameters[0]); + case 5: + return std::make_unique(block_parameters[0], block_parameters[1]); + case 6: + return std::make_unique(block_parameters[0]); + default: + return nullptr; + } +} diff --git a/exercise/block.h b/exercise/block.h new file mode 100644 index 0000000..eccc9df --- /dev/null +++ b/exercise/block.h @@ -0,0 +1,73 @@ +#pragma once + +class block +{ +protected: + double in; +public: + block() : in(0.){} + void input(double input); + virtual double output() const = 0; + virtual ~block() = 0; +}; + +class block_with_constant : public block +{ +protected: + double constant; +public: + block_with_constant(double constant = 0) : constant(constant) {} +}; + +// returns input +class identity : public block +{ +public: + double output() const override; +}; + +// returns input + constant +class addition : public block_with_constant +{ +public: + addition(double constant = 0) : block_with_constant(constant) {} + double output() const override; +}; + +// returns input * constant +class multiplication : public block_with_constant +{ +public: + multiplication(double constant = 0) : block_with_constant(constant) {} + double output() const override; +}; + +// returns pow(input, constant) +class power : public block_with_constant +{ +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 min; + double max; +public: + limit(double min = 0, double max = 0) {} + double output() const override; +}; + +// returns -1, 0, 1 based on comparison with constant +class condition : public block_with_constant +{ +public: + condition(double constant = 0) : block_with_constant(constant) {} + double output() const override; +}; + +std::unique_ptr create_block(int type, const std::vector& block_parameters); + diff --git a/exercise/exercise.vcxproj b/exercise/exercise.vcxproj index edf7bc6..6a9e6b0 100644 --- a/exercise/exercise.vcxproj +++ b/exercise/exercise.vcxproj @@ -91,6 +91,7 @@ WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true pch.h + stdcpp17 Console @@ -106,6 +107,7 @@ _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true pch.h + stdcpp17 Console @@ -123,6 +125,7 @@ WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true pch.h + stdcpp17 Console @@ -142,6 +145,7 @@ NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true pch.h + stdcpp17 Console @@ -151,9 +155,11 @@ + + Create diff --git a/exercise/exercise.vcxproj.filters b/exercise/exercise.vcxproj.filters index 921adad..3b38d9f 100644 --- a/exercise/exercise.vcxproj.filters +++ b/exercise/exercise.vcxproj.filters @@ -18,6 +18,9 @@ Header Files + + Header Files + @@ -26,5 +29,8 @@ Source Files + + Source Files + \ No newline at end of file From ec0b0642b8cd78f9e2f9bb38f2c99deef7ca0a6b Mon Sep 17 00:00:00 2001 From: marasi Date: Mon, 19 Nov 2018 12:52:24 +0100 Subject: [PATCH 3/7] Command line parsing --- exercise/command.cpp | 167 ++++++++++++++++++++++++++++++ exercise/command.h | 68 ++++++++++++ exercise/exercise.cpp | Bin 1700 -> 494 bytes exercise/exercise.vcxproj | 2 + exercise/exercise.vcxproj.filters | 6 ++ exercise/pch.h | Bin 1262 -> 1392 bytes 6 files changed, 243 insertions(+) create mode 100644 exercise/command.cpp create mode 100644 exercise/command.h diff --git a/exercise/command.cpp b/exercise/command.cpp new file mode 100644 index 0000000..9b0b83d --- /dev/null +++ b/exercise/command.cpp @@ -0,0 +1,167 @@ +#include "pch.h" +#include "command.h" + +bool help::execute() +{ + 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; +} + +bool add_block::execute() +{ + return false; +} + +bool remove_block::execute() +{ + return false; +} + +bool front_block::execute() +{ + return false; +} + +bool print_seq::execute() +{ + return false; +} + +bool eval_value::execute() +{ + return false; +} + +bool eval_file::execute() +{ + return false; +} + +std::optional parse_int(std::string s) +{ + try + { + return std::stoi(s); + } + catch (...) + { + return std::nullopt; + } +} + +std::optional parse_double(std::string s) +{ + try + { + return std::stod(s); + } + catch (...) + { + return std::nullopt; + } +} + +std::unique_ptr create_add_block(int argc, char* argv[]) +{ + // get type + std::string types(argv[2]); + std::optional type = parse_int(types); + if(!type.has_value()) + { + return nullptr; + } + + // get parameters + std::string parameters(argv[3]); + for (int i = 4; 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 + std::string indexs(argv[2]); + std::optional index = parse_int(indexs); + return index.has_value() ? std::make_unique(index.value()) : nullptr; +} + +std::unique_ptr create_front_block(char* argv[]) +{ + // get index + std::string indexs(argv[2]); + std::optional index = parse_int(indexs); + return index.has_value() ? std::make_unique(index.value()) : nullptr; +} + +std::unique_ptr create_eval_value(char* argv[]) +{ + // get value + std::string values(argv[2]); + std::optional value = parse_double(values); + 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 >= 4 && 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..7cab976 --- /dev/null +++ b/exercise/command.h @@ -0,0 +1,68 @@ +#pragma once +class command +{ +public: + virtual bool execute() = 0; +}; + +class help : public command +{ +public: + bool execute() 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() override; +}; + +class remove_block : public command +{ +private: + int index; +public: + remove_block(int index) : index(index) {}; + bool execute() override; +}; + +class front_block : public command +{ +private: + int index; +public: + front_block(int index) : index(index) {}; + bool execute() override; +}; + +class print_seq : public command +{ +public: + bool execute() override; +}; + +class eval_value : public command +{ +private: + double value; +public: + eval_value(double value) : value(value) {}; + bool execute() 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() override; +}; + +std::unique_ptr create_command(int argc, char* argv[]); + diff --git a/exercise/exercise.cpp b/exercise/exercise.cpp index 0b13bf3971924035b013e700a99a55c261c8bf94..271eff4ff48f7c113a307e03822d651431a8771d 100644 GIT binary patch literal 494 zcmaiw%ZkE45Jm4+!GFl;CJ`0gI_i&zNG9JJ9_oX_lx@xNGlP1zuYa*mOe$txWh#4fS+Sz|(LZw>uKor6lI9c^shRN4=<}AllY~Sh11bc$1zarHm)(q!Y>(NuOFvIG<~!j&abMZg7B=8y zicVsG={Gxey*l0DbZkrR3vRDmqs!SRJXKb*4v@{>3na5tvI}gLYuf{C3|&&+#@pPn z-nV;1(KCHDAG%%v(k0SD+aZ<$d|hVeA2R!num*#y3faNhqGDWNCr9?k9{b%7UlTVP z(jCIsp0lH9lI=6(|2;M*jEZ09taJ85dthI&+_Bf33i;2q$H>cPXD@+%lQHGwt>c`q zd%`@jT~=95_6|=M=#}gbm}f{1xDlFjVpN_g=Xz5%xR%Z}`cv*RtD917Nbc&p!PV}o z@z)3C%(FaR#dHj&1w(ld#wMnbGal>(XUfDgGM(%THkAEbnP*s2O*B<@NZrpoE6Tap zyKx^wByU_(`5NG3NPQ{$Q)cN_b1&2zOv(52R^NSeqT27~Gx}?amU>4q2uaPl1ZUw0 zau2FDCR-?8w4ylm=x4sR$129enJP%VuC9w}$y&J=s}-+eO*K<9f}3qdUbbR6Bn}~$ z{s(-id)hI5=XT`zP>w=p+sCf^IL2P)DqfjyXaryP;xzsf#PI3bmVRw>lWRv+CY@Z{ Ofv$_^iQ~vTE22Mm@D&pP diff --git a/exercise/exercise.vcxproj b/exercise/exercise.vcxproj index 6a9e6b0..4f6d1ae 100644 --- a/exercise/exercise.vcxproj +++ b/exercise/exercise.vcxproj @@ -156,10 +156,12 @@ + + Create diff --git a/exercise/exercise.vcxproj.filters b/exercise/exercise.vcxproj.filters index 3b38d9f..3d09aae 100644 --- a/exercise/exercise.vcxproj.filters +++ b/exercise/exercise.vcxproj.filters @@ -21,6 +21,9 @@ Header Files + + Header Files + @@ -32,5 +35,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/exercise/pch.h b/exercise/pch.h index d87432d37058f4909a1979f297c238023153b462..16ebcd2049cfb844e908e3e94adc81006e1e0c10 100644 GIT binary patch delta 267 zcmaFI`GIRg6idA_LncEWLo!1SLn%WFLn?y;gAGF>keAMo&rk%ED`CiB$Yro&;AP-q zz@|16sIC~OstBkz5wFTzpo(0usg(%x>M<=4{mL#zckKOJEphIwT` YYm Date: Wed, 21 Nov 2018 14:29:44 +0100 Subject: [PATCH 4/7] Sequence logic --- exercise/block.cpp | 105 +++++++++++++++++++++--- exercise/block.h | 24 ++++-- exercise/command.cpp | 63 ++++++++------ exercise/command.h | 18 ++-- exercise/exercise.cpp | Bin 494 -> 518 bytes exercise/exercise.vcxproj | 2 + exercise/exercise.vcxproj.filters | 6 ++ exercise/pch.h | Bin 1392 -> 1512 bytes exercise/sequence.cpp | 132 ++++++++++++++++++++++++++++++ exercise/sequence.h | 21 +++++ 10 files changed, 319 insertions(+), 52 deletions(-) create mode 100644 exercise/sequence.cpp create mode 100644 exercise/sequence.h diff --git a/exercise/block.cpp b/exercise/block.cpp index 227d0b5..3a69976 100644 --- a/exercise/block.cpp +++ b/exercise/block.cpp @@ -1,6 +1,17 @@ #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() { } @@ -10,29 +21,65 @@ 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, min, max); + return std::clamp(in, minimum, maximum); +} + +void condition::print(std::ostream& os) const +{ + os << block_type::condition << " " << constant << " "; } double condition::output() const @@ -51,22 +98,54 @@ double condition::output() const } } -std::unique_ptr create_block(int type, const std::vector& block_parameters) +std::unique_ptr create_block(int type, std::istream& stream) { switch (type) { - case 1: + case block_type::identity: + { return std::make_unique(); - case 2: - return std::make_unique(block_parameters[0]); - case 3: - return std::make_unique(block_parameters[0]); - case 4: - return std::make_unique(block_parameters[0]); - case 5: - return std::make_unique(block_parameters[0], block_parameters[1]); - case 6: - return std::make_unique(block_parameters[0]); + } + 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 index eccc9df..cef742e 100644 --- a/exercise/block.h +++ b/exercise/block.h @@ -4,11 +4,13 @@ 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 @@ -22,6 +24,8 @@ class block_with_constant : public block // returns input class identity : public block { +protected: + void print(std::ostream& os) const override; public: double output() const override; }; @@ -29,6 +33,8 @@ class identity : public block // 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; @@ -37,6 +43,8 @@ class addition : public block_with_constant // 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; @@ -45,6 +53,8 @@ class multiplication : public block_with_constant // 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; @@ -54,20 +64,24 @@ class power : public block_with_constant class limit : public block { private: - double min; - double max; + double minimum; + double maximum; +protected: + void print(std::ostream& os) const override; public: - limit(double min = 0, double max = 0) {} + 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; }; -std::unique_ptr create_block(int type, const std::vector& block_parameters); - +// factory function +std::unique_ptr create_block(int type, std::istream& stream); diff --git a/exercise/command.cpp b/exercise/command.cpp index 9b0b83d..5aace30 100644 --- a/exercise/command.cpp +++ b/exercise/command.cpp @@ -1,7 +1,12 @@ #include "pch.h" #include "command.h" +#include "sequence.h" -bool help::execute() +command::~command() +{ +} + +bool help::execute() const { std::cout << "Sequence Help" << std::endl @@ -16,37 +21,43 @@ bool help::execute() return true; } -bool add_block::execute() +bool add_block::execute() const { - return false; + sequence s; + return s.add(type, parameters); } -bool remove_block::execute() +bool remove_block::execute() const { - return false; + sequence s; + return s.remove(index); } -bool front_block::execute() +bool front_block::execute() const { - return false; + sequence s; + return s.front(index); } -bool print_seq::execute() +bool print_seq::execute() const { - return false; + sequence s; + return s.print(); } -bool eval_value::execute() +bool eval_value::execute() const { - return false; + sequence s; + return s.eval(value); } -bool eval_file::execute() +bool eval_file::execute() const { - return false; + sequence s; + return s.eval(input, output); } -std::optional parse_int(std::string s) +std::optional parse_int(const std::string& s) { try { @@ -58,7 +69,7 @@ std::optional parse_int(std::string s) } } -std::optional parse_double(std::string s) +std::optional parse_double(const std::string& s) { try { @@ -73,16 +84,16 @@ std::optional parse_double(std::string s) std::unique_ptr create_add_block(int argc, char* argv[]) { // get type - std::string types(argv[2]); - std::optional type = parse_int(types); + 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(argv[3]); - for (int i = 4; i < argc; i++) + std::string parameters; + for (int i = 3; i < argc; i++) { parameters.append(" "); parameters.append(argv[i]); @@ -94,24 +105,24 @@ std::unique_ptr create_add_block(int argc, char* argv[]) std::unique_ptr create_remove_block(char* argv[]) { // get index - std::string indexs(argv[2]); - std::optional index = parse_int(indexs); + 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 - std::string indexs(argv[2]); - std::optional index = parse_int(indexs); + 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 - std::string values(argv[2]); - std::optional value = parse_double(values); + const std::string value_s(argv[2]); + std::optional value = parse_double(value_s); return value.has_value() ? std::make_unique(value.value()) : nullptr; } @@ -136,7 +147,7 @@ std::unique_ptr create_command(int argc, char* argv[]) { return std::make_unique(); } - else if (argc >= 4 && command.compare("--add") == 0) + else if (argc >= 3 && command.compare("--add") == 0) { return create_add_block(argc, argv); } diff --git a/exercise/command.h b/exercise/command.h index 7cab976..1196fee 100644 --- a/exercise/command.h +++ b/exercise/command.h @@ -1,14 +1,16 @@ #pragma once + class command { public: - virtual bool execute() = 0; + virtual bool execute() const = 0; + virtual ~command() = 0; }; class help : public command { public: - bool execute() override; + bool execute() const override; }; class add_block : public command @@ -18,7 +20,7 @@ class add_block : public command std::string parameters; public: add_block(int type, std::string parameters) : type(type), parameters(parameters) {}; - bool execute() override; + bool execute() const override; }; class remove_block : public command @@ -27,7 +29,7 @@ class remove_block : public command int index; public: remove_block(int index) : index(index) {}; - bool execute() override; + bool execute() const override; }; class front_block : public command @@ -36,13 +38,13 @@ class front_block : public command int index; public: front_block(int index) : index(index) {}; - bool execute() override; + bool execute() const override; }; class print_seq : public command { public: - bool execute() override; + bool execute() const override; }; class eval_value : public command @@ -51,7 +53,7 @@ class eval_value : public command double value; public: eval_value(double value) : value(value) {}; - bool execute() override; + bool execute() const override; }; class eval_file : public command @@ -61,7 +63,7 @@ class eval_file : public command std::string output; public: eval_file(std::string input, std::string output) : input(input), output(output) {}; - bool execute() override; + bool execute() const override; }; std::unique_ptr create_command(int argc, char* argv[]); diff --git a/exercise/exercise.cpp b/exercise/exercise.cpp index 271eff4ff48f7c113a307e03822d651431a8771d..d110b80371ee76e05d8a34b9e0ff15f3e1ab8556 100644 GIT binary patch delta 37 jcmaFI+{Q9t5qB~}K0_WuF+&N1!o-C;k-3v28K(jO*BuJK delta 13 VcmZo;dB;3q(ZqE-CKoYI0RSsr20s7* diff --git a/exercise/exercise.vcxproj b/exercise/exercise.vcxproj index 4f6d1ae..5c3cb23 100644 --- a/exercise/exercise.vcxproj +++ b/exercise/exercise.vcxproj @@ -158,6 +158,7 @@ + @@ -169,6 +170,7 @@ Create Create + diff --git a/exercise/exercise.vcxproj.filters b/exercise/exercise.vcxproj.filters index 3d09aae..b76f1f2 100644 --- a/exercise/exercise.vcxproj.filters +++ b/exercise/exercise.vcxproj.filters @@ -24,6 +24,9 @@ Header Files + + Header Files + @@ -38,5 +41,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/exercise/pch.h b/exercise/pch.h index 16ebcd2049cfb844e908e3e94adc81006e1e0c10..4eb28f2725001709db4ec1029e4a98c4ec8d5010 100644 GIT binary patch delta 43 ycmeys^@4lDEEdMJ$#YrMC(mN3nLLk0ZL$cf8gD5>2}34B4iJ|xR8E#>4Fmv3> type) + { + blocks.push_back(create_block(type, fs)); + } + + fs.close(); +} + +sequence::~sequence() +{ + std::ofstream fs(file); + for (auto it = blocks.begin(); it != blocks.end(); ++it) + { + fs << **it; + } + fs.close(); +} + +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(int index) const +{ + return 0 < index && index < blocks.size() - 1; +} + +bool sequence::remove(int 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(int 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() const +{ + for (auto it = blocks.begin(); it != blocks.end(); ++it) + { + std::cout << **it << std::endl; + } + + 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) const +{ + bool result = false; + + if (blocks.size() > 0) + { + std::cout << eval_impl(value); + result = true; + } + + return result; +} + +bool sequence::eval(const std::string& input, const std::string& output) const +{ + bool result = false; + + if (blocks.size() > 0) + { + std::ifstream file_in(input); + std::ofstream file_out(output); + + double value; + while (file_in >> value && file_out << eval_impl(value) << " ") {} + + file_in.close(); + file_out.close(); + + 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..1d199f4 --- /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(int index) const; + double eval_impl(double) const; +public: + sequence(std::string file = "local.txt"); + ~sequence(); + bool add(int type, const std::string& parameters); + bool remove(int index); + bool front(int index); + bool print() const; + bool eval(double value) const; + bool eval(const std::string& input, const std::string& output) const; +}; From 1ed89ef7dabd7b253181e37bbba27589c8a43cb1 Mon Sep 17 00:00:00 2001 From: marasi Date: Thu, 22 Nov 2018 16:09:10 +0100 Subject: [PATCH 5/7] Created test project --- exercise.sln | 10 +++ test/block_test.cpp | 21 +++++ test/stdafx.cpp | 8 ++ test/stdafx.h | 13 +++ test/targetver.h | 8 ++ test/test.vcxproj | 183 ++++++++++++++++++++++++++++++++++++++ test/test.vcxproj.filters | 33 +++++++ 7 files changed, 276 insertions(+) create mode 100644 test/block_test.cpp create mode 100644 test/stdafx.cpp create mode 100644 test/stdafx.h create mode 100644 test/targetver.h create mode 100644 test/test.vcxproj create mode 100644 test/test.vcxproj.filters diff --git a/exercise.sln b/exercise.sln index 9df518c..05b3067 100644 --- a/exercise.sln +++ b/exercise.sln @@ -5,6 +5,8 @@ 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 @@ -21,6 +23,14 @@ Global {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 diff --git a/test/block_test.cpp b/test/block_test.cpp new file mode 100644 index 0000000..6c6b6c0 --- /dev/null +++ b/test/block_test.cpp @@ -0,0 +1,21 @@ +#include "stdafx.h" +#include "../exercise/block.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace test +{ + TEST_CLASS(block_test) + { + public: + TEST_METHOD(test_identity) + { + identity block; + block.input(4.5); + + double out = block.output(); + + Assert::AreEqual(4.5, out); + } + }; +} \ 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..43280fc --- /dev/null +++ b/test/stdafx.h @@ -0,0 +1,13 @@ +// 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" + +// 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..e0d2f2c --- /dev/null +++ b/test/test.vcxproj @@ -0,0 +1,183 @@ + + + + + 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 + + + true + + + true + + + true + + + + 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..11fd339 --- /dev/null +++ b/test/test.vcxproj.filters @@ -0,0 +1,33 @@ + + + + + {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 + + + \ No newline at end of file From e1bb51b886e620df811377530e951be35df98ccf Mon Sep 17 00:00:00 2001 From: imaras Date: Thu, 22 Nov 2018 22:11:58 +0100 Subject: [PATCH 6/7] Block tests --- exercise/sequence.cpp | 6 +- exercise/sequence.h | 8 +- test/block_test.cpp | 187 +++++++++++++++++++++++++++++++++++++++++- test/stdafx.h | 1 + test/test.vcxproj | 4 + 5 files changed, 196 insertions(+), 10 deletions(-) diff --git a/exercise/sequence.cpp b/exercise/sequence.cpp index 9cdd6f8..216775e 100644 --- a/exercise/sequence.cpp +++ b/exercise/sequence.cpp @@ -39,12 +39,12 @@ bool sequence::add(int type, const std::string& parameters) return result; } -bool sequence::check_index(int index) const +bool sequence::check_index(size_t index) const { return 0 < index && index < blocks.size() - 1; } -bool sequence::remove(int index) +bool sequence::remove(size_t index) { bool result = false; @@ -59,7 +59,7 @@ bool sequence::remove(int index) return result; } -bool sequence::front(int index) +bool sequence::front(size_t index) { bool result = false; diff --git a/exercise/sequence.h b/exercise/sequence.h index 1d199f4..da6adb1 100644 --- a/exercise/sequence.h +++ b/exercise/sequence.h @@ -5,16 +5,16 @@ class sequence { private: - std::string file{}; + std::string file; std::vector > blocks; - bool check_index(int index) const; + bool check_index(size_t index) const; double eval_impl(double) const; public: sequence(std::string file = "local.txt"); ~sequence(); bool add(int type, const std::string& parameters); - bool remove(int index); - bool front(int index); + bool remove(size_t index); + bool front(size_t index); bool print() const; bool eval(double value) const; bool eval(const std::string& input, const std::string& output) const; diff --git a/test/block_test.cpp b/test/block_test.cpp index 6c6b6c0..6e75358 100644 --- a/test/block_test.cpp +++ b/test/block_test.cpp @@ -8,14 +8,195 @@ namespace test TEST_CLASS(block_test) { public: - TEST_METHOD(test_identity) + 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/stdafx.h b/test/stdafx.h index 43280fc..248d05a 100644 --- a/test/stdafx.h +++ b/test/stdafx.h @@ -9,5 +9,6 @@ // Headers for CppUnitTest #include "CppUnitTest.h" +#include // TODO: reference additional headers your program requires here diff --git a/test/test.vcxproj b/test/test.vcxproj index e0d2f2c..c415197 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -77,15 +77,19 @@ true + $(ProjectDir)$(Configuration)\ true + $(ProjectDir)$(Platform)\$(Configuration)\ true + $(ProjectDir)$(Configuration)\ true + $(ProjectDir)$(Platform)\$(Configuration)\ From 6e7e822be104f1cc7c21fc1a7a066872ad199358 Mon Sep 17 00:00:00 2001 From: imaras Date: Sun, 25 Nov 2018 15:36:47 +0100 Subject: [PATCH 7/7] Sequence tests --- exercise/command.cpp | 23 +++++++---- exercise/pch.h | Bin 1512 -> 1472 bytes exercise/sequence.cpp | 27 +++++------- exercise/sequence.h | 8 ++-- test/sequence_test.cpp | 84 ++++++++++++++++++++++++++++++++++++++ test/stdafx.h | 2 + test/test.vcxproj | 1 + test/test.vcxproj.filters | 3 ++ 8 files changed, 118 insertions(+), 30 deletions(-) create mode 100644 test/sequence_test.cpp diff --git a/exercise/command.cpp b/exercise/command.cpp index 5aace30..f838001 100644 --- a/exercise/command.cpp +++ b/exercise/command.cpp @@ -21,40 +21,45 @@ bool help::execute() const return true; } +const std::string file_name = "local.txt"; + bool add_block::execute() const { - sequence s; + sequence s(file_name); return s.add(type, parameters); } bool remove_block::execute() const { - sequence s; + sequence s(file_name); return s.remove(index); } bool front_block::execute() const { - sequence s; + sequence s(file_name); return s.front(index); } bool print_seq::execute() const { - sequence s; - return s.print(); + sequence s(file_name); + return s.print(std::cout); } bool eval_value::execute() const { - sequence s; - return s.eval(value); + sequence s(file_name); + return s.eval(value, std::cout); } bool eval_file::execute() const { - sequence s; - return s.eval(input, output); + 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) diff --git a/exercise/pch.h b/exercise/pch.h index 4eb28f2725001709db4ec1029e4a98c4ec8d5010..9f458574e990b33104f1443ad1a0d0b7951cd330 100644 GIT binary patch delta 11 ScmaFCeSmvI7whCrtY!cnPy~Je delta 29 jcmX@W{epW#7b|ZmLkUABLk 0) { - std::cout << eval_impl(value); + os << eval_impl(value); result = true; } return result; } -bool sequence::eval(const std::string& input, const std::string& output) const +bool sequence::eval(std::istream& is, std::ostream& os) const { bool result = false; if (blocks.size() > 0) { - std::ifstream file_in(input); - std::ofstream file_out(output); - double value; - while (file_in >> value && file_out << eval_impl(value) << " ") {} - - file_in.close(); - file_out.close(); - + while (is >> value) + { + os << eval_impl(value) << " "; + } result = true; } diff --git a/exercise/sequence.h b/exercise/sequence.h index da6adb1..073e5ae 100644 --- a/exercise/sequence.h +++ b/exercise/sequence.h @@ -10,12 +10,12 @@ class sequence bool check_index(size_t index) const; double eval_impl(double) const; public: - sequence(std::string file = "local.txt"); + 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() const; - bool eval(double value) const; - bool eval(const std::string& input, const std::string& output) const; + 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/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.h b/test/stdafx.h index 248d05a..68eb05d 100644 --- a/test/stdafx.h +++ b/test/stdafx.h @@ -9,6 +9,8 @@ // Headers for CppUnitTest #include "CppUnitTest.h" +#include #include +#include // TODO: reference additional headers your program requires here diff --git a/test/test.vcxproj b/test/test.vcxproj index c415197..1bcc276 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -168,6 +168,7 @@ + Create Create diff --git a/test/test.vcxproj.filters b/test/test.vcxproj.filters index 11fd339..79f34d0 100644 --- a/test/test.vcxproj.filters +++ b/test/test.vcxproj.filters @@ -29,5 +29,8 @@ Source Files + + Source Files + \ No newline at end of file