Unittest of Singleton.hpp is ready

master
Denes Matetelki 14 years ago
parent 9834554c17
commit 123b9fca17

2
.gitignore vendored

@ -6,8 +6,10 @@
*.so
*/cov
*/lcov.info
*/lcov2.info
*/core
*.kdev_include_paths
test/generated_main.cpp
test/test
leak.log

@ -6,6 +6,9 @@ if(CXXTEST_FOUND)
set(CXXTEST_USE_PERL TRUE)
include_directories(${CXXTEST_INCLUDE_DIR} ../include)
enable_testing()
CXXTEST_ADD_TEST(test generated_main.cpp test_threadpool.hpp )
CXXTEST_ADD_TEST(test generated_main.cpp
test_threadpool.hpp
test_Singelton.hpp
)
target_link_libraries(test CppUtils gcov)
endif()

@ -18,11 +18,14 @@ valgrind --log-file=leak.log --leak-check=full --show-reachable=yes --show-below
echo -e "${pre}Capture coverage info${post}"
lcov --directory ../build --capture -o lcov.info
# some classes are not used in the lib yet
lcov --directory ../test --capture -o lcov2.info
cat lcov2.info >> lcov.info
echo -e "${pre}Filtering coverage tracefile${post}"
lcov -r lcov.info "g++-v*" -o lcov.info
lcov -r lcov.info "/usr/include/cxxtest*" -o lcov.info
lcov -r lcov.info "*cpp_utils/test*" -o lcov.info
echo -e "${pre}Generating HTML${post}"
rm -rf ./cov

@ -1,13 +1,100 @@
// gpp ./generated_main.cpp ../src/*.cpp -I../include -lpthread -lrt
#include <cxxtest/TestSuite.h>
#define private public // need to reach Singleton's private m_instance
#include "Singleton.hpp"
#include "Common.hpp"
class TestSingletonSuite : public CxxTest::TestSuite
{
class BasicSingleton : public Singleton<BasicSingleton>
{
public:
int getSeven() { return 7; }
};
public:
void testBasic( void )
{
// 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);
}
class MyTestSuite : public CxxTest::TestSuite
/** @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
*/
class TestMe
{
public:
void testAddition( void )
int fv()
{
TS_ASSERT( 1 + 1 > 1 );
TS_ASSERT_EQUALS( 1 + 1, 2 );
TRACE(this);
return EvilClass::getInstance()->getValue();
}
};
class EvilClass : public Singleton<EvilClass>
{
public:
virtual int getValue(void)
{
TRACE(this);
return 665;
}
};
class DummyClass : public EvilClass
{
public:
int getValue(void)
{
TRACE(this);
return 13;
}
};
void testAdvancedTest( void )
{
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();
}
};

@ -5,7 +5,7 @@
#include "Common.hpp"
class MyTestSuite : public CxxTest::TestSuite
class TestThreadPoolSuite : public CxxTest::TestSuite
{
public:

Loading…
Cancel
Save