diff --git a/test/SmartPtr/main.cpp b/test/SmartPtr/main.cpp index 3918a8d..5299127 100644 --- a/test/SmartPtr/main.cpp +++ b/test/SmartPtr/main.cpp @@ -26,6 +26,7 @@ using namespace std; using namespace Loki; +extern void DoWeakLeakTest( void ); extern void DoStrongRefCountTests( void ); extern void DoStrongRefLinkTests( void ); extern void DoStrongReleaseTests( void ); @@ -1010,6 +1011,7 @@ int main( unsigned int argc, const char * argv[] ) } DoRefLinkTests(); + DoWeakLeakTest(); DoStrongRefCountTests(); DoStrongReleaseTests(); DoStrongReleaseTests(); diff --git a/test/SmartPtr/strong.cpp b/test/SmartPtr/strong.cpp index 2a5349d..a1ab40e 100644 --- a/test/SmartPtr/strong.cpp +++ b/test/SmartPtr/strong.cpp @@ -47,6 +47,65 @@ typedef Loki::StrongPtr< Thingy, true, TwoRefCounts, DisallowConversion, Thingy_DeleteNothing_ptr; +// ---------------------------------------------------------------------------- + +class Counted +{ +public: + + Counted( void ) : m_size( 0 ) + { + s_constructions++; + } + + ~Counted( void ) + { + s_destructions++; + } + + static inline bool AllDestroyed( void ) + { + return ( s_constructions == s_destructions ); + } + + static inline bool ExtraConstructions( void ) + { + return ( s_constructions > s_destructions ); + } + + static inline bool ExtraDestructions( void ) + { + return ( s_constructions < s_destructions ); + } + + static inline unsigned int GetCtorCount( void ) + { + return s_constructions; + } + + static inline unsigned int GetDtorCount( void ) + { + return s_destructions; + } + +private: + /// Not implemented. + Counted( const Counted & ); + /// Not implemented. + Counted & operator = ( const Counted & ); + + static unsigned int s_constructions; + static unsigned int s_destructions; + + int m_size; +}; + +unsigned int Counted::s_constructions = 0; +unsigned int Counted::s_destructions = 0; + +typedef Loki::StrongPtr< Counted, false > Counted_WeakPtr; +typedef Loki::StrongPtr< Counted, true > Counted_StrongPtr; + // ---------------------------------------------------------------------------- class Earth; @@ -252,6 +311,26 @@ typedef Loki::StrongPtr< const BaseClass, false, TwoRefCounts, DisallowConversio // ---------------------------------------------------------------------------- +void DoWeakLeakTest( void ) +{ + assert( Counted::AllDestroyed() ); + assert( Counted::GetCtorCount() == 0 ); + assert( Counted::GetDtorCount() == 0 ); + Counted_WeakPtr pWeakInt; + { + Counted_StrongPtr pStrongInt( new Counted ); + pWeakInt = pStrongInt; + assert( Counted::ExtraConstructions() ); + assert( Counted::GetCtorCount() == 1 ); + assert( Counted::GetDtorCount() == 0 ); + } + assert( Counted::AllDestroyed() ); + assert( Counted::GetCtorCount() == 1 ); + assert( Counted::GetDtorCount() == 1 ); +} + +// ---------------------------------------------------------------------------- + void DoStrongRefCountTests( void ) {