You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.1 KiB
108 lines
2.1 KiB
14 years ago
|
#include <cxxtest/TestSuite.h>
|
||
|
|
||
14 years ago
|
#define private public // need to reach Singleton's private m_instance
|
||
|
|
||
|
#include "Common.hpp"
|
||
14 years ago
|
#include "Fixture.hpp"
|
||
14 years ago
|
#include "Singleton.hpp"
|
||
|
|
||
14 years ago
|
|
||
14 years ago
|
class TestSingletonSuite : public CxxTest::TestSuite
|
||
14 years ago
|
{
|
||
14 years ago
|
|
||
14 years ago
|
private:
|
||
|
|
||
14 years ago
|
class BasicSingleton : public Singleton<BasicSingleton>
|
||
|
{
|
||
|
public:
|
||
|
int getSeven() { return 7; }
|
||
|
};
|
||
|
|
||
14 years ago
|
public:
|
||
14 years ago
|
|
||
|
void testBasic( void )
|
||
|
{
|
||
14 years ago
|
TEST_HEADER;
|
||
|
|
||
14 years ago
|
// no instance yet
|
||
|
TS_ASSERT_EQUALS (BasicSingleton::getInstance(), (BasicSingleton*)0);
|
||
|
|
||
|
BasicSingleton::createInstance();
|
||
|
TS_ASSERT_DIFFERS (BasicSingleton::getInstance(), (BasicSingleton*)0);
|
||
|
|
||
|
BasicSingleton* p1;
|
||
|
BasicSingleton* p2;
|
||
|
p1 = BasicSingleton::getInstance();
|
||
|
p2 = BasicSingleton::getInstance();
|
||
|
TS_ASSERT_EQUALS ( p1, p2 );
|
||
|
|
||
|
TS_ASSERT_EQUALS( BasicSingleton::getInstance()->getSeven(), 7 );
|
||
|
|
||
|
BasicSingleton::createInstance();
|
||
|
p2 = BasicSingleton::getInstance();
|
||
|
TS_ASSERT_EQUALS ( (void*)p1, (void*)p2 );
|
||
|
|
||
|
BasicSingleton::destroy();
|
||
|
TS_ASSERT_EQUALS (BasicSingleton::getInstance(), (BasicSingleton*)0);
|
||
|
}
|
||
|
|
||
|
/** @note TestMe class has to be unittested, but it uses a singleton: EvilClass
|
||
|
* Which takes way too much time to use in a unittest (it's an LDAP connection,
|
||
|
* calculates for too long, etc)
|
||
|
*
|
||
|
* So we plan to destroy EvilClass and put a DummyClass (which is derived from
|
||
|
* EvilClass and implements the expensice calls with dummy ones) to TestMe
|
||
|
*/
|
||
|
|
||
14 years ago
|
private:
|
||
|
|
||
14 years ago
|
class TestMe
|
||
|
{
|
||
|
public:
|
||
|
int fv()
|
||
|
{
|
||
14 years ago
|
TRACE;
|
||
14 years ago
|
return EvilClass::getInstance()->getValue();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class EvilClass : public Singleton<EvilClass>
|
||
|
{
|
||
|
public:
|
||
|
virtual int getValue(void)
|
||
|
{
|
||
14 years ago
|
TRACE;
|
||
14 years ago
|
return 665;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class DummyClass : public EvilClass
|
||
|
{
|
||
|
public:
|
||
|
int getValue(void)
|
||
|
{
|
||
14 years ago
|
TRACE;
|
||
14 years ago
|
return 13;
|
||
|
}
|
||
|
};
|
||
|
|
||
14 years ago
|
public:
|
||
|
|
||
14 years ago
|
void testAdvancedTest( void )
|
||
|
{
|
||
14 years ago
|
TEST_HEADER;
|
||
|
|
||
14 years ago
|
TestMe tm;
|
||
|
|
||
|
EvilClass::createInstance();
|
||
|
TS_ASSERT_EQUALS ( tm.fv(), 665 );
|
||
|
|
||
|
EvilClass::destroy();
|
||
|
EvilClass::m_instance = dynamic_cast<EvilClass*>(new DummyClass());
|
||
|
TS_ASSERT_EQUALS ( tm.fv(), 13 );
|
||
|
|
||
|
EvilClass::destroy();
|
||
|
}
|
||
|
|
||
14 years ago
|
};
|