From: <syn...@us...> - 2008-12-19 13:05:59
|
Revision: 915 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=915&view=rev Author: syntheticpp Date: 2008-12-19 13:05:51 +0000 (Fri, 19 Dec 2008) Log Message: ----------- add link to GCC bug report Modified Paths: -------------- trunk/test/SmartPtr/strong.cpp Modified: trunk/test/SmartPtr/strong.cpp =================================================================== --- trunk/test/SmartPtr/strong.cpp 2008-12-19 00:39:29 UTC (rev 914) +++ trunk/test/SmartPtr/strong.cpp 2008-12-19 13:05:51 UTC (rev 915) @@ -1185,3 +1185,34 @@ } // ---------------------------------------------------------------------------- + + +// GCC bug +// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38579 + +struct Policy +{ +protected: + Policy() {} + Policy(const Policy&) {} +}; + +template<int I, class P> +struct BugGcc : + //protected P + public P +{ + BugGcc() {} + + template<int I2, class P2> + BugGcc(const BugGcc<I2, P2>& t) : P(t) {} +}; + +void foo() +{ + BugGcc<0, Policy> f1; + BugGcc<1, Policy> f2(f1); +} + + + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <syn...@us...> - 2008-12-19 13:09:03
|
Revision: 916 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=916&view=rev Author: syntheticpp Date: 2008-12-19 13:08:55 +0000 (Fri, 19 Dec 2008) Log Message: ----------- add link to GCC bug report Modified Paths: -------------- trunk/test/SmartPtr/strong.cpp Modified: trunk/test/SmartPtr/strong.cpp =================================================================== --- trunk/test/SmartPtr/strong.cpp 2008-12-19 13:05:51 UTC (rev 915) +++ trunk/test/SmartPtr/strong.cpp 2008-12-19 13:08:55 UTC (rev 916) @@ -1195,6 +1195,7 @@ protected: Policy() {} Policy(const Policy&) {} + int i; }; template<int I, class P> @@ -1212,6 +1213,10 @@ { BugGcc<0, Policy> f1; BugGcc<1, Policy> f2(f1); + + // Policy members are still not public, + // this will not compile: + //int i = f1.i; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ric...@us...> - 2010-10-30 03:13:03
|
Revision: 1081 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1081&view=rev Author: rich_sposato Date: 2010-10-30 03:12:56 +0000 (Sat, 30 Oct 2010) Log Message: ----------- Added tests for new single-owner policy classes for StrongPtr. Modified Paths: -------------- trunk/test/SmartPtr/strong.cpp Modified: trunk/test/SmartPtr/strong.cpp =================================================================== --- trunk/test/SmartPtr/strong.cpp 2010-10-30 03:11:07 UTC (rev 1080) +++ trunk/test/SmartPtr/strong.cpp 2010-10-30 03:12:56 UTC (rev 1081) @@ -1,12 +1,12 @@ //////////////////////////////////////////////////////////////////////////////// // Test program for The Loki Library // Copyright (c) 2006 Richard Sposato -// 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 authors make no representations about the -// suitability of this software for any purpose. It is provided "as is" +// The authors make no representations about the +// suitability of this software for any purpose. It is provided "as is" // without express or implied warranty. //////////////////////////////////////////////////////////////////////////////// @@ -23,12 +23,21 @@ #include <iostream> #include <cassert> +#include <cstring> #include "base.h" // ---------------------------------------------------------------------------- +#if !defined( NULL ) + #define nullptr 0 +#endif + +#if !defined( nullptr ) + #define nullptr NULL +#endif + using namespace std; using namespace Loki; @@ -140,6 +149,7 @@ AssertCheck, AllowReset, DeleteSingle, DontPropagateConst > Moon_StrongPtr; + // ---------------------------------------------------------------------------- class Earth @@ -329,6 +339,8 @@ const unsigned int ctorCount = Counted::GetCtorCount(); const unsigned int dtorCount = Counted::GetDtorCount(); assert( Counted::AllDestroyed() ); + (void)ctorCount; + (void)dtorCount; { Counted_WeakPtr pWeakInt; @@ -1028,11 +1040,591 @@ // ---------------------------------------------------------------------------- +class Ball +{ +public: + + inline static unsigned int GetCtorCount( void ) + { + return s_CtorCount; + } + + inline static unsigned int GetDtorCount( void ) + { + return s_DtorCount; + } + + inline static bool AllDestroyed( void ) + { + return ( s_CtorCount == s_DtorCount ); + } + + inline Ball( void ) + { + s_CtorCount++; + } + + inline ~Ball( void ) + { + s_DtorCount++; + } + +private: + static unsigned int s_CtorCount; + static unsigned int s_DtorCount; +}; + +unsigned int Ball::s_CtorCount = 0; +unsigned int Ball::s_DtorCount = 0; + +// ---------------------------------------------------------------------------- + +// These typedefs are for testing StrongPtr ownership policies that enforce a single owner. +typedef ::Loki::StrongPtr< Ball, false, ::Loki::SingleOwnerRefCount, + ::Loki::DisallowConversion, ::Loki::AssertCheck, ::Loki::OnlyStrongMayReset > + NonOwner_Counted_BallPtr; +typedef ::Loki::StrongPtr< Ball, true, ::Loki::SingleOwnerRefCount, + ::Loki::DisallowConversion, ::Loki::AssertCheck, ::Loki::OnlyStrongMayReset > + Owner_Counted_BallPtr; + +typedef ::Loki::StrongPtr< Ball, false, ::Loki::Lockable1OwnerRefCount, + ::Loki::DisallowConversion, ::Loki::AssertCheck, ::Loki::OnlyStrongMayReset > + Lockable_NonOwner_Counted_BallPtr; +typedef ::Loki::StrongPtr< Ball, true, ::Loki::Lockable1OwnerRefCount, + ::Loki::DisallowConversion, ::Loki::AssertCheck, ::Loki::OnlyStrongMayReset > + Lockable_Owner_Counted_BallPtr; + +typedef ::Loki::StrongPtr< Ball, false, ::Loki::SingleOwnerRefLinks, + ::Loki::DisallowConversion, ::Loki::AssertCheck, ::Loki::OnlyStrongMayReset > + NonOwner_Linked_BallPtr; +typedef ::Loki::StrongPtr< Ball, true, ::Loki::SingleOwnerRefLinks, + ::Loki::DisallowConversion, ::Loki::AssertCheck, ::Loki::OnlyStrongMayReset > + Owner_Linked_BallPtr; + +// ---------------------------------------------------------------------------- + +template < class OwnerPtr, class NonOwnerPtr > +class TheOwner +{ +public: + + TheOwner( void ) : m_myBall(), m_otherBall() {} + ~TheOwner( void ) {} + + void ClearBall( void ) + { + Ball * pBall = NULL; + ResetAll( m_myBall, pBall ); + } + + bool OwnBall( const NonOwnerPtr & ball ) + { + if ( !ball ) + return false; + m_myBall = ball; + return true; + } + + bool OwnBall( const OwnerPtr & ball ) + { + if ( !ball ) + return false; + m_myBall = ball; + return true; + } + + bool OwnBall( Ball * ball ) + { + if ( nullptr == ball ) + return false; + m_myBall = ball; + return true; + } + + bool DoesOwnBall( void ) const + { + const bool hasBall = m_myBall; + return hasBall; + } + + void UseBall( const OwnerPtr & ball ) + { + m_otherBall = ball; + } + + bool DoesUseBall( void ) const + { + const bool hasBall = m_otherBall; + return hasBall; + } + + const OwnerPtr & ShareMyBall( void ) const + { + return m_myBall; + } + + const NonOwnerPtr & ShareOtherBall( void ) + { + return m_otherBall; + } + +private: + OwnerPtr m_myBall; + NonOwnerPtr m_otherBall; +}; + +typedef TheOwner< Owner_Counted_BallPtr, NonOwner_Counted_BallPtr > Toddler; + +typedef TheOwner< Owner_Linked_BallPtr, NonOwner_Linked_BallPtr > Kitten; + +typedef TheOwner< Lockable_Owner_Counted_BallPtr, Lockable_NonOwner_Counted_BallPtr > Teenager; + +// ---------------------------------------------------------------------------- + +template < class OwnerPtr, class NonOwnerPtr, class Owner > +void DoSingleOwnerTest( OwnerPtr & op1, + const NonOwnerPtr & pBall1, + const NonOwnerPtr & pBall2, + const NonOwnerPtr & pBall3, + Owner & owner1, + Owner & owner2, + Owner & owner3 ) +{ + + assert( !pBall1.IsStrong() ); + assert( !pBall2.IsStrong() ); + assert( !pBall3.IsStrong() ); + assert( pBall1 != nullptr ); + assert( pBall2 != nullptr ); + assert( pBall3 != nullptr ); + + assert( !owner1.DoesOwnBall() ); + assert( !owner2.DoesOwnBall() ); + assert( !owner3.DoesOwnBall() ); + assert( !owner1.DoesUseBall() ); + assert( !owner2.DoesUseBall() ); + assert( !owner3.DoesUseBall() ); + + owner1.OwnBall( pBall1 ); + owner2.OwnBall( pBall2 ); + owner3.OwnBall( pBall3 ); + assert( owner1.DoesOwnBall() ); + assert( owner2.DoesOwnBall() ); + assert( owner3.DoesOwnBall() ); + assert( !owner1.DoesUseBall() ); + assert( !owner2.DoesUseBall() ); + assert( !owner3.DoesUseBall() ); + + assert( owner1.ShareMyBall() == pBall1 ); + assert( owner2.ShareMyBall() == pBall2 ); + assert( owner3.ShareMyBall() == pBall3 ); + assert( owner1.ShareMyBall().IsStrong() ); + assert( owner2.ShareMyBall().IsStrong() ); + assert( owner3.ShareMyBall().IsStrong() ); + + owner1.UseBall( owner2.ShareMyBall() ); + owner2.UseBall( owner3.ShareMyBall() ); + owner3.UseBall( owner1.ShareMyBall() ); + assert( owner1.DoesUseBall() ); + assert( owner2.DoesUseBall() ); + assert( owner3.DoesUseBall() ); + + assert( !owner1.ShareOtherBall().IsStrong() ); + assert( !owner2.ShareOtherBall().IsStrong() ); + assert( !owner3.ShareOtherBall().IsStrong() ); + + assert( owner1.ShareOtherBall() == pBall2 ); + assert( owner2.ShareOtherBall() == pBall3 ); + assert( owner3.ShareOtherBall() == pBall1 ); + assert( owner1.ShareMyBall() == pBall1 ); + assert( owner2.ShareMyBall() == pBall2 ); + assert( owner3.ShareMyBall() == pBall3 ); + + try + { + assert( owner2.ShareMyBall() == pBall2 ); + // Can owner1 own a ball that is owned by somebody else? + owner1.OwnBall( pBall2 ); + assert( false ); + } + catch ( const ::std::logic_error & ex ) + { + assert( true ); + assert( ::strcmp( ex.what(), ::Loki::StrongPtr_Single_Owner_Exception_Message ) == 0 ); + (void)ex; + } + try + { + assert( owner3.ShareMyBall() == pBall3 ); + // Can owner2 own a ball that is owned by somebody else? + owner2.OwnBall( pBall3 ); + assert( false ); + } + catch ( const ::std::logic_error & ex ) + { + assert( true ); + assert( ::strcmp( ex.what(), ::Loki::StrongPtr_Single_Owner_Exception_Message ) == 0 ); + (void)ex; + } + try + { + assert( owner1.ShareMyBall() == pBall1 ); + // Can owner3 own a ball that is owned by somebody else? + owner3.OwnBall( pBall1 ); + assert( false ); + } + catch ( const ::std::logic_error & ex ) + { + assert( true ); + assert( ::strcmp( ex.what(), ::Loki::StrongPtr_Single_Owner_Exception_Message ) == 0 ); + (void)ex; + } + + // These tests occur after exceptions were thrown. Since these tests pass, + // that means the pointers are still in a valid state. Also, the pointers + // are untouched by the temporary StrongPtr's that were made inside the + // assignment operators, so therefore the pointers have strong exception + // safety. + owner1.ClearBall(); + owner2.ClearBall(); + owner3.ClearBall(); + assert( !owner1.DoesOwnBall() ); + assert( !owner2.DoesOwnBall() ); + assert( !owner3.DoesOwnBall() ); + assert( nullptr == pBall1 ); + assert( nullptr == pBall2 ); + assert( nullptr == pBall3 ); + assert( owner1.ShareMyBall() == pBall1 ); + assert( owner2.ShareMyBall() == pBall2 ); + assert( owner3.ShareMyBall() == pBall3 ); + assert( owner1.ShareMyBall() == nullptr ); + assert( owner2.ShareMyBall() == nullptr ); + assert( owner3.ShareMyBall() == nullptr ); + + { + Ball * baseball = new Ball; + Ball * football = new Ball; + Ball * softball = new Ball; + + NonOwnerPtr p1( baseball ); + NonOwnerPtr p2( football ); + NonOwnerPtr p3( softball ); + assert( nullptr != p1 ); + assert( nullptr != p2 ); + assert( nullptr != p3 ); + + assert( !p1.IsStrong() ); + assert( !p2.IsStrong() ); + assert( !p3.IsStrong() ); + + { + Owner o1; + Owner o2; + Owner o3; + + assert( !o1.DoesOwnBall() ); + assert( !o2.DoesOwnBall() ); + assert( !o3.DoesOwnBall() ); + assert( !o1.DoesUseBall() ); + assert( !o2.DoesUseBall() ); + assert( !o3.DoesUseBall() ); + + o1.OwnBall( p1 ); + o2.OwnBall( p2 ); + o3.OwnBall( p3 ); + + assert( o1.DoesOwnBall() ); + assert( o2.DoesOwnBall() ); + assert( o3.DoesOwnBall() ); + assert( !o1.DoesUseBall() ); + assert( !o2.DoesUseBall() ); + assert( !o3.DoesUseBall() ); + + assert( o1.ShareMyBall() == p1 ); + assert( o2.ShareMyBall() == p2 ); + assert( o3.ShareMyBall() == p3 ); + + o1.ClearBall(); + o2.ClearBall(); + o3.ClearBall(); + assert( !o1.DoesOwnBall() ); + assert( !o2.DoesOwnBall() ); + assert( !o3.DoesOwnBall() ); + assert( o1.ShareMyBall() == nullptr ); + assert( o2.ShareMyBall() == nullptr ); + assert( o3.ShareMyBall() == nullptr ); + assert( nullptr == p1 ); + assert( nullptr == p2 ); + assert( nullptr == p3 ); + assert( o1.ShareMyBall() == p1 ); + assert( o2.ShareMyBall() == p2 ); + assert( o3.ShareMyBall() == p3 ); + } + + assert( Ball::AllDestroyed() ); + assert( nullptr == p1 ); + assert( nullptr == p2 ); + assert( nullptr == p3 ); + } + + { + OwnerPtr op_1; + OwnerPtr op_2; + (void)op_1; + (void)op_2; + op_1 = op_2; + } + assert( Ball::AllDestroyed() ); + + // Test ResetAll with owner pointer. + { + Ball * ball = new Ball; + Ball * noBall = nullptr; + OwnerPtr op_1( ball ); + NonOwnerPtr np1( op_1 ); + NonOwnerPtr np2( np1 ); + assert( ball == op_1 ); + assert( ball == np1 ); + assert( ball == np2 ); + + ResetAll( op_1, noBall ); + assert( Ball::AllDestroyed() ); + assert( nullptr == op_1 ); + assert( nullptr == np1 ); + assert( nullptr == np2 ); + assert( nullptr == noBall ); + } + + // Test ReleaseAll with owner pointer. + { + Ball * ball = new Ball; + Ball * noBall = nullptr; + OwnerPtr op_1( ball ); + NonOwnerPtr np1( op_1 ); + NonOwnerPtr np2( np1 ); + assert( ball == op_1 ); + assert( ball == np1 ); + assert( ball == np2 ); + + ReleaseAll( op_1, noBall ); + assert( !Ball::AllDestroyed() ); + assert( nullptr != noBall ); + assert( noBall == ball ); + assert( nullptr == op_1 ); + assert( nullptr == np1 ); + assert( nullptr == np2 ); + delete noBall; + assert( Ball::AllDestroyed() ); + } + + // Test ResetAll with nonowner pointer. + { + Ball * ball = new Ball; + Ball * noBall = nullptr; + OwnerPtr op_1( ball ); + NonOwnerPtr np1( op_1 ); + NonOwnerPtr np2( np1 ); + assert( ball == op_1 ); + assert( ball == np1 ); + assert( ball == np2 ); + + ResetAll( np1, noBall ); + // ResetAll fails for non-owner pointers. + assert( !Ball::AllDestroyed() ); + assert( nullptr != op_1 ); + assert( nullptr != np1 ); + assert( nullptr != np2 ); + assert( nullptr == noBall ); + } + assert( Ball::AllDestroyed() ); + + // Test ReleaseAll with nonowner pointer. + { + Ball * ball = new Ball; + Ball * noBall = nullptr; + OwnerPtr op_1( ball ); + NonOwnerPtr np1( op_1 ); + NonOwnerPtr np2( np1 ); + assert( ball == op_1 ); + assert( ball == np1 ); + assert( ball == np2 ); + + ReleaseAll( np1, noBall ); + // ReleaseAll fails for non-owner pointers. + assert( !Ball::AllDestroyed() ); + assert( nullptr != op_1 ); + assert( nullptr != np1 ); + assert( nullptr != np2 ); + assert( nullptr == noBall ); + } + assert( Ball::AllDestroyed() ); + + // Test assignment operator. + { + OwnerPtr op2; + Ball * ball1 = new Ball; + Ball * ball2 = new Ball; + op1 = ball1; + op2 = ball2; + assert( ball1 == op1 ); + assert( ball2 == op2 ); + op1 = nullptr; + } + assert( Ball::AllDestroyed() ); + + // Test the Swap function for strong co-pointers. + { + NonOwnerPtr np1; + NonOwnerPtr np2; + Ball * ball1 = new Ball; + Ball * ball2 = new Ball; + np1 = ball1; + np2 = ball2; + assert( ball1 == np1 ); + assert( ball2 == np2 ); + assert( nullptr != np1 ); + assert( nullptr != np2 ); + + op1 = np1; + OwnerPtr op2( np2 ); + assert( np1 == op1 ); + assert( np2 == op2 ); + assert( np2 != op1 ); + assert( np1 != op2 ); + + op1.Swap( op2 ); + + assert( ball1 != op1 ); // Owners do not point to original objects. + assert( ball2 != op2 ); + assert( ball2 == op1 ); // Owners now point to different objects. + assert( ball1 == op2 ); + assert( ball1 != np2 ); // Non-owners still point to original objects. + assert( ball2 != np1 ); + assert( ball2 == np2 ); // Non-owners do not point to different objects. + assert( ball1 == np1 ); + assert( np1 != op1 ); + assert( np2 != op2 ); + assert( np2 == op1 ); + assert( np1 == op2 ); + assert( nullptr != op1 ); + assert( nullptr != op2 ); + assert( nullptr != np1 ); + assert( nullptr != np2 ); + op1 = nullptr; + } + assert( Ball::AllDestroyed() ); + + // Test the Swap function for weak co-pointers. + { + NonOwnerPtr np1; + NonOwnerPtr np2; + Ball * ball1 = new Ball; + Ball * ball2 = new Ball; + np1 = ball1; + np2 = ball2; + assert( ball1 == np1 ); + assert( ball2 == np2 ); + assert( nullptr != np1 ); + assert( nullptr != np2 ); + + op1 = np1; + OwnerPtr op2( np2 ); + assert( np1 == op1 ); + assert( np2 == op2 ); + assert( np2 != op1 ); + assert( np1 != op2 ); + + np1.Swap( np2 ); + + assert( ball1 == op1 ); // Owners still point to their original objects. + assert( ball2 == op2 ); + assert( ball2 != op1 ); // Owners do not point to different objects. + assert( ball1 != op2 ); + assert( ball1 == np2 ); // Non-owners now point to different objects. + assert( ball2 == np1 ); + assert( ball2 != np2 ); // Non-owners do not point to their original objects. + assert( ball1 != np1 ); + assert( np1 != op1 ); + assert( np2 != op2 ); + assert( np2 == op1 ); + assert( np1 == op2 ); + assert( nullptr != op1 ); + assert( nullptr != op2 ); + assert( nullptr != np1 ); + assert( nullptr != np2 ); + op1 = nullptr; + } + assert( Ball::AllDestroyed() ); + + // A Swap of a weak pointer with a strong pointer will cause a compiler error. +} + +// ---------------------------------------------------------------------------- + +void DoSingleOwnerTests( void ) +{ + + // These tests exercise the lockable single-owner StrongPtr policies. + { + Ball * baseball = new Ball; + Ball * football = new Ball; + Ball * softball = new Ball; + Lockable_Owner_Counted_BallPtr op1; + Lockable_NonOwner_Counted_BallPtr pBaseball( baseball ); + Lockable_NonOwner_Counted_BallPtr pFootball( football ); + Lockable_NonOwner_Counted_BallPtr pSoftball( softball ); + Teenager teen1; + Teenager teen2; + Teenager teen3; + DoSingleOwnerTest( op1, pBaseball, pFootball, pSoftball, + teen1, teen2, teen3 ); + } + assert( Ball::AllDestroyed() ); + + // These tests exercise the reference-counted single-owner StrongPtr policies. + { + Ball * soccerBall = new Ball; + Ball * bouncyBall = new Ball; + Ball * basketBall = new Ball; + + NonOwner_Counted_BallPtr pSoccerBall( soccerBall ); + NonOwner_Counted_BallPtr pBouncyBall( bouncyBall ); + NonOwner_Counted_BallPtr pBasketBall( basketBall ); + Toddler child1; + Toddler child2; + Toddler child3; + Owner_Counted_BallPtr op1; + DoSingleOwnerTest( op1, pSoccerBall, pBouncyBall, pBasketBall, + child1, child2, child3 ); + } + assert( Ball::AllDestroyed() ); + + // These tests exercise the linked-cycle single-owner StrongPtr policies. + { + Ball * yarnBall = new Ball; + Ball * twineBall = new Ball; + Ball * stringBall = new Ball; + + NonOwner_Linked_BallPtr pYarnBall( yarnBall ); + NonOwner_Linked_BallPtr pTwineBall( twineBall ); + NonOwner_Linked_BallPtr pStringBall( stringBall ); + Kitten cat1; + Kitten cat2; + Kitten cat3; + Owner_Linked_BallPtr op1; + DoSingleOwnerTest( op1, pYarnBall, pTwineBall, pStringBall, + cat1, cat2, cat3 ); + } + assert( Ball::AllDestroyed() ); +} + +// ---------------------------------------------------------------------------- + void DoStrongForwardReferenceTest( void ) { /** @note These lines should cause the compiler to make a warning message about attempting to delete an undefined type. They should also cause - an error message about a negative subscript since + an error message about a negative subscript since */ //Thingy_DeleteSingle_ptr p1; //Thingy_DeleteSingle_ptr p2( p1 ); @@ -1064,15 +1656,15 @@ { }; typedef Loki::StrongPtr -< +< BaseClass, false, TwoRefCounts, DisallowConversion, - AssertCheck, CantResetWithStrong, DeleteSingle, DontPropagateConst + AssertCheck, CantResetWithStrong, DeleteSingle, DontPropagateConst > Ptr; bool Compare( const Ptr&, const Ptr&) { - return true; + return true; } void friend_handling2() @@ -1497,6 +2089,3 @@ // this will not compile: //int i = f1.i; } - - - This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ric...@us...> - 2011-09-20 17:52:17
|
Revision: 1106 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1106&view=rev Author: rich_sposato Date: 2011-09-20 17:52:11 +0000 (Tue, 20 Sep 2011) Log Message: ----------- Added ability for StrongPtr to handle arrays. Modified Paths: -------------- trunk/test/SmartPtr/strong.cpp Modified: trunk/test/SmartPtr/strong.cpp =================================================================== --- trunk/test/SmartPtr/strong.cpp 2011-09-20 17:51:46 UTC (rev 1105) +++ trunk/test/SmartPtr/strong.cpp 2011-09-20 17:52:11 UTC (rev 1106) @@ -1781,7 +1781,7 @@ namespace { - class Feline + class Feline : public BaseClass { public: virtual ~Feline() {} @@ -1796,7 +1796,12 @@ 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 @@ -2089,3 +2094,164 @@ // this will not compile: //int i = f1.i; } + +// ---------------------------------------------------------------------------- + +/// Use these typedefs to test DeleteArray policy. + +typedef Loki::StrongPtr< Tiger, true, TwoRefCounts, DisallowConversion, + AssertCheck, CantResetWithStrong, DeleteArray, DontPropagateConst > + TigerArray_2RefCounts_ptr; + +typedef Loki::StrongPtr< Tiger, true, TwoRefLinks, DisallowConversion, + AssertCheck, CantResetWithStrong, DeleteArray, DontPropagateConst > + TigerArray_2RefLinks_ptr; + +typedef Loki::StrongPtr< Tiger, true, LockableTwoRefCounts, DisallowConversion, + AssertCheck, CantResetWithStrong, DeleteArray, DontPropagateConst > + TigerArray_Lock2RefCounts_ptr; + +// ---------------------------------------------------------------------------- + +void DoStrongArrayTests( void ) +{ + cout << "Starting DoStrongArrayTests." << endl; + + { + // test default construction. + TigerArray_2RefCounts_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_2RefCounts_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_2RefCounts_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_2RefCounts_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 DoStrongArrayTests." << endl; +} + +// ---------------------------------------------------------------------------- This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ric...@us...> - 2011-10-03 04:52:58
|
Revision: 1130 http://loki-lib.svn.sourceforge.net/loki-lib/?rev=1130&view=rev Author: rich_sposato Date: 2011-10-03 04:52:52 +0000 (Mon, 03 Oct 2011) Log Message: ----------- Added void lines to remove compiler warnings. Modified Paths: -------------- trunk/test/SmartPtr/strong.cpp Modified: trunk/test/SmartPtr/strong.cpp =================================================================== --- trunk/test/SmartPtr/strong.cpp 2011-10-03 04:39:17 UTC (rev 1129) +++ trunk/test/SmartPtr/strong.cpp 2011-10-03 04:52:52 UTC (rev 1130) @@ -2113,10 +2113,12 @@ try { Tiger & p4 = sp2[ 4 ]; + (void)p4; assert( false ); } catch ( const ::std::out_of_range & ex ) { + (void)ex; assert( true ); } @@ -2124,10 +2126,12 @@ try { Tiger & p8 = sp1[ 8 ]; + (void)p8; assert( false ); } catch ( const ::std::out_of_range & ex ) { + (void)ex; assert( true ); } @@ -2156,20 +2160,24 @@ try { Tiger & p4 = sp1[ 4 ]; + (void)p4; assert( false ); } catch ( const ::std::out_of_range & ex ) { + (void)ex; assert( true ); } try { Tiger & p8 = sp2[ 8 ]; + (void)p8; assert( false ); } catch ( const ::std::out_of_range & ex ) { + (void)ex; assert( true ); } @@ -2179,10 +2187,12 @@ try { const Tiger & p4 = sp3[ 4 ]; + (void)p4; assert( false ); } catch ( const ::std::out_of_range & ex ) { + (void)ex; assert( true ); } @@ -2192,10 +2202,12 @@ try { const Tiger & p8 = sp5[ 8 ]; + (void)p8; assert( false ); } catch ( const ::std::out_of_range & ex ) { + (void)ex; assert( true ); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |