#include "common.h" #include "SharedHandle.h" #include #include #include namespace aria2 { class SharedHandleTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(SharedHandleTest); CPPUNIT_TEST(testSharedHandle); CPPUNIT_TEST(testWeakHandle); CPPUNIT_TEST_SUITE_END(); static SharedHandle staticHandle; public: void setUp() { } static SharedHandle getInstance() { if(staticHandle.isNull()) { staticHandle.reset(new int(1)); } return staticHandle; } void testSharedHandle(); void testWeakHandle(); }; SharedHandle SharedHandleTest::staticHandle; CPPUNIT_TEST_SUITE_REGISTRATION( SharedHandleTest ); void SharedHandleTest::testSharedHandle() { std::cout << "xh:" << std::endl; SharedHandle xh(new int(1)); CPPUNIT_ASSERT_EQUAL((size_t)1, xh.getRefCount()); std::cout << "nullHandle:" << std::endl; SharedHandle nullHandle; CPPUNIT_ASSERT_EQUAL((size_t)1, nullHandle.getRefCount()); std::cout << "staticHandle:" << std::endl; CPPUNIT_ASSERT_EQUAL((size_t)1, staticHandle.getRefCount()); SharedHandle localStaticHandle = getInstance(); CPPUNIT_ASSERT_EQUAL((size_t)2, localStaticHandle.getRefCount()); } void SharedHandleTest::testWeakHandle() { SharedHandle x; x.reset(new int(1)); WeakHandle y = x; WeakHandle z; z = y; std::cout << "z.getRefCount() = " << z.getRefCount() << std::endl; y.reset(); z.reset(); std::cout << "z.getRefCount() = " << z.getRefCount() << std::endl; std::cout << "x.getRefCount() = " << x.getRefCount() << std::endl; SharedHandle w; x = w; std::cout << "w.getRefCount() = " << w.getRefCount() << std::endl; std::cout << "x.getRefCount() = " << x.getRefCount() << std::endl; } } // namespace aria2