aria2/test/SingletonHolderTest.cc
Tatsuhiro Tsujikawa 3505201f33 2008-04-20 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
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
2008-04-20 00:50:22 +00:00

61 lines
1.3 KiB
C++

#include "SingletonHolder.h"
#include "SharedHandle.h"
#include <iostream>
#include <cppunit/extensions/HelperMacros.h>
namespace aria2 {
class SingletonHolderTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(SingletonHolderTest);
CPPUNIT_TEST(testInstance);
CPPUNIT_TEST_SUITE_END();
private:
public:
void setUp() {
}
void testInstance();
};
CPPUNIT_TEST_SUITE_REGISTRATION( SingletonHolderTest );
class M {
private:
std::string _greeting;
public:
M(const std::string& greeting):_greeting(greeting) {}
const std::string& greeting() const { return _greeting; }
void greeting(const std::string& greeting) {
_greeting = greeting;
}
};
typedef SharedHandle<M> MHandle;
typedef SharedHandle<int> IntHandle;
void SingletonHolderTest::testInstance()
{
MHandle m(new M("Hello world."));
SingletonHolder<MHandle>::instance(m);
std::cerr << SingletonHolder<MHandle>::instance()->greeting() << std::endl;
SingletonHolder<MHandle>::instance()->greeting("Yes, it worked!");
std::cerr << SingletonHolder<MHandle>::instance()->greeting() << std::endl;
IntHandle i(new int(100));
SingletonHolder<IntHandle>::instance(i);
std::cerr << SingletonHolder<IntHandle>::instance() << std::endl;
std::cerr << SingletonHolder<MHandle>::instance()->greeting() << std::endl;
}
} // namespace aria2