#ifndef TEST_INCLUDED #define TEST_INCLUDED #include #include #include #include namespace test // test tuple comprises name and nullary function (object) { template struct test { string name; function action; static test make(string name, function action) { test result; // aggreggate initializer compiler bugs result.name = name; result.action = action; return result; } }; } namespace test // failure exception used to indicate checked test failures { class failure : public std::exception { public: // struction (default cases are OK) failure(const std::string &why) : reason(why) { } public: // usage virtual const char *what() const { return reason.c_str(); } private: // representation std::string reason; }; } namespace test // not_implemented exception used to mark unimplemented tests { class not_implemented : public std::exception { public: // usage (default ctor and dtor are OK) virtual const char *what() const { return "not implemented"; } }; } namespace test // test utilities { inline void check(bool condition, const std::string &description) { if(!condition) { throw failure(description); } } inline void check_true(bool value, const std::string &description) { check(value, "expected true: " + description); } inline void check_false(bool value, const std::string &description) { check(!value, "expected false: " + description); } template void check_equal( const lhs_type &lhs, const rhs_type &rhs, const std::string &description) { check(lhs == rhs, "expected equal values: " + description); } template void check_unequal( const lhs_type &lhs, const rhs_type &rhs, const std::string &description) { check(lhs != rhs, "expected unequal values: " + description); } inline void check_null(const void *ptr, const std::string &description) { check(!ptr, "expected null pointer: " + description); } inline void check_non_null(const void *ptr, const std::string &description) { check(ptr, "expected non-null pointer: " + description); } } #define TEST_CHECK_THROW(expression, exception, description) \ try \ { \ expression; \ throw ::test::failure(description); \ } \ catch(exception &) \ { \ } namespace test // memory tracking (enabled if test new and delete linked in) { class allocations { public: // singleton access static allocations &instance() { static allocations singleton; return singleton; } public: // logging void clear() { alloc_count = dealloc_count = 0; } void allocation() { ++alloc_count; } void deallocation() { ++dealloc_count; } public: // reporting unsigned long allocated() const { return alloc_count; } unsigned long deallocated() const { return dealloc_count; } bool balanced() const { return alloc_count == dealloc_count; } private: // structors (default dtor is fine) allocations() : alloc_count(0), dealloc_count(0) { } private: // prevention allocations(const allocations &); allocations &operator=(const allocations &); private: // state unsigned long alloc_count, dealloc_count; }; } namespace test // tester is the driver class for a sequence of tests { template class tester { public: // structors (default destructor is OK) tester(test_iterator first_test, test_iterator after_last_test) : begin(first_test), end(after_last_test) { } public: // usage bool operator()(); // returns true if all tests passed private: // representation test_iterator begin, end; private: // prevention tester(const tester &); tester &operator=(const tester &); }; template bool tester::operator()() { using namespace std; unsigned long passed = 0, failed = 0, unimplemented = 0; for(test_iterator current = begin; current != end; ++current) { cerr << "[" << current->name << "] " << flush; string result = "passed"; // optimistic try { allocations::instance().clear(); current->action(); if(!allocations::instance().balanced()) { unsigned long allocated = allocations::instance().allocated(); unsigned long deallocated = allocations::instance().deallocated(); ostringstream report; report << "new/delete (" << allocated << " allocated, " << deallocated << " deallocated)"; throw failure(report.str()); } ++passed; } catch(const failure &caught) { (result = "failed: ") += caught.what(); ++failed; } catch(const not_implemented &) { result = "not implemented"; ++unimplemented; } catch(const exception &caught) { (result = "exception: ") += caught.what(); ++failed; } catch(...) { result = "failed with unknown exception"; ++failed; } cerr << result << endl; } cerr << passed + failed << " tests: " << passed << " passed, " << failed << " failed"; if(unimplemented) { cerr << " (" << unimplemented << " not implemented)"; } cerr << endl; return failed == 0; } } #endif