Commit 00af577e authored by Andrea Valassi's avatar Andrea Valassi
Browse files

Improve test for shared_from_this


git-svn-id: file:///git/lcgcool.svndb/cool/trunk@19399 4525493e-7705-40b1-a816-d608a930855b
parent 27bfa6e2
...@@ -187,21 +187,14 @@ public: ...@@ -187,21 +187,14 @@ public:
{ {
Derived* pD = new Derived(); Derived* pD = new Derived();
std::cout << "Derived* = " << pD << std::endl; std::cout << "Derived* = " << pD << std::endl;
std::cout << "Base* = " << (Base*)pD << std::endl; std::cout << "Base* = " << (Base*)pD << std::endl;
std::shared_ptr<Base> spB1( (Base*)pD ); std::shared_ptr<Base> spB1( (Base*)pD ); // uses ctor<Base> which is a different method than ctor<Derived> within shared_ptr<Base>!
CPPUNIT_ASSERT_EQUAL_MESSAGE( "CountB1 = 1", 1l, spB1.use_count() ); CPPUNIT_ASSERT_EQUAL_MESSAGE( "CountB1 = 1", 1l, spB1.use_count() );
CPPUNIT_ASSERT_EQUAL_MESSAGE( "Derived*", pD, dynamic_cast<Derived*>( spB1.get() ) ); CPPUNIT_ASSERT_EQUAL_MESSAGE( "Derived*", pD, dynamic_cast<Derived*>( spB1.get() ) );
std::cout << "... about to throw?" << std::endl; CPPUNIT_ASSERT_THROW( pD->shared_from_this(), std::bad_weak_ptr ); // I guess this throws because ctor<Derived> was not called for this...
std::shared_ptr<Base> spB2 = pD->shared_from_this(); // throws bad_weak_ptr CPPUNIT_ASSERT_EQUAL_MESSAGE( "CountB1 = 1", 1l, spB1.use_count() );
std::cout << "... did not throw?" << std::endl;
CPPUNIT_ASSERT_EQUAL_MESSAGE( "CountB1 = 2", 2l, spB1.use_count() );
CPPUNIT_ASSERT_EQUAL_MESSAGE( "CountB2 = 2", 2l, spB2.use_count() );
spB1.reset(); spB1.reset();
CPPUNIT_ASSERT_EQUAL_MESSAGE( "CountB1 = 0", 0l, spB1.use_count() ); CPPUNIT_ASSERT_EQUAL_MESSAGE( "CountB1 = 0", 0l, spB1.use_count() );
CPPUNIT_ASSERT_EQUAL_MESSAGE( "CountB2 = 1", 1l, spB2.use_count() );
spB2.reset();
CPPUNIT_ASSERT_EQUAL_MESSAGE( "CountB2 = 0", 0l, spB2.use_count() );
} }
#endif #endif
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment