From: <ric...@us...> - 2008-08-23 07:08:25
|
Revision: 900 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=900&view=rev Author: rich_sposato Date: 2008-08-23 07:08:22 +0000 (Sat, 23 Aug 2008) Log Message: ----------- Added pre-conditions and post-conditions. Changed name of class. Modified Paths: -------------- trunk/test/Checker/main.cpp Modified: trunk/test/Checker/main.cpp =================================================================== --- trunk/test/Checker/main.cpp 2008-08-23 07:00:13 UTC (rev 899) +++ trunk/test/Checker/main.cpp 2008-08-23 07:08:22 UTC (rev 900) @@ -4,9 +4,9 @@ // Copyright (c) 2008 Richard Sposato // The copyright on this file is protected under the terms of the MIT license. // -// Permission to use, copy, modify, distribute and sell this software for any -// purpose is hereby granted without fee, provided that the above copyright -// notice appear in all copies and that both that copyright notice and this +// Permission to use, copy, modify, distribute and sell this software for any +// purpose is hereby granted without fee, provided that the above copyright +// notice appear in all copies and that both that copyright notice and this // permission notice appear in supporting documentation. // // The author makes no representations about the suitability of this software @@ -23,7 +23,10 @@ #include "../../include/loki/Checker.h" +#include <stdexcept> +#include <vector> + #if !defined( nullptr ) #define nullptr NULL #endif @@ -42,110 +45,275 @@ { public: - // This example shows how static functions can use a no-throw checkers. - static void ChangeThat( void ) - { - CheckStaticForNoThrow checker( &Thingy::StaticIsValid ); - (void)checker; - s_value--; - } + static void ChangeThat( void ); - // This example shows how static functions can use an invariant checkers. - static unsigned int GetThat( void ) - { - CheckStaticInvariants checker( &Thingy::StaticIsValid ); - (void)checker; - return s_value; - } + static unsigned int GetThat( void ); - // This example shows how ctors can use an invariant checker. - explicit Thingy( unsigned int value ) : m_value( value ) - { - CheckInvariants checker( this, &Thingy::IsValid ); - (void)checker; - checker.Check(); - } + explicit Thingy( unsigned int value ); - // A destructor really doesn't need a checker, but does need to confirm - // the object is valid at the start of the destructor. - ~Thingy( void ) - { - assert( IsValid() ); - } + Thingy( const Thingy & that ); - bool operator == ( const Thingy & that ) const - { - return ( m_value == that.m_value ); - } + Thingy & operator = ( const Thingy & that ); - void DoSomethingEvil( void ) - { - m_value = 0; - } + ~Thingy( void ); - // This example shows how to use the no-throw checker. - unsigned int GetValue( void ) const - { - CheckForNoThrow checker( this, &Thingy::IsValid ); - (void)checker; - return m_value; - } + void Swap( Thingy & that ); - // This example shows how to use the equality checker. - unsigned int DoSomething( bool doThrow ) const - { - CheckForEquality checker( this, &Thingy::IsValid ); - (void)checker; - if ( doThrow ) - throw ::std::exception( "Test Exception." ); - return m_value; - } + bool operator == ( const Thingy & that ) const; - // This example shows how to use the no-change checker. - void DoSomethingElse( void ) const - { - CheckForNoChange checker( this, &Thingy::IsValid ); - (void)checker; - } + void DoSomethingEvil( void ); + unsigned int GetValue( void ) const; + + unsigned int DoSomething( bool doThrow ) const; + + void DoSomethingElse( void ) const; + + void AddCount( unsigned int count ); + + unsigned int GetCount( unsigned int index ) const; + + void ClearCounts( void ); + private: - // This is a static validator. - static bool StaticIsValid( void ) - { - assert( s_value != 0 ); - return true; - } + /// This is a static validator. + static bool StaticIsValid( void ); - // This is a per-instance validator. - bool IsValid( void ) const - { - assert( nullptr != this ); - assert( m_value != 0 ); - return true; - } + /// This is a per-instance validator. + bool IsValid( void ) const; + /// This can be used to validate pre-conditions and post-conditions. + bool IsValidEmpty( void ) const; + + /// This can be used to validate pre-conditions and post-conditions. + bool IsValidFull( void ) const; + // These lines show how to declare checkers for non-static functions in a host class. - typedef ::Loki::Checker< Thingy, ::Loki::CheckForNoThrow > CheckForNoThrow; - typedef ::Loki::Checker< Thingy, ::Loki::CheckForNoChangeOrThrow > CheckForNoChangeOrThrow; - typedef ::Loki::Checker< Thingy, ::Loki::CheckForNoChange > CheckForNoChange; - typedef ::Loki::Checker< Thingy, ::Loki::CheckForEquality > CheckForEquality; - typedef ::Loki::Checker< Thingy, ::Loki::CheckForNothing > CheckInvariants; + typedef ::Loki::ContractChecker< Thingy, ::Loki::CheckForNoThrow > CheckForNoThrow; + typedef ::Loki::ContractChecker< Thingy, ::Loki::CheckForNoChangeOrThrow > CheckForNoChangeOrThrow; + typedef ::Loki::ContractChecker< Thingy, ::Loki::CheckForNoChange > CheckForNoChange; + typedef ::Loki::ContractChecker< Thingy, ::Loki::CheckForEquality > CheckForEquality; + typedef ::Loki::ContractChecker< Thingy, ::Loki::CheckForNothing > CheckInvariants; // These lines show how to declare checkers for static functions of a host class. typedef ::Loki::StaticChecker< ::Loki::CheckStaticForNoThrow > CheckStaticForNoThrow; typedef ::Loki::StaticChecker< ::Loki::CheckStaticForNothing > CheckStaticInvariants; + typedef ::std::vector< unsigned int > IntBlock; + static unsigned int s_value; unsigned int m_value; + IntBlock m_counts; + }; unsigned int Thingy::s_value = 10; // ---------------------------------------------------------------------------- +// This example shows how static functions can use a no-throw checkers. +void Thingy::ChangeThat( void ) +{ + CheckStaticForNoThrow checker( &Thingy::StaticIsValid ); + (void)checker; + s_value--; +} + +// ---------------------------------------------------------------------------- + +// This example shows how static functions can use an invariant checkers. +unsigned int Thingy::GetThat( void ) +{ + CheckStaticInvariants checker( &Thingy::StaticIsValid ); + (void)checker; + return s_value; +} + +// ---------------------------------------------------------------------------- + +// This example shows how ctors can use an invariant checker. +Thingy::Thingy( unsigned int value ) : + m_value( value ), + m_counts() +{ + CheckInvariants checker( this, &Thingy::IsValid ); + (void)checker; +} + +// ---------------------------------------------------------------------------- + +Thingy::Thingy( const Thingy & that ) : + m_value( that.m_value ), + m_counts( that.m_counts ) +{ + CheckInvariants checker( this, &Thingy::IsValid ); + (void)checker; +} + +// ---------------------------------------------------------------------------- + +Thingy & Thingy::operator = ( const Thingy & that ) +{ + CheckInvariants checker( this, &Thingy::IsValid ); + (void)checker; + if ( &that != this ) + { + Thingy temp( that ); + temp.Swap( *this ); + } + return *this; +} + +// ---------------------------------------------------------------------------- + +// A destructor really doesn't need a checker, but does need to confirm +// the object is valid at the start of the destructor. +Thingy::~Thingy( void ) +{ + assert( IsValid() ); +} + +// ---------------------------------------------------------------------------- + +// A swap function gets 2 checkers - one for this, and another for that. +void Thingy::Swap( Thingy & that ) +{ + CheckInvariants checker1( this, &Thingy::IsValid ); + (void)checker1; + CheckInvariants checker2( &that, &Thingy::IsValid ); + (void)checker2; + + const IntBlock counts( m_counts ); + m_counts = that.m_counts; + that.m_counts = counts; + const unsigned int value = m_value; + m_value = that.m_value; + that.m_value = value; +} + +// ---------------------------------------------------------------------------- + +bool Thingy::operator == ( const Thingy & that ) const +{ + return ( m_value == that.m_value ); +} + +// ---------------------------------------------------------------------------- + +void Thingy::DoSomethingEvil( void ) +{ + m_value = 0; +} + +// ---------------------------------------------------------------------------- + +// This example shows how to use the no-throw checker. +unsigned int Thingy::GetValue( void ) const +{ + CheckForNoThrow checker( this, &Thingy::IsValid ); + (void)checker; + return m_value; +} + +// ---------------------------------------------------------------------------- + +// This example shows how to use the equality checker. +unsigned int Thingy::DoSomething( bool doThrow ) const +{ + CheckForEquality checker( this, &Thingy::IsValid ); + (void)checker; + if ( doThrow ) + throw ::std::logic_error( "Test Exception." ); + return m_value; +} + +// ---------------------------------------------------------------------------- + +// This example shows how to use the no-change checker. +void Thingy::DoSomethingElse( void ) const +{ + CheckForNoChange checker( this, &Thingy::IsValid ); + (void)checker; +} + +// ---------------------------------------------------------------------------- + +void Thingy::AddCount( unsigned int count ) +{ + // This function's checker cares about class invariants and post-conditions, + // but does not need to check pre-conditions, so it passes in a nullptr for + // the pre-condition validator. Ths post-condition validator just makes sure + // the container has at least 1 element. + CheckInvariants checker( this, &Thingy::IsValid, nullptr, &Thingy::IsValidFull ); + m_counts.push_back( count ); +} + +// ---------------------------------------------------------------------------- + +unsigned int Thingy::GetCount( unsigned int index ) const +{ + // This function's checker cares about class invariants and both the pre- and + // post-conditions, so it passes in pointers for all 3 validators. The pre- + // and post-conditions are both about making sure the container is not empty. + CheckForNoChangeOrThrow checker( this, &Thingy::IsValid, &Thingy::IsValidFull, &Thingy::IsValidFull ); + if ( m_counts.size() <= index ) + return 0; + const unsigned int count = m_counts[ index ]; + return count; +} + +// ---------------------------------------------------------------------------- + +void Thingy::ClearCounts( void ) +{ + // This function's checker cares about class invariants and post-conditions, + // but does not need to check pre-conditions, so it passes in a nullptr for + // the pre-condition validator. Ths post-condition validator just makes sure + // the container has no elements. + CheckForNoThrow checker( this, &Thingy::IsValid, nullptr, &Thingy::IsValidEmpty ); + m_counts.clear(); +} + +// ---------------------------------------------------------------------------- + +bool Thingy::StaticIsValid( void ) +{ + assert( s_value != 0 ); + return true; +} + +// ---------------------------------------------------------------------------- + +bool Thingy::IsValid( void ) const +{ + assert( nullptr != this ); + assert( m_value != 0 ); + return true; +} + +// ---------------------------------------------------------------------------- + +bool Thingy::IsValidEmpty( void ) const +{ + assert( nullptr != this ); + assert( m_counts.size() == 0 ); + return true; +} + +// ---------------------------------------------------------------------------- + +bool Thingy::IsValidFull( void ) const +{ + assert( nullptr != this ); + assert( m_counts.size() != 0 ); + return true; +} + +// ---------------------------------------------------------------------------- + // This is a validator function called by checkers inside standalone functions. bool AllIsValid( void ) { @@ -174,6 +342,7 @@ int main( unsigned int argc, const char * const argv[] ) { + unsigned int count = 0; try { // First do some tests on class member functions. @@ -182,6 +351,17 @@ Thingy t2( 2 ); t2.DoSomething( true ); + // These lines will exercise the functions with pre- and post-conditions. + t1.AddCount( 11 ); + t1.AddCount( 13 ); + t1.AddCount( 17 ); + t1.AddCount( 19 ); + count = t1.GetCount( 3 ); + assert( count == 19 ); + count = t1.GetCount( 0 ); + assert( count == 11 ); + t1.ClearCounts(); + // Next do some tests with static member functions. Thingy::ChangeThat(); const unsigned int value = Thingy::GetThat(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <ric...@us...> - 2009-09-26 21:10:49
|
Revision: 1023 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1023&view=rev Author: rich_sposato Date: 2009-09-26 21:10:43 +0000 (Sat, 26 Sep 2009) Log Message: ----------- Added year to copyright notice. Modified Paths: -------------- trunk/test/Checker/main.cpp Modified: trunk/test/Checker/main.cpp =================================================================== --- trunk/test/Checker/main.cpp 2009-09-26 21:03:36 UTC (rev 1022) +++ trunk/test/Checker/main.cpp 2009-09-26 21:10:43 UTC (rev 1023) @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // // Test program for The Loki Library -// Copyright (c) 2008 Richard Sposato +// Copyright (c) 2008, 2009 Richard Sposato // The copyright on this file is protected under the terms of the MIT license. // // Permission to use, copy, modify, distribute and sell this software for any This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ric...@us...> - 2011-11-04 00:54:53
|
Revision: 1172 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1172&view=rev Author: rich_sposato Date: 2011-11-04 00:54:46 +0000 (Fri, 04 Nov 2011) Log Message: ----------- Update cout strings to be more accurate. Added tests. Modified Paths: -------------- trunk/test/Checker/main.cpp Modified: trunk/test/Checker/main.cpp =================================================================== --- trunk/test/Checker/main.cpp 2011-11-04 00:53:49 UTC (rev 1171) +++ trunk/test/Checker/main.cpp 2011-11-04 00:54:46 UTC (rev 1172) @@ -70,7 +70,7 @@ unsigned int DoSomething( bool doThrow ) const; - void DoSomethingElse( void ) const; + void DoSomethingElse( unsigned int count, bool doThrow ); void AddCount( unsigned int count ); @@ -235,7 +235,7 @@ // This example shows how to use the equality checker. unsigned int Thingy::DoSomething( bool doThrow ) const { - CheckMementoFor::Equality checker( this, &Thingy::IsValid ); + CheckMementoFor::NoChange checker( this, &Thingy::IsValid ); (void)checker; if ( doThrow ) throw ::std::logic_error( "Test Exception." ); @@ -245,10 +245,16 @@ // ---------------------------------------------------------------------------- // This example shows how to use the no-change checker. -void Thingy::DoSomethingElse( void ) const +void Thingy::DoSomethingElse( unsigned int count, bool doThrow ) { - CheckMementoFor::NoChange checker( this, &Thingy::IsValid ); + CheckMementoFor::NoChangeOnThrow checker( this, &Thingy::IsValid ); (void)checker; + + IntBlock counts( m_counts ); + counts.push_back( count ); + if ( doThrow ) + throw ::std::logic_error( "Test Exception." ); + counts.swap( m_counts ); } // ---------------------------------------------------------------------------- @@ -270,7 +276,7 @@ // This function's checker cares about class invariants and both the pre- and // post-conditions, so it passes in pointers for all 3 validators. The pre- // and post-conditions are both about making sure the container is not empty. - CheckMementoFor::NoChangeOrThrow checker( this, &Thingy::IsValid, &Thingy::IsValidFull, &Thingy::IsValidFull ); + CheckMementoFor::NoThrowOrChange checker( this, &Thingy::IsValid, &Thingy::IsValidFull, &Thingy::IsValidFull ); if ( m_counts.size() <= index ) return 0; const unsigned int count = m_counts[ index ]; @@ -360,53 +366,89 @@ (void)argc; (void)argv; + cout << "These tests confirm the Checker exception policies work properly." << endl; + cout << "The tests will throw exceptions. If you see any assertions, the test failed." << endl; + try { cout << "Just before call to ThrowTest." << endl; ThrowTest(); cout << "Just after call to ThrowTest." << endl; + assert( false ); } catch ( const ::std::logic_error & ex ) { - cout << "Caught an exception! " << ex.what() << endl; + cout << "Caught a logic_error exception! " << ex.what() << endl; + assert( true ); } catch ( const ::std::exception & ex ) { - cout << "Caught an exception! " << ex.what() << endl; + cout << "Caught a standard exception! " << ex.what() << endl; + assert( false ); } catch ( ... ) { - cout << "Caught an exception!" << endl; + cout << "Caught an unknown exception!" << endl; + assert( false ); } unsigned int count = 0; try { - cout << "Running basic tests with Thingy." << endl; + cout << "Starting test of CheckFor::NoChange policy." << endl; // First do some tests on class member functions. Thingy t1( 1 ); t1.DoSomething( false ); + assert( true ); Thingy t2( 2 ); t2.DoSomething( true ); - cout << "Done with basic tests with Thingy." << endl; + assert( false ); } catch ( const ::std::logic_error & ex ) { cout << "Caught an exception! " << ex.what() << endl; + assert( true ); } catch ( const ::std::exception & ex ) { + cout << "Caught a standard exception! " << ex.what() << endl; + } + catch ( ... ) + { + cout << "Caught an unknown exception!" << endl; + } + cout << "Done testing CheckFor::NoChange policy." << endl; + + try + { + cout << "Starting test of CheckFor::NoChangeOnThrow policy." << endl; + Thingy t1( 1 ); + t1.DoSomethingElse( 3, false ); + Thingy t2( 2 ); + t2.DoSomethingElse( 5, true ); + assert( false ); + } + catch ( const ::std::logic_error & ex ) + { cout << "Caught an exception! " << ex.what() << endl; + assert( true ); } + catch ( const ::std::exception & ex ) + { + cout << "Caught a standard exception! " << ex.what() << endl; + assert( false ); + } catch ( ... ) { - cout << "Caught an exception!" << endl; + cout << "Caught an unknown exception!" << endl; + assert( false ); } + cout << "Finished test of CheckFor::NoChangeOnThrow policy." << endl; try { + cout << "Starting tests of CheckFor::Invariants and CheckFor::NoThrowOrChange policy." << endl; 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 ); @@ -421,54 +463,64 @@ } catch ( const ::std::logic_error & ex ) { - cout << "Caught an exception! " << ex.what() << endl; + cout << "Caught a logic_error exception! " << ex.what() << endl; } catch ( const ::std::exception & ex ) { - cout << "Caught an exception! " << ex.what() << endl; + cout << "Caught a standard exception! " << ex.what() << endl; + assert( false ); } catch ( ... ) { - cout << "Caught an exception!" << endl; + cout << "Caught an unknown exception!" << endl; + assert( false ); } + cout << "Finished tests of CheckFor::Invariants and CheckFor::NoThrowOrChange policy." << endl; try { - cout << "Now run tests on static member functions" << endl; + cout << "Starting test of CheckStaticFor::Invariants policy." << 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; + assert( true ); } catch ( const ::std::logic_error & ex ) { - cout << "Caught an exception! " << ex.what() << endl; + cout << "Caught a logic_error exception! " << ex.what() << endl; + assert( false ); } catch ( const ::std::exception & ex ) { - cout << "Caught an exception! " << ex.what() << endl; + cout << "Caught a standard exception! " << ex.what() << endl; + assert( false ); } catch ( ... ) { - cout << "Caught an exception!" << endl; + cout << "Caught an unknown exception!" << endl; + assert( false ); } + cout << "Finished test of CheckStaticFor::Invariants policy." << endl; try { - cout << "Now run test on a standalone function." << endl; + cout << "Starting test of CheckStaticFor::NoThrow policy." << endl; // Then do a test with a standalone function. DoSomething(); - cout << "Done with test on a standalone function." << endl; + assert( true ); } catch ( const ::std::exception & ex ) { - cout << "Caught an exception! " << ex.what() << endl; + cout << "Caught a standard exception! " << ex.what() << endl; + assert( false ); } catch ( ... ) { - cout << "Caught an exception!" << endl; + cout << "Caught an unknown exception!" << endl; + assert( false ); } + cout << "Finished test of CheckStaticFor::NoThrow policy." << endl; cout << "All done! If you see this line, and no assertions failed, then the test passed!" << endl; return 0; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ric...@us...> - 2009-09-26 19:42:16
|
Revision: 1018 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1018&view=rev Author: rich_sposato Date: 2009-09-26 19:42:09 +0000 (Sat, 26 Sep 2009) Log Message: ----------- Added tests which compare host to memento. Modified Paths: -------------- trunk/test/Checker/main.cpp Modified: trunk/test/Checker/main.cpp =================================================================== --- trunk/test/Checker/main.cpp 2009-09-26 19:40:20 UTC (rev 1017) +++ trunk/test/Checker/main.cpp 2009-09-26 19:42:09 UTC (rev 1018) @@ -80,6 +80,17 @@ private: + class Memento + { + public: + explicit Memento( const Thingy & t ) : m_count( t.m_counts.size() ) {} + bool operator == ( const Thingy & t ) const + { + return ( m_count == t.m_counts.size() ); + } + unsigned int m_count; + }; + /// This is a static validator. static bool StaticIsValid( void ); @@ -95,6 +106,9 @@ // This shows how to declare checkers for non-static functions in a host class. typedef ::Loki::CheckFor< Thingy > CheckFor; + // This shows how to declare checkers for non-static functions in a host class. + typedef ::Loki::CheckFor< Thingy, Memento > CheckMementoFor; + // This shows how to declare checkers for static functions of a host class. typedef ::Loki::CheckStaticFor CheckStaticFor; @@ -221,7 +235,7 @@ // This example shows how to use the equality checker. unsigned int Thingy::DoSomething( bool doThrow ) const { - CheckFor::Equality checker( this, &Thingy::IsValid ); + CheckMementoFor::Equality checker( this, &Thingy::IsValid ); (void)checker; if ( doThrow ) throw ::std::logic_error( "Test Exception." ); @@ -233,7 +247,7 @@ // This example shows how to use the no-change checker. void Thingy::DoSomethingElse( void ) const { - CheckFor::NoChange checker( this, &Thingy::IsValid ); + CheckMementoFor::NoChange checker( this, &Thingy::IsValid ); (void)checker; } @@ -256,7 +270,7 @@ // This function's checker cares about class invariants and both the pre- and // post-conditions, so it passes in pointers for all 3 validators. The pre- // and post-conditions are both about making sure the container is not empty. - CheckFor::NoChangeOrThrow checker( this, &Thingy::IsValid, &Thingy::IsValidFull, &Thingy::IsValidFull ); + CheckMementoFor::NoChangeOrThrow checker( this, &Thingy::IsValid, &Thingy::IsValidFull, &Thingy::IsValidFull ); if ( m_counts.size() <= index ) return 0; const unsigned int count = m_counts[ index ]; @@ -456,7 +470,7 @@ cout << "Caught an exception!" << endl; } - cout << "All done!" << endl; + cout << "All done! If you see this line, and no assertions failed, then the test passed!" << endl; return 0; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |