mirror of
https://github.com/aria2/aria2.git
synced 2024-12-14 17:39:11 +00:00
3505201f33
Rewritten SharedHandle. Now copy constructor taking raw pointer has keyword explicit and SharedHandle's default constructor initializes its internal obj to null, old implementation initializes it using obj's default constructor. To assign null, write SharedHandle<T> x(...); x.reset(); TODO: test/SharedHandleTest.cc needs more tests. * src/SharedHandle.h
83 lines
1.8 KiB
C++
83 lines
1.8 KiB
C++
#include "common.h"
|
|
#include "SharedHandle.h"
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
namespace aria2 {
|
|
|
|
class SharedHandleTest:public CppUnit::TestFixture {
|
|
|
|
CPPUNIT_TEST_SUITE(SharedHandleTest);
|
|
CPPUNIT_TEST(testSharedHandle);
|
|
CPPUNIT_TEST(testWeakHandle);
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
static SharedHandle<int> staticHandle;
|
|
public:
|
|
void setUp() {
|
|
}
|
|
|
|
static SharedHandle<int> getInstance() {
|
|
if(staticHandle.isNull()) {
|
|
staticHandle.reset(new int(1));
|
|
}
|
|
return staticHandle;
|
|
}
|
|
|
|
void testSharedHandle();
|
|
void testWeakHandle();
|
|
};
|
|
|
|
SharedHandle<int> SharedHandleTest::staticHandle;
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( SharedHandleTest );
|
|
|
|
void SharedHandleTest::testSharedHandle() {
|
|
std::cout << "xh:" << std::endl;
|
|
SharedHandle<int> xh(new int(1));
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, xh.getRefCount());
|
|
|
|
std::cout << "nullHandle:" << std::endl;
|
|
SharedHandle<int> nullHandle;
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, nullHandle.getRefCount());
|
|
|
|
std::cout << "staticHandle:" << std::endl;
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, staticHandle.getRefCount());
|
|
|
|
SharedHandle<int> localStaticHandle = getInstance();
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, localStaticHandle.getRefCount());
|
|
}
|
|
|
|
void SharedHandleTest::testWeakHandle()
|
|
{
|
|
SharedHandle<int> x;
|
|
x.reset(new int(1));
|
|
|
|
WeakHandle<int> y = x;
|
|
|
|
WeakHandle<int> 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<int> w;
|
|
|
|
x = w;
|
|
|
|
std::cout << "w.getRefCount() = " << w.getRefCount() << std::endl;
|
|
std::cout << "x.getRefCount() = " << x.getRefCount() << std::endl;
|
|
|
|
}
|
|
|
|
} // namespace aria2
|