From: <ric...@us...> - 2011-09-20 23:32:35
|
Revision: 1113 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1113&view=rev Author: rich_sposato Date: 2011-09-20 23:32:29 +0000 (Tue, 20 Sep 2011) Log Message: ----------- Added tests for SmartPtr::operator[] - and related changes. Modified Paths: -------------- trunk/test/SmartPtr/base.h trunk/test/SmartPtr/main.cpp trunk/test/SmartPtr/strong.cpp Modified: trunk/test/SmartPtr/base.h =================================================================== --- trunk/test/SmartPtr/base.h 2011-09-20 23:25:41 UTC (rev 1112) +++ trunk/test/SmartPtr/base.h 2011-09-20 23:32:29 UTC (rev 1113) @@ -73,10 +73,11 @@ return s_destructions; } +protected: + BaseClass( const BaseClass & that ) : m_refCount( that.m_refCount ) {} + private: /// Not implemented. - BaseClass( const BaseClass & ); - /// Not implemented. BaseClass & operator = ( const BaseClass & ); static unsigned int s_constructions; @@ -110,7 +111,45 @@ } }; +// ---------------------------------------------------------------------------- +/** @class Feline - The feline family of classes are to test dynamic_cast. Also used to test + pointers to arrays of objects. + */ +class Feline : public BaseClass +{ +public: + virtual ~Feline() {} + virtual Feline * Clone( void ) const = 0; +}; + +class Lion : public Feline +{ +public: + virtual ~Lion() {} + virtual Lion * Clone( void ) const { return new Lion( *this ); } +}; + +class Tiger : public Feline +{ +public: + Tiger( void ) : m_stripes( 100 ) {} + virtual ~Tiger() {} + virtual Tiger * Clone( void ) const { return new Tiger( *this ); } + unsigned int GetStripes( void ) const { return m_stripes; } + void SetStripes( unsigned int s ) { m_stripes = s; } +private: + unsigned int m_stripes; +}; + +class Dog +{ +public: + virtual ~Dog() {} + virtual Dog * Clone( void ) const { return new Dog( *this ); } +}; + + // ---------------------------------------------------------------------------- /** @class MimicCOM Acts like a COM object by having an intrusive ref count. Modified: trunk/test/SmartPtr/main.cpp =================================================================== --- trunk/test/SmartPtr/main.cpp 2011-09-20 23:25:41 UTC (rev 1112) +++ trunk/test/SmartPtr/main.cpp 2011-09-20 23:32:29 UTC (rev 1113) @@ -1205,75 +1205,6 @@ // ---------------------------------------------------------------------------- -namespace -{ - - class Feline - { - public: - - 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; - } - - Feline( void ) { s_constructions++; } - virtual ~Feline() { s_destructions++; } - virtual Feline * Clone( void ) const = 0; - - private: - static unsigned int s_constructions; - static unsigned int s_destructions; - }; - - unsigned int Feline::s_constructions = 0; - unsigned int Feline::s_destructions = 0; - - class Tiger : public Feline - { - public: - virtual ~Tiger() {} - virtual Tiger * Clone( void ) const { return new Tiger( *this ); } - }; - - class Lion : public Feline - { - public: - virtual ~Lion() {} - virtual Lion * Clone( void ) const { return new Lion( *this ); } - }; - - class Dog - { - public: - virtual ~Dog() {} - virtual Dog * Clone( void ) const { return new Dog( *this ); } - }; - -} - -// ---------------------------------------------------------------------------- - void DoSmartPtrDynamicCastTests( void ) { cout << "Starting DoSmartPtrDynamicCastTests." << endl; @@ -1359,9 +1290,9 @@ assert( !pDog ); } - assert( Feline::AllDestroyed() ); - assert( !Feline::ExtraConstructions() ); - assert( !Feline::ExtraDestructions() ); + assert( BaseClass::AllDestroyed() ); + assert( !BaseClass::ExtraConstructions() ); + assert( !BaseClass::ExtraDestructions() ); cout << "Finished DoSmartPtrDynamicCastTests." << endl; } @@ -1518,6 +1449,161 @@ // ---------------------------------------------------------------------------- +/// Use these typedefs to test DeleteArray policy. + +typedef Loki::SmartPtr< Tiger, RefCounted, DisallowConversion, + AssertCheck, ArrayStorage, DontPropagateConst > + TigerArray_RefCounted_ptr; + +typedef Loki::SmartPtr< Tiger, RefLinked, DisallowConversion, + AssertCheck, ArrayStorage, DontPropagateConst > + TigerArray_2RefLinks_ptr; + +// ---------------------------------------------------------------------------- + +void DoSmartArrayTests( void ) +{ + cout << "Starting DoSmartArrayTests." << endl; + + { + // test default construction. + TigerArray_RefCounted_ptr sp1; + assert( !sp1 ); + assert( 0 == sp1.GetArrayCount() ); + + // test assignment. + sp1.Assign( new Tiger[ 8 ], 8 ); + assert( sp1 ); + assert( 8 == sp1.GetArrayCount() ); + sp1[ 0 ].SetStripes( 8 ); + sp1[ 1 ].SetStripes( 16 ); + sp1[ 2 ].SetStripes( 24 ); + sp1[ 3 ].SetStripes( 32 ); + sp1[ 4 ].SetStripes( 40); + sp1[ 5 ].SetStripes( 48 ); + sp1[ 6 ].SetStripes( 56 ); + sp1[ 7 ].SetStripes( 64 ); + + // test initialization construction. + TigerArray_RefCounted_ptr sp2( new Tiger[ 4 ], 4 ); + assert( sp2 ); + assert( 4 == sp2.GetArrayCount() ); + sp2[ 0 ].SetStripes( 5 ); + sp2[ 1 ].SetStripes( 10 ); + sp2[ 2 ].SetStripes( 15 ); + sp2[ 3 ].SetStripes( 20 ); + + // test range checking. + try + { + Tiger & p4 = sp2[ 4 ]; + assert( false ); + } + catch ( const ::std::out_of_range & ex ) + { + assert( true ); + } + + // test range checking. + try + { + Tiger & p8 = sp1[ 8 ]; + assert( false ); + } + catch ( const ::std::out_of_range & ex ) + { + assert( true ); + } + + // test swap. + sp2.Swap( sp1 ); + assert( sp1 ); + assert( sp2 ); + // test checking of item count. + assert( 4 == sp1.GetArrayCount() ); + assert( 8 == sp2.GetArrayCount() ); + + // test that operator[] returns reference to + assert( 5 == sp1[ 0 ].GetStripes() ); + assert( 10 == sp1[ 1 ].GetStripes() ); + assert( 15 == sp1[ 2 ].GetStripes() ); + assert( 20 == sp1[ 3 ].GetStripes() ); + assert( 8 == sp2[ 0 ].GetStripes() ); + assert( 16 == sp2[ 1 ].GetStripes() ); + assert( 24 == sp2[ 2 ].GetStripes() ); + assert( 32 == sp2[ 3 ].GetStripes() ); + assert( 40 == sp2[ 4 ].GetStripes() ); + assert( 48 == sp2[ 5 ].GetStripes() ); + assert( 56 == sp2[ 6 ].GetStripes() ); + assert( 64 == sp2[ 7 ].GetStripes() ); + + try + { + Tiger & p4 = sp1[ 4 ]; + assert( false ); + } + catch ( const ::std::out_of_range & ex ) + { + assert( true ); + } + + try + { + Tiger & p8 = sp2[ 8 ]; + assert( false ); + } + catch ( const ::std::out_of_range & ex ) + { + assert( true ); + } + + const TigerArray_RefCounted_ptr sp3( sp1 ); + assert( sp3 == sp1 ); + assert( sp3.GetArrayCount() == sp1.GetArrayCount() ); + try + { + const Tiger & p4 = sp3[ 4 ]; + assert( false ); + } + catch ( const ::std::out_of_range & ex ) + { + assert( true ); + } + + const TigerArray_RefCounted_ptr sp5( sp2 ); + assert( sp5 == sp2 ); + assert( sp5.GetArrayCount() == sp2.GetArrayCount() ); + try + { + const Tiger & p8 = sp5[ 8 ]; + assert( false ); + } + catch ( const ::std::out_of_range & ex ) + { + assert( true ); + } + + sp2 = sp1; + assert( sp1 == sp2 ); + assert( sp3 == sp2 ); + assert( sp2.GetArrayCount() == sp1.GetArrayCount() ); + assert( sp2.GetArrayCount() == sp1.GetArrayCount() ); + assert( sp1 != sp5 ); + assert( sp2 != sp5 ); + assert( sp3 != sp5 ); + assert( sp1.GetArrayCount() != sp5.GetArrayCount() ); + assert( sp2.GetArrayCount() != sp5.GetArrayCount() ); + assert( sp3.GetArrayCount() != sp5.GetArrayCount() ); + } + + assert( BaseClass::AllDestroyed() ); + assert( !BaseClass::ExtraConstructions() ); + assert( !BaseClass::ExtraDestructions() ); + cout << "Finished DoSmartArrayTests." << endl; +} + +// ---------------------------------------------------------------------------- + int main( int argc, const char * argv[] ) { bool doThreadTest = false; @@ -1556,6 +1642,7 @@ DoSmartPtrDynamicCastTests(); DoStrongPtrDynamicCastTests(); DoStrongArrayTests(); + DoSmartArrayTests(); #if defined (LOKI_OBJECT_LEVEL_THREADING) || defined (LOKI_CLASS_LEVEL_THREADING) if ( doThreadTest ) Modified: trunk/test/SmartPtr/strong.cpp =================================================================== --- trunk/test/SmartPtr/strong.cpp 2011-09-20 23:25:41 UTC (rev 1112) +++ trunk/test/SmartPtr/strong.cpp 2011-09-20 23:32:29 UTC (rev 1113) @@ -1778,42 +1778,6 @@ // ---------------------------------------------------------------------------- -namespace -{ - - class Feline : public BaseClass - { - public: - virtual ~Feline() {} - }; - - class Lion : public Feline - { - public: - virtual ~Lion() {} - }; - - class Tiger : public Feline - { - public: - Tiger( void ) : m_stripes( 100 ) {} - virtual ~Tiger() {} - unsigned int GetStripes( void ) const { return m_stripes; } - void SetStripes( unsigned int s ) { m_stripes = s; } - private: - unsigned int m_stripes; - }; - - class Dog - { - public: - virtual ~Dog() {} - }; - -} - -// ---------------------------------------------------------------------------- - void DoStrongPtrDynamicCastTests( void ) { typedef ::Loki::StrongPtr< Feline, true, ::Loki::TwoRefCounts > FelineCountPtr; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |