///////////////////////////////////////////////////////////////////////////// // TestHarness.cpp - Declares test aggregator and test functor base class // // ver 1.0 // // // // Language: Visual C++, ver 7.0 // // Platform: Dell Dimension 8300, Windows XP Prof, SP 1 // // Application: CSE687 prototype for project #1, Spring 2004 // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////////////// #include #include #include #include #include "testHarness.h" using namespace TestHarness; //----< display title with test count >-------------------------------- unsigned int test::count = 0; void test::title(const std::string& msg) { tout.write(); tout << " test #" << ++count << ": " << msg; size_t len = tout.str().size(); tout << "\n " << std::string(len, '-') << std::endl; tout.write(); } //----< constructor takes title string for initial display >----------- tester::tester(output& out, const std::string& msg) { out << "\n " << msg; out << "\n " << std::string(msg.length()+2,'=') << "\n\n"; out.write(); } //----< put a test reference into container >-------------------------- void tester::registerTest(test *tf) { _test.push_back(tf); } //----< execute all tests in container >------------------------------- bool tester::execute(output& out) { failed = 0; for(int i=0; i<(int)_test.size(); i++) { if(!(*_test[i])()) failed++; } out << "\n" << std::setw(4) << (unsigned)_test.size() << " tests executed"; out << "\n" << std::setw(4) << failed << " tests failed\n\n"; return (failed == 0); } // #ifdef TEST_TESTHARNESS class test1 : public test { public: test1(output& out) : test(out) {} bool operator()() { title("passed"); return true; } }; class test2 : public test { public: test2(output& out) : test(out) {} bool operator()() { title("failed"); return false; } }; class test3 : public test { public: test3(output& out) : test(out) {} bool operator()() { title("show output"); typedef std::vector vec; vec v; v.push_back("aardvark"); v.push_back("mule"); v.push_back("zebra"); showCont show; show(v); } }; #include int main() { stdOutput out; tester t1(out, "Testing tester, test, and output"); t1.registerTest(new test1(out)); t1.registerTest(new test2(out)); t1.registerTest(new test3(out)); t1.execute(out); nullOutput null; tester t2(out, "Testing null output tests"); t2.registerTest(new test1(null)); t2.registerTest(new test2(null)); t2.execute(out); } #endif