FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
April 01, 2001

Adding Exception Testing to Unit Tests

(Page 3 of 10)
April 2001/Adding Exception Testing to Unit Tests/Listing 2

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 —
Previous Page | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 Next Page
RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK