#pragma once ///////////////////////////////////////////////////////////////////// // CodeUtil_Tests.h - Demonstrate CodeUtilities operations // // ver 1.1 using single-user TestHarness // // // // Jim Fawcett, CSE687-Object Oriented Design, Fall 2018 // ///////////////////////////////////////////////////////////////////// /* * Package Operations: * ------------------- * CodeUtil_Tests provides four tests, one for each class or, in some * cases, part of a class. These run in the single-user TestHarness, * which Project #1 develops. * * All of the CodeUtility tests are defined in this CodeUtil_Tests.h file. * They are executed in the main function in Proj1_TestDriver.cpp. * * Notes: * - Each test contains, in comments, a Test Description and Test * Procedure, intended to be a sample of how tests are documented. * - This package uses the single-user TestHarness, defined in * TestUtilities, to test itself. * - Tests use Singleton Logger directly, and so, are bound to its * implementation details. See Proj1Reqs_Tests.h to see alternate * strategy. * * Required Files: * --------------- * CodeUtil_Tests.h, Proj1_TestDriver.cpp * ITest.h * SingletonLogger.h * * Maintenance History: * -------------------- * ver 1.1 : 02 Oct 2018 * - added include for ITest * - eliminated include of TestUtilitiesDemo.h * ver 1.0 : 23 Sep 2018 * - first release */ #include "../CodeUtilities/CodeUtilities.h" #include "../SingletonLogger/SingletonLogger/SingletonLogger.h" #include "../TestUtilities/ITest.h" namespace UtilitiesTest { using Logger = CodeUtilities::Logger<1, CodeUtilities::NoLock>; class TestReq9a : public Utilities::ITest { /* * Test Description: * Demonstrates normal operation of Utilities::ProcessCmdLine class * Test Procedure: * - Construct typical command line and construct instance of * ProcessCmdLine with that. * - Show what it returns. */ private: const std::string path_; public: TestReq9a(const std::string& path) : path_(path) {} ~TestReq9a() { std::cout << "\n deleting TestReq9a"; } bool test() { Logger* pLog = Logger::getInstance(); Message msg = "\n Req #9a - Testing CodeUtilities::ProcessCmdLine - normal conditions"; pLog->write(msg); msg = "------------------------------------------------------------------------"; pLog->write(msg); std::string cmdLine = "../.. /s /r *.h *.cpp 50"; const int demoArgc = 7; char* demoArgv[demoArgc]; demoArgv[0] = const_cast("DemoReqs.exe"); demoArgv[1] = const_cast("../.."); demoArgv[2] = const_cast("/s"); demoArgv[3] = const_cast("/r"); demoArgv[4] = const_cast("*.h"); demoArgv[5] = const_cast("*.cpp"); demoArgv[6] = const_cast("50"); std::ostringstream out; Utilities::ProcessCmdLine pcl(demoArgc, demoArgv, out); pLog->write("input command line:"); pLog->write(cmdLine); pLog->write("processed command line:"); pcl.showCmdLine(); pLog->write(out.str()); out.str(""); pLog->write("path = " + pcl.path()); out.str(""); std::string temp = "options = "; pcl.showOptions(); pLog->write(temp + out.str()); out.str(""); temp = "patterns = "; pcl.showPatterns(); pLog->write(temp + out.str()); out.str(""); temp = "maxItems = "; pcl.showMaxItems(); pLog->write(temp + out.str()); pLog->write(" "); cmdLine = "../.. /s *.*"; const int demoArgc2 = 4; demoArgv[0] = const_cast("DemoReqs.exe"); demoArgv[1] = const_cast("../.."); demoArgv[2] = const_cast("/s"); demoArgv[3] = const_cast("*.*"); out.str(""); Utilities::ProcessCmdLine pcl2(demoArgc2, demoArgv, out); pLog->write("input command line:"); pLog->write(cmdLine); pLog->write("processed command line:"); pcl2.showCmdLine(); pLog->write(out.str()); out.str(""); pLog->write("path = " + pcl.path()); out.str(""); temp = "options = "; pcl2.showOptions(); pLog->write(temp + out.str()); out.str(""); temp = "patterns = "; pcl2.showPatterns(); pLog->write(temp + out.str()); out.str(""); temp = "maxItems = "; pcl2.showMaxItems(); pLog->write(temp + out.str()); out.str(""); pLog->writeTail("This is the end of log\n"); return true; } }; class TestReq9b : public Utilities::ITest { /* * Test Description: * Demonstrate ProcessCmdLine class * Test Procedure: * - Constructs ProcessCmdLine instance with abnormal arguments * - Demonstates that pcl instance does not crash, at least for * these inputs. * - Illustrates things that need to be cleaned up to make the * class robust against user errors. */ private: const std::string path_; public: TestReq9b(const std::string& path) : path_(path) {} ~TestReq9b() { std::cout << "\n deleting TestReq9b"; } bool test() { Logger* pLog = Logger::getInstance(); Message msg = "\n Req #9b - Testing CodeUtilities::ProcessCmdLine - abnormal conditions"; pLog->write(msg); msg = "------------------------------------------------------------------------"; pLog->write(msg); std::string cmdLine = "../ /z / *.h * -50"; const int demoArgc = 7; char* demoArgv[demoArgc]; demoArgv[0] = const_cast("DemoReqs.exe"); demoArgv[1] = const_cast("../"); demoArgv[2] = const_cast("/z"); demoArgv[3] = const_cast("/"); demoArgv[4] = const_cast("*.h"); demoArgv[5] = const_cast("*"); demoArgv[6] = const_cast("-50"); std::ostringstream out; Utilities::ProcessCmdLine pcl(demoArgc, demoArgv, out); pLog->write("input command line:"); pLog->write(cmdLine); pLog->write("processed command line:"); pcl.showCmdLine(); pLog->write(out.str()); out.str(""); pLog->write("path = " + pcl.path()); out.str(""); std::string temp = "options = "; pcl.showOptions(); pLog->write(temp + out.str()); out.str(""); temp = "patterns = "; pcl.showPatterns(); pLog->write(temp + out.str()); out.str(""); temp = "maxItems = "; pcl.showMaxItems(); pLog->write(temp + out.str()); out.str(""); cmdLine = "/z *.h -50"; const int demoArgc2 = 4; demoArgv[0] = const_cast("DemoReqs.exe"); demoArgv[1] = const_cast("/z"); demoArgv[2] = const_cast("*.h"); demoArgv[3] = const_cast("50"); out.str(""); Utilities::ProcessCmdLine pcl2(demoArgc2, demoArgv, out); pLog->write("\n input command line:"); pLog->write(cmdLine); pLog->write("processed command line:"); pcl2.showCmdLine(); pLog->write(out.str()); out.str(""); pLog->write("path = " + pcl2.path()); out.str(""); temp = "options = "; pcl2.showOptions(); pLog->write(temp + out.str()); out.str(""); temp = "patterns = "; pcl2.showPatterns(); pLog->write(temp + out.str()); out.str(""); temp = "maxItems = "; pcl2.showMaxItems(); pLog->write(temp + out.str()); out.str(""); pLog->write("\n These outputs help us to make changes to improve robustness of ProcessCmdLine"); pLog->writeTail("This is the end of log\n"); return true; } }; class TestReq9c : public Utilities::ITest { /* * Test Description: * Demonstrates Converter class in CodeUtilities. * Test Procedure: * - Converter - Convert from size_t to string and back again. */ private: const std::string path_; public: TestReq9c(const std::string& path) : path_(path) {} ~TestReq9c() { std::cout << "\n deleting TestReq9c"; } bool test() { Logger* pLog = Logger::getInstance(); Message msg = "\n Req #9c - Testing Converter Class - basic functionality"; pLog->write(msg); msg = "------------------------------------------------------------------------"; pLog->write(msg); std::string t1 = Utilities::Converter::toString(42); msg = "converting size_t 42 to string: \"" + t1 + "\""; pLog->write(msg); size_t t2 = Utilities::Converter::toValue("24"); msg = "converting string \"24\" to size_t: " + Utilities::Converter::toString(t2); pLog->write(msg); return true; } }; class TestReq9d : public Utilities::ITest { /* * Test Description: * Demonstrates Box and PersistFactory classes in CodeUtilities. * Test Procedure: * - Construct Box and Box and show that they * behave like doubles and std::strings. * - Use PersistFactory to wrap a Boxed and a string in XML tags */ private: const std::string path_; public: TestReq9d(const std::string& path) : path_(path) {} ~TestReq9d() { std::cout << "\n deleting TestReq9d"; } bool test() { Logger* pLog = Logger::getInstance(); Message msg = "\n Req #9d - Testing Box and PersistFactory Classes - basic functionality"; pLog->write(msg); msg = "------------------------------------------------------------------------"; pLog->write(msg); Utilities::Box boxedDouble = 3.1415927; std::string temp = Utilities::Converter>::toString(boxedDouble); pLog->write("boxedDouble = " + temp); Utilities::PersistFactory name = std::string("Jim"); name.append(" Fawcett"); temp = name.toXml("name"); pLog->write(temp); Utilities::PersistFactory> value = Utilities::Box(9.87654); temp = value.toXml("value"); value += 3.14159; pLog->write(value.toXml("value")); return true; } }; }