From 123b9fca17be3959b2b9defee775bf0d79e0c8c4 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Tue, 1 Mar 2011 15:59:45 +0100 Subject: [PATCH] Unittest of Singleton.hpp is ready --- .gitignore | 2 + test/CMakeLists.txt | 5 +- test/run_test.sh | 5 +- test/test_Singelton.hpp | 99 +++++++++++++++++++++++++++++++++++++--- test/test_threadpool.hpp | 2 +- 5 files changed, 104 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index e09d3ea..33fc9db 100644 --- a/.gitignore +++ b/.gitignore @@ -6,8 +6,10 @@ *.so */cov */lcov.info +*/lcov2.info */core *.kdev_include_paths test/generated_main.cpp test/test leak.log + diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 868e5a8..bc9357b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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() diff --git a/test/run_test.sh b/test/run_test.sh index d87f7b0..53f62d1 100755 --- a/test/run_test.sh +++ b/test/run_test.sh @@ -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 diff --git a/test/test_Singelton.hpp b/test/test_Singelton.hpp index b578799..2914de9 100644 --- a/test/test_Singelton.hpp +++ b/test/test_Singelton.hpp @@ -1,13 +1,100 @@ + + +// gpp ./generated_main.cpp ../src/*.cpp -I../include -lpthread -lrt + + #include +#define private public // need to reach Singleton's private m_instance + #include "Singleton.hpp" +#include "Common.hpp" -class MyTestSuite : public CxxTest::TestSuite +class TestSingletonSuite : public CxxTest::TestSuite { + + class BasicSingleton : public Singleton + { + public: + int getSeven() { return 7; } + }; + public: - void testAddition( void ) - { - TS_ASSERT( 1 + 1 > 1 ); - TS_ASSERT_EQUALS( 1 + 1, 2 ); - } + + 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); + } + +/** @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: + int fv() + { + TRACE(this); + return EvilClass::getInstance()->getValue(); + } + }; + + class EvilClass : public Singleton + { + 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(new DummyClass()); + TS_ASSERT_EQUALS ( tm.fv(), 13 ); + + EvilClass::destroy(); + } + }; diff --git a/test/test_threadpool.hpp b/test/test_threadpool.hpp index 916aed3..ba37009 100644 --- a/test/test_threadpool.hpp +++ b/test/test_threadpool.hpp @@ -5,7 +5,7 @@ #include "Common.hpp" -class MyTestSuite : public CxxTest::TestSuite +class TestThreadPoolSuite : public CxxTest::TestSuite { public: