From: <ric...@us...> - 2008-10-07 04:50:55
|
Revision: 901 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=901&view=rev Author: rich_sposato Date: 2008-10-07 04:46:34 +0000 (Tue, 07 Oct 2008) Log Message: ----------- Added more tests Modified Paths: -------------- trunk/test/Checker/main.cpp Modified: trunk/test/Checker/main.cpp =================================================================== --- trunk/test/Checker/main.cpp 2008-08-23 07:08:22 UTC (rev 900) +++ trunk/test/Checker/main.cpp 2008-10-07 04:46:34 UTC (rev 901) @@ -24,6 +24,7 @@ #include "../../include/loki/Checker.h" #include <stdexcept> +#include <iostream> #include <vector> @@ -35,7 +36,9 @@ #define NULL 0 #endif +using namespace std; + // ---------------------------------------------------------------------------- /* This class has 2 invariants. The this pointer may never equal NULL, and the @@ -124,7 +127,7 @@ // ---------------------------------------------------------------------------- -// 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 ) { CheckStaticInvariants checker( &Thingy::StaticIsValid ); @@ -279,6 +282,7 @@ // ---------------------------------------------------------------------------- +// This is a static validator. bool Thingy::StaticIsValid( void ) { assert( s_value != 0 ); @@ -287,6 +291,7 @@ // ---------------------------------------------------------------------------- +// This is a per-instance validator. bool Thingy::IsValid( void ) const { assert( nullptr != this ); @@ -339,18 +344,64 @@ // ---------------------------------------------------------------------------- +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[] ) { + 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; try { + cout << "Running basic tests with Thingy." << endl; // First do some tests on class member functions. Thingy t1( 1 ); t1.DoSomething( false ); Thingy t2( 2 ); 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. t1.AddCount( 11 ); t1.AddCount( 13 ); @@ -361,19 +412,60 @@ count = t1.GetCount( 0 ); assert( count == 11 ); 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. Thingy::ChangeThat(); const unsigned int value = Thingy::GetThat(); assert( value != 0 ); + cout << "Done with tests on static member functions" << 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 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; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |