Added more tests

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@901 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
rich_sposato 2008-10-07 04:46:34 +00:00
parent 3d855ff607
commit 5a243771f5

View file

@ -24,6 +24,7 @@
#include "../../include/loki/Checker.h" #include "../../include/loki/Checker.h"
#include <stdexcept> #include <stdexcept>
#include <iostream>
#include <vector> #include <vector>
@ -35,6 +36,8 @@
#define NULL 0 #define NULL 0
#endif #endif
using namespace std;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -124,7 +127,7 @@ void Thingy::ChangeThat( void )
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// This example shows how static functions can use an invariant checkers. // This example shows how static functions can use an invariant checker.
unsigned int Thingy::GetThat( void ) unsigned int Thingy::GetThat( void )
{ {
CheckStaticInvariants checker( &Thingy::StaticIsValid ); CheckStaticInvariants checker( &Thingy::StaticIsValid );
@ -279,6 +282,7 @@ void Thingy::ClearCounts( void )
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// This is a static validator.
bool Thingy::StaticIsValid( void ) bool Thingy::StaticIsValid( void )
{ {
assert( s_value != 0 ); assert( s_value != 0 );
@ -287,6 +291,7 @@ bool Thingy::StaticIsValid( void )
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// This is a per-instance validator.
bool Thingy::IsValid( void ) const bool Thingy::IsValid( void ) const
{ {
assert( nullptr != this ); assert( nullptr != this );
@ -339,18 +344,64 @@ void DoSomething( void )
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void ThrowTest( void )
{
Thingy thingy( 10 );
throw ::std::logic_error( "Will Thingy assert during an exception?" );
}
// ----------------------------------------------------------------------------
int main( unsigned int argc, const char * const argv[] ) int main( unsigned int argc, const char * const argv[] )
{ {
try
{
cout << "Just before call to ThrowTest." << endl;
ThrowTest();
cout << "Just after call to ThrowTest." << endl;
}
catch ( const ::std::logic_error & ex )
{
cout << "Caught an exception! " << ex.what() << endl;
}
catch ( const ::std::exception & ex )
{
cout << "Caught an exception! " << ex.what() << endl;
}
catch ( ... )
{
cout << "Caught an exception!" << endl;
}
unsigned int count = 0; unsigned int count = 0;
try try
{ {
cout << "Running basic tests with Thingy." << endl;
// First do some tests on class member functions. // First do some tests on class member functions.
Thingy t1( 1 ); Thingy t1( 1 );
t1.DoSomething( false ); t1.DoSomething( false );
Thingy t2( 2 ); Thingy t2( 2 );
t2.DoSomething( true ); t2.DoSomething( true );
cout << "Done with basic tests with Thingy." << endl;
}
catch ( const ::std::logic_error & ex )
{
cout << "Caught an exception! " << ex.what() << endl;
}
catch ( const ::std::exception & ex )
{
cout << "Caught an exception! " << ex.what() << endl;
}
catch ( ... )
{
cout << "Caught an exception!" << endl;
}
try
{
Thingy t1( 1 );
cout << "Now running tests with Thingy counts." << endl;
// These lines will exercise the functions with pre- and post-conditions. // These lines will exercise the functions with pre- and post-conditions.
t1.AddCount( 11 ); t1.AddCount( 11 );
t1.AddCount( 13 ); t1.AddCount( 13 );
@ -361,19 +412,60 @@ int main( unsigned int argc, const char * const argv[] )
count = t1.GetCount( 0 ); count = t1.GetCount( 0 );
assert( count == 11 ); assert( count == 11 );
t1.ClearCounts(); t1.ClearCounts();
cout << "Done with tests with Thingy counts." << endl;
}
catch ( const ::std::logic_error & ex )
{
cout << "Caught an exception! " << ex.what() << endl;
}
catch ( const ::std::exception & ex )
{
cout << "Caught an exception! " << ex.what() << endl;
}
catch ( ... )
{
cout << "Caught an exception!" << endl;
}
try
{
cout << "Now run tests on static member functions" << endl;
// Next do some tests with static member functions. // Next do some tests with static member functions.
Thingy::ChangeThat(); Thingy::ChangeThat();
const unsigned int value = Thingy::GetThat(); const unsigned int value = Thingy::GetThat();
assert( value != 0 ); assert( value != 0 );
cout << "Done with tests on static member functions" << endl;
// Then do a test with a standalone function. }
DoSomething(); catch ( const ::std::logic_error & ex )
{
cout << "Caught an exception! " << ex.what() << endl;
}
catch ( const ::std::exception & ex )
{
cout << "Caught an exception! " << ex.what() << endl;
} }
catch ( ... ) catch ( ... )
{ {
cout << "Caught an exception!" << endl;
} }
try
{
cout << "Now run test on a standalone function." << endl;
// Then do a test with a standalone function.
DoSomething();
cout << "Done with test on a standalone function." << endl;
}
catch ( const ::std::exception & ex )
{
cout << "Caught an exception! " << ex.what() << endl;
}
catch ( ... )
{
cout << "Caught an exception!" << endl;
}
cout << "All done!" << endl;
return 0; return 0;
} }