Listing 2: Implementation of Counter class
#include "Counter.h" #include <new> #include <cstdlib> #include <cassert> using namespace std; int Counter::sExceptionPointCount = 0; int Counter::sThrowCount = -1; bool Counter::sDontThrow = false; int Counter::sNewCount = 0; int Counter::sPassCount = 0; int Counter::sFailCount = 0; ostream* Counter::mOs = &cout; void Counter::CouldThrow() throw ( TestException ) { if( !sDontThrow && ++sExceptionPointCount == sThrowCount ) { throw TestException(); } } void Counter::SetThrowCount( int inCount ) throw() { sExceptionPointCount = 0; sThrowCount = inCount; sDontThrow = false; } void Counter::DontThrow() throw() { sDontThrow = false; } bool Counter::HasThrown() throw() { return sExceptionPointCount >= sThrowCount; } int Counter::GetAllocationCount() throw() { return sNewCount; } void Counter::Pass( const char* testName ) throw() { ++sPassCount; //(*mOs) << " Passed test " << testName << endl; } void Counter::Fail( const char* testName ) throw() { ++sFailCount; (*mOs) << "**** Failed test " << testName << " at exception count " << sThrowCount << "." << endl; } void Counter::Test( bool result, const char* testName ) throw() { if( result ) { Pass( testName ); } else { Fail( testName ); } } void Counter::PrintTestSummary() { (*mOs) << "Test Results:" << std::endl; (*mOs) << "Total Tests: " << sPassCount + sFailCount << std::endl; (*mOs) << "Passed : " << sPassCount << std::endl; (*mOs) << "Failed : " << sFailCount << std::endl; } // not shown: debugging // memory manager -- available in // online version of this listing // ... End of Listing