Added tests for bug 3224518.

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@1095 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
rich_sposato 2011-09-07 22:50:38 +00:00
parent 2e1bf19bdd
commit 9fe5e76c73

View file

@ -54,7 +54,7 @@ unsigned int MimicCOM::s_destructions = 0;
class Thingy; class Thingy;
typedef Loki::SmartPtr< Thingy, RefCounted, DisallowConversion, typedef Loki::SmartPtr< Thingy, RefCounted, DisallowConversion,
AssertCheck, DefaultSPStorage, PropagateConst > NoCheck, DefaultSPStorage, PropagateConst >
Thingy_DefaultStorage_ptr; Thingy_DefaultStorage_ptr;
typedef Loki::SmartPtr< Thingy, RefCounted, DisallowConversion, typedef Loki::SmartPtr< Thingy, RefCounted, DisallowConversion,
@ -69,18 +69,23 @@ typedef Loki::SmartPtr< BaseClass, RefCounted, DisallowConversion,
AssertCheck, DefaultSPStorage, DontPropagateConst > AssertCheck, DefaultSPStorage, DontPropagateConst >
NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr; NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr;
/// @note These 3 are used for testing const policies. /// @note These 4 are used for testing const policies.
typedef Loki::SmartPtr< const BaseClass, RefCounted, DisallowConversion,
AssertCheck, DefaultSPStorage, DontPropagateConst >
ConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr;
typedef Loki::SmartPtr< const BaseClass, RefCounted, DisallowConversion,
AssertCheck, DefaultSPStorage, PropagateConst >
ConstBase_RefCount_NoConvert_Assert_Propagate_ptr;
typedef Loki::SmartPtr< BaseClass, RefCounted, DisallowConversion, typedef Loki::SmartPtr< BaseClass, RefCounted, DisallowConversion,
AssertCheck, DefaultSPStorage, PropagateConst > NoCheck, DefaultSPStorage, DontPropagateConst >
NonConstBase_RefCount_NoConvert_Assert_Propagate_ptr; NonConstBase_RefCount_NoConvert_NoCheck_DontPropagate_ptr;
typedef Loki::SmartPtr< const BaseClass, RefCounted, DisallowConversion,
NoCheck, DefaultSPStorage, DontPropagateConst >
ConstBase_RefCount_NoConvert_NoCheck_DontPropagate_ptr;
typedef Loki::SmartPtr< const BaseClass, RefCounted, DisallowConversion,
NoCheck, DefaultSPStorage, PropagateConst >
ConstBase_RefCount_NoConvert_NoCheck_Propagate_ptr;
typedef Loki::SmartPtr< BaseClass, RefCounted, DisallowConversion,
NoCheck, DefaultSPStorage, PropagateConst >
NonConstBase_RefCount_NoConvert_NoCheck_Propagate_ptr;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -135,11 +140,12 @@ typedef Loki::SmartPtr< MimicCOM, COMRefCounted, DisallowConversion,
void DoConstConversionTests( void ) void DoConstConversionTests( void )
{ {
cout << "Starting DoConstConversionTests." << endl;
NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p1; NonConstBase_RefCount_NoConvert_NoCheck_DontPropagate_ptr p1;
ConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p2( p1 ); ConstBase_RefCount_NoConvert_NoCheck_DontPropagate_ptr p2( p1 );
ConstBase_RefCount_NoConvert_Assert_Propagate_ptr p3( p1 ); ConstBase_RefCount_NoConvert_NoCheck_Propagate_ptr p3( p1 );
NonConstBase_RefCount_NoConvert_Assert_Propagate_ptr p4( p1 ); NonConstBase_RefCount_NoConvert_NoCheck_Propagate_ptr p4( p1 );
// p1 = p2; // illegal! converts const to non-const. // p1 = p2; // illegal! converts const to non-const.
// p1 = p3; // illegal! converts const to non-const. // p1 = p3; // illegal! converts const to non-const.
@ -153,18 +159,26 @@ void DoConstConversionTests( void )
p4 = p1; // legal, but dubious. Changes const-propagation policy. p4 = p1; // legal, but dubious. Changes const-propagation policy.
// p4 = p2; // illegal! converts const to non-const. // p4 = p2; // illegal! converts const to non-const.
// p4 = p3; // illegal! converts const to non-const. // p4 = p3; // illegal! converts const to non-const.
assert( BaseClass::AllDestroyed() );
assert( !BaseClass::ExtraConstructions() );
assert( !BaseClass::ExtraDestructions() );
cout << "Finished DoConstConversionTests." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void DoOwnershipConversionTests( void ) void DoOwnershipConversionTests( void )
{ {
NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p1; cout << "Starting DoOwnershipConversionTests." << endl;
NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p2;
NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p3; {
NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p1( new BaseClass );
NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p2( new BaseClass );
NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p3( new BaseClass );
NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p4( new BaseClass ); NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p4( new BaseClass );
NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p5; NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p5( new BaseClass );
NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p6; NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p6( new BaseClass );
// legal constructions. Each should allow copy with same policies. // legal constructions. Each should allow copy with same policies.
NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p7( p1 ); NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p7( p1 );
@ -177,101 +191,118 @@ void DoOwnershipConversionTests( void )
// NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p12( p6 ); // NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p12( p6 );
// illegal constructions! Can't convert from one ownership policy to another. // illegal constructions! Can't convert from one ownership policy to another.
// NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p13( p2 ); // NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p13( p2 );
// NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p14( p3 ); // NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p14( p3 );
// NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p15( p4 ); // NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p15( p4 );
// NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p16( p5 ); // NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p16( p5 );
// NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p17( p6 ); // NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p17( p6 );
// illegal constructions! Can't convert from one ownership policy to another. // illegal constructions! Can't convert from one ownership policy to another.
// NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p18( p1 ); // NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p18( p1 );
// NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p19( p3 ); // NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p19( p3 );
// NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p20( p4 ); // NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p20( p4 );
// NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p21( p5 ); // NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p21( p5 );
// NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p22( p6 ); // NonConstBase_ComRef_NoConvert_Assert_DontPropagate_ptr p22( p6 );
// illegal constructions! Can't convert from one ownership policy to another. // illegal constructions! Can't convert from one ownership policy to another.
// NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p23( p1 ); // NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p23( p1 );
// NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p24( p2 ); // NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p24( p2 );
// NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p25( p4 ); // NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p25( p4 );
// NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p26( p5 ); // NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p26( p5 );
// NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p27( p6 ); // NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p27( p6 );
// illegal constructions! Can't convert from one ownership policy to another. // illegal constructions! Can't convert from one ownership policy to another.
// NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p28( p1 ); // NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p28( p1 );
// NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p29( p2 ); // NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p29( p2 );
// NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p30( p3 ); // NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p30( p3 );
// NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p31( p5 ); // NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p31( p5 );
// NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p32( p6 ); // NonConstBase_DeepCopy_NoConvert_Assert_DontPropagate_ptr p32( p6 );
// illegal constructions! Can't convert from one ownership policy to another. // illegal constructions! Can't convert from one ownership policy to another.
// NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p33( p1 ); // NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p33( p1 );
// NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p34( p2 ); // NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p34( p2 );
// NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p35( p3 ); // NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p35( p3 );
// NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p36( p4 ); // NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p36( p4 );
// NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p37( p6 ); // NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p37( p6 );
// illegal constructions! Can't convert from one ownership policy to another. // illegal constructions! Can't convert from one ownership policy to another.
// NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p38( p1 ); // NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p38( p1 );
// NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p39( p2 ); // NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p39( p2 );
// NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p40( p3 ); // NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p40( p3 );
// NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p41( p4 ); // NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p41( p4 );
// NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p42( p5 ); // NonConstBase_NoCopy_NoConvert_Assert_DontPropagate_ptr p42( p5 );
}
assert( BaseClass::AllDestroyed() );
assert( !BaseClass::ExtraConstructions() );
assert( !BaseClass::ExtraDestructions() );
{
// These constructions are legal because the preserve const-ness. // These constructions are legal because the preserve const-ness.
ConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p43( p5 );
ConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p44( p43 );
const BaseClass * rawP = new BaseClass; const BaseClass * rawP = new BaseClass;
ConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p43( rawP );
ConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p44( p43 );
rawP = new BaseClass;
// These constructions are illegal because the don't preserve constness. // These constructions are illegal because the don't preserve constness.
// NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p45( rawP ); // NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p45( rawP );
// NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p46( p43 ); // NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p46( p43 );
ConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p47( rawP ); ConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p47( rawP );
NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p48; NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p48;
NonConstBase_KillCopy_NoConvert_Assert_DontPropagate_ptr p49( new BaseClass );
// This assignment is legal because it preserves constness. // This assignment is legal because it preserves constness.
p48 = p5; p48 = p49;
// This assignment is illegal because it won't preserve constness. // This assignment is illegal because it won't preserve constness.
// p48 = p43; // p48 = p43;
// illegal assignements! Can't convert from one ownership policy to another. // illegal assignments! Can't convert from one ownership policy to another.
// p1 = p2; // p1 = p2;
// p1 = p3; // p1 = p3;
// p1 = p4; // p1 = p4;
// p1 = p5; // p1 = p5;
// p1 = p6; // p1 = p6;
// p2 = p1; // p2 = p1;
// p2 = p3; // p2 = p3;
// p2 = p4; // p2 = p4;
// p2 = p5; // p2 = p5;
// p2 = p6; // p2 = p6;
// p3 = p1; // p3 = p1;
// p3 = p2; // p3 = p2;
// p3 = p4; // p3 = p4;
// p3 = p5; // p3 = p5;
// p3 = p6; // p3 = p6;
// p4 = p1; // p4 = p1;
// p4 = p2; // p4 = p2;
// p4 = p3; // p4 = p3;
// p4 = p5; // p4 = p5;
// p4 = p6; // p4 = p6;
// p5 = p1; // p5 = p1;
// p5 = p2; // p5 = p2;
// p5 = p3; // p5 = p3;
// p5 = p4; // p5 = p4;
// p5 = p6; // p5 = p6;
// p6 = p1; // p6 = p1;
// p6 = p2; // p6 = p2;
// p6 = p3; // p6 = p3;
// p6 = p4; // p6 = p4;
// p6 = p5; // p6 = p5;
}
assert( BaseClass::AllDestroyed() );
assert( !BaseClass::ExtraConstructions() );
assert( !BaseClass::ExtraDestructions() );
cout << "Finished DoOwnershipConversionTests." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void DoInheritanceConversionTests( void ) void DoInheritanceConversionTests( void )
{ {
cout << "Starting DoInheritanceConversionTests." << endl;
{
NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p1; NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p1;
PublicSub_RefCount_NoConvert_Assert_DontPropagate_ptr p2; PublicSub_RefCount_NoConvert_Assert_DontPropagate_ptr p2( new PublicSubClass );
PrivateSub_RefCount_NoConvert_Assert_DontPropagate_ptr p3; PrivateSub_RefCount_NoConvert_Assert_DontPropagate_ptr p3;
p1 = p2; // legal. Cast to public base class allowed. p1 = p2; // legal. Cast to public base class allowed.
@ -291,12 +322,21 @@ void DoInheritanceConversionTests( void )
// PublicSub_RefCount_NoConvert_Assert_DontPropagate_ptr p7( p3 ); // PublicSub_RefCount_NoConvert_Assert_DontPropagate_ptr p7( p3 );
// PrivateSub_RefCount_NoConvert_Assert_DontPropagate_ptr p8( p1 ); // PrivateSub_RefCount_NoConvert_Assert_DontPropagate_ptr p8( p1 );
// PrivateSub_RefCount_NoConvert_Assert_DontPropagate_ptr p9( p2 ); // PrivateSub_RefCount_NoConvert_Assert_DontPropagate_ptr p9( p2 );
}
assert( BaseClass::AllDestroyed() );
assert( !BaseClass::ExtraConstructions() );
assert( !BaseClass::ExtraDestructions() );
cout << "Finished DoInheritanceConversionTests." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void DoRefCountSwapTests( void ) void DoRefCountSwapTests( void )
{ {
cout << "Starting DoRefCountSwapTests." << endl;
{
NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p1( new BaseClass ); NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p1( new BaseClass );
NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p2( new BaseClass ); NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p2( new BaseClass );
@ -355,13 +395,21 @@ void DoRefCountSwapTests( void )
assert( p2 == p3 ); assert( p2 == p3 );
assert( p2 != p4 ); assert( p2 != p4 );
assert( p2 != p1 ); assert( p2 != p1 );
}
assert( BaseClass::AllDestroyed() );
assert( !BaseClass::ExtraConstructions() );
assert( !BaseClass::ExtraDestructions() );
cout << "Finished DoRefCountSwapTests." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void DoRefLinkSwapTests( void ) void DoRefLinkSwapTests( void )
{ {
cout << "Starting DoRefLinkSwapTests." << endl;
{
BaseClass * pBaseClass = new BaseClass; BaseClass * pBaseClass = new BaseClass;
NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p1( pBaseClass ); NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p1( pBaseClass );
NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p2( new BaseClass ); NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p2( new BaseClass );
@ -916,12 +964,19 @@ void DoRefLinkSwapTests( void )
assert( p8 == p8 ); assert( p8 == p8 );
assert( p9 == p9 ); assert( p9 == p9 );
assert( pA == pA ); assert( pA == pA );
}
assert( BaseClass::AllDestroyed() );
assert( !BaseClass::ExtraConstructions() );
assert( !BaseClass::ExtraDestructions() );
cout << "Finished DoRefLinkSwapTests." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void DoRefLinkTests( void ) void DoRefLinkTests( void )
{ {
cout << "Starting DoRefLinkTests." << endl;
const unsigned int ctorCount = BaseClass::GetCtorCount(); (void) ctorCount; const unsigned int ctorCount = BaseClass::GetCtorCount(); (void) ctorCount;
const unsigned int dtorCount = BaseClass::GetDtorCount(); (void) dtorCount; const unsigned int dtorCount = BaseClass::GetDtorCount(); (void) dtorCount;
@ -951,12 +1006,18 @@ void DoRefLinkTests( void )
assert( dtorCount + 2 == BaseClass::GetDtorCount() ); assert( dtorCount + 2 == BaseClass::GetDtorCount() );
assert( BaseClass::GetCtorCount() == BaseClass::GetDtorCount() ); assert( BaseClass::GetCtorCount() == BaseClass::GetDtorCount() );
assert( BaseClass::AllDestroyed() );
assert( !BaseClass::ExtraConstructions() );
assert( !BaseClass::ExtraDestructions() );
cout << "Finished DoRefLinkTests." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void DoRefCountNullPointerTests( void ) void DoRefCountNullPointerTests( void )
{ {
cout << "Starting DoRefCountNullPointerTests." << endl;
BaseClass * pNull = NULL; (void) pNull; BaseClass * pNull = NULL; (void) pNull;
const unsigned int ctorCount = BaseClass::GetCtorCount(); (void) ctorCount; const unsigned int ctorCount = BaseClass::GetCtorCount(); (void) ctorCount;
const unsigned int dtorCount = BaseClass::GetDtorCount(); (void) dtorCount; const unsigned int dtorCount = BaseClass::GetDtorCount(); (void) dtorCount;
@ -965,7 +1026,7 @@ void DoRefCountNullPointerTests( void )
{ {
NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p0; NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p0;
NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p1; NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p1;
NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p2( p0 ); NonConstBase_RefCount_NoConvert_Assert_DontPropagate_ptr p2;
assert( !p0 ); assert( !p0 );
assert( !p1 ); assert( !p1 );
@ -1005,12 +1066,19 @@ void DoRefCountNullPointerTests( void )
} }
assert( ctorCount == BaseClass::GetCtorCount() ); assert( ctorCount == BaseClass::GetCtorCount() );
assert( dtorCount == BaseClass::GetDtorCount() ); assert( dtorCount == BaseClass::GetDtorCount() );
assert( BaseClass::AllDestroyed() );
assert( !BaseClass::ExtraConstructions() );
assert( !BaseClass::ExtraDestructions() );
cout << "Finished DoRefCountNullPointerTests." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void DoRefLinkNullPointerTests( void ) void DoRefLinkNullPointerTests( void )
{ {
cout << "Starting DoRefLinkNullPointerTests." << endl;
BaseClass * pNull = NULL; (void) pNull; BaseClass * pNull = NULL; (void) pNull;
const unsigned int ctorCount = BaseClass::GetCtorCount(); (void) ctorCount; const unsigned int ctorCount = BaseClass::GetCtorCount(); (void) ctorCount;
const unsigned int dtorCount = BaseClass::GetDtorCount(); (void) dtorCount; const unsigned int dtorCount = BaseClass::GetDtorCount(); (void) dtorCount;
@ -1019,7 +1087,7 @@ void DoRefLinkNullPointerTests( void )
{ {
NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p0; NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p0;
NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p1; NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p1;
NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p2( p0 ); NonConstBase_RefLink_NoConvert_Assert_DontPropagate_ptr p2;
assert( !p0 ); assert( !p0 );
assert( !p1 ); assert( !p1 );
@ -1059,12 +1127,18 @@ void DoRefLinkNullPointerTests( void )
} }
assert( ctorCount == BaseClass::GetCtorCount() ); assert( ctorCount == BaseClass::GetCtorCount() );
assert( dtorCount == BaseClass::GetDtorCount() ); assert( dtorCount == BaseClass::GetDtorCount() );
assert( BaseClass::AllDestroyed() );
assert( !BaseClass::ExtraConstructions() );
assert( !BaseClass::ExtraDestructions() );
cout << "Finished DoRefLinkNullPointerTests." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void DoComRefTest( void ) void DoComRefTest( void )
{ {
cout << "Starting DoComRefTest." << endl;
const unsigned int ctorCount = MimicCOM::GetCtorCount(); (void) ctorCount; const unsigned int ctorCount = MimicCOM::GetCtorCount(); (void) ctorCount;
const unsigned int dtorCount = MimicCOM::GetDtorCount(); (void) dtorCount; const unsigned int dtorCount = MimicCOM::GetDtorCount(); (void) dtorCount;
@ -1090,12 +1164,19 @@ void DoComRefTest( void )
} }
assert( ctorCount+2 == MimicCOM::GetCtorCount() ); assert( ctorCount+2 == MimicCOM::GetCtorCount() );
assert( dtorCount+2 == MimicCOM::GetDtorCount() ); assert( dtorCount+2 == MimicCOM::GetDtorCount() );
assert( BaseClass::AllDestroyed() );
assert( !BaseClass::ExtraConstructions() );
assert( !BaseClass::ExtraDestructions() );
cout << "Finished DoComRefTest." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void DoForwardReferenceTest( void ) void DoForwardReferenceTest( void )
{ {
cout << "Starting DoForwardReferenceTest." << endl;
/** @note These lines should cause the compiler to make a warning message /** @note These lines should cause the compiler to make a warning message
about attempting to delete an undefined type. But it should not produce about attempting to delete an undefined type. But it should not produce
any error messages. any error messages.
@ -1112,6 +1193,11 @@ void DoForwardReferenceTest( void )
//Thingy_HeapStorage_ptr p5( p4 ); //Thingy_HeapStorage_ptr p5( p4 );
//Thingy_HeapStorage_ptr p6; //Thingy_HeapStorage_ptr p6;
//p6 = p5; //p6 = p5;
assert( BaseClass::AllDestroyed() );
assert( !BaseClass::ExtraConstructions() );
assert( !BaseClass::ExtraDestructions() );
cout << "Finished DoForwardReferenceTest." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -1122,25 +1208,63 @@ namespace
class Feline class Feline
{ {
public: public:
virtual ~Feline() {}
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 class Lion : public Feline
{ {
public: public:
virtual ~Lion() {} virtual ~Lion() {}
}; virtual Lion * Clone( void ) const { return new Lion( *this ); }
class Tiger : public Feline
{
public:
virtual ~Tiger() {}
}; };
class Dog class Dog
{ {
public: public:
virtual ~Dog() {} virtual ~Dog() {}
virtual Dog * Clone( void ) const { return new Dog( *this ); }
}; };
} }
@ -1149,11 +1273,14 @@ namespace
void DoSmartPtrDynamicCastTests( void ) void DoSmartPtrDynamicCastTests( void )
{ {
typedef ::Loki::SmartPtr< Feline > FelinePtr; cout << "Starting DoSmartPtrDynamicCastTests." << endl;
typedef ::Loki::SmartPtr< Lion > LionPtr;
typedef ::Loki::SmartPtr< Tiger > TigerPtr;
typedef ::Loki::SmartPtr< Dog > DogPtr;
typedef ::Loki::SmartPtr< Feline, RefCounted, DisallowConversion, NoCheck > FelinePtr;
typedef ::Loki::SmartPtr< Tiger, RefCounted, DisallowConversion, NoCheck > TigerPtr;
typedef ::Loki::SmartPtr< Lion, RefCounted, DisallowConversion, NoCheck > LionPtr;
typedef ::Loki::SmartPtr< Dog, RefCounted, DisallowConversion, NoCheck > DogPtr;
{
Feline * feline = new Lion; Feline * feline = new Lion;
Lion * lion = new Lion; Lion * lion = new Lion;
Tiger * tiger = new Tiger; Tiger * tiger = new Tiger;
@ -1227,6 +1354,103 @@ void DoSmartPtrDynamicCastTests( void )
// Can't cast from lion to dog since these animal types are not related. // Can't cast from lion to dog since these animal types are not related.
pDog.DynamicCastFrom( pcLion ); pDog.DynamicCastFrom( pcLion );
assert( !pDog ); assert( !pDog );
}
assert( Feline::AllDestroyed() );
assert( !Feline::ExtraConstructions() );
assert( !Feline::ExtraDestructions() );
cout << "Finished DoSmartPtrDynamicCastTests." << endl;
}
// ----------------------------------------------------------------------------
/// @note Used for testing exception-throwing policy, and for checking for null-pointer dereference.
typedef ::Loki::SmartPtr< Feline, DeepCopy, DisallowConversion, RejectNullStatic, DefaultSPStorage, DontPropagateConst > Feline_DeepCopy_Ptr;
typedef ::Loki::SmartPtr< Tiger, DeepCopy, DisallowConversion, RejectNullStatic, DefaultSPStorage, DontPropagateConst > Tiger_DeepCopy_Ptr;
typedef ::Loki::SmartPtr< Lion, DeepCopy, DisallowConversion, RejectNullStatic, DefaultSPStorage, DontPropagateConst > Lion_DeepCopy_Ptr;
typedef ::Loki::SmartPtr< Dog, DeepCopy, DisallowConversion, RejectNullStatic, DefaultSPStorage, DontPropagateConst > Dog_DeepCopy_Ptr;
// ----------------------------------------------------------------------------
void DoDeepCopyTests( void )
{
cout << "Starting DoDeepCopyTests." << endl;
try
{
const Dog_DeepCopy_Ptr p1( NULL );
Dog_DeepCopy_Ptr p2( p1 );
assert( false );
}
catch ( ... )
{
assert( true );
}
try
{
Dog_DeepCopy_Ptr p1( NULL );
Dog_DeepCopy_Ptr p2( p1 );
assert( false );
}
catch ( ... )
{
assert( true );
}
try
{
const Dog_DeepCopy_Ptr p1( NULL );
Dog_DeepCopy_Ptr p2( NULL );
p2 = p1;
assert( false );
}
catch ( ... )
{
assert( true );
}
try
{
Dog_DeepCopy_Ptr p1( NULL );
Dog_DeepCopy_Ptr p2( NULL );
p2 = p1;
assert( false );
}
catch ( ... )
{
assert( true );
}
try
{
Tiger_DeepCopy_Ptr p1( new Tiger );
Lion_DeepCopy_Ptr p2( NULL );
p2.DynamicCastFrom( p1 );
assert( false );
}
catch ( ... )
{
assert( true );
}
try
{
const Tiger_DeepCopy_Ptr p1( new Tiger );
Lion_DeepCopy_Ptr p2( NULL );
p2.DynamicCastFrom( p1 );
assert( false );
}
catch ( ... )
{
assert( true );
}
assert( Feline::AllDestroyed() );
assert( !Feline::ExtraConstructions() );
assert( !Feline::ExtraDestructions() );
cout << "Finished DoDeepCopyTests." << endl;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -1240,6 +1464,7 @@ int main( int argc, const char * argv[] )
doThreadTest = true; doThreadTest = true;
} }
DoDeepCopyTests();
DoRefLinkTests(); DoRefLinkTests();
DoWeakLeakTest(); DoWeakLeakTest();
DoStrongRefCountTests(); DoStrongRefCountTests();