From 0f69afd01db455a79547b3fd5a12aa23928ba7f4 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sun, 13 Nov 2011 15:37:44 +0100 Subject: [PATCH] cleanup at unittest files --- include/Singleton_DCLP.hpp | 5 +++ include/Singleton_call_once.hpp | 5 +++ {test => other}/test_TimerThreadMultimap.hpp | 0 src/Thread.cpp | 3 ++ test/CMakeLists.txt | 26 ++++++------ test/run_test.sh | 3 +- test/test_PThreadWrappers.hpp | 35 ---------------- test/test_Semaphore.hpp | 5 +++ test/test_Singelton_DCLP.hpp | 35 ---------------- test/test_Singleton_DCLP.hpp | 1 + ..._once.hpp => test_Singleton_call_once.hpp} | 1 + test/test_Thread.hpp | 42 +++++++++++-------- test/valgrind.supp | 13 +++++- 13 files changed, 70 insertions(+), 104 deletions(-) rename {test => other}/test_TimerThreadMultimap.hpp (100%) delete mode 100644 test/test_PThreadWrappers.hpp delete mode 100644 test/test_Singelton_DCLP.hpp rename test/{test_Singelton_call_once.hpp => test_Singleton_call_once.hpp} (94%) diff --git a/include/Singleton_DCLP.hpp b/include/Singleton_DCLP.hpp index 07ead7f..55da169 100644 --- a/include/Singleton_DCLP.hpp +++ b/include/Singleton_DCLP.hpp @@ -39,6 +39,11 @@ public: return m_instance; } + static void destroy() + { + delete m_instance; + } + private: diff --git a/include/Singleton_call_once.hpp b/include/Singleton_call_once.hpp index 117a913..3d8de36 100644 --- a/include/Singleton_call_once.hpp +++ b/include/Singleton_call_once.hpp @@ -24,6 +24,11 @@ public: return m_instance; } + static void destroy() + { + delete m_instance; + } + private: static void do_init() diff --git a/test/test_TimerThreadMultimap.hpp b/other/test_TimerThreadMultimap.hpp similarity index 100% rename from test/test_TimerThreadMultimap.hpp rename to other/test_TimerThreadMultimap.hpp diff --git a/src/Thread.cpp b/src/Thread.cpp index 2f735d0..f4c6f75 100644 --- a/src/Thread.cpp +++ b/src/Thread.cpp @@ -33,6 +33,9 @@ void Thread::start() void* Thread::join() const { TRACE; + if ( !m_isRunning ) + return 0; + void* retVal; pthread_join( m_threadHandler, &retVal ); return retVal; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0b0bfe3..56e6b87 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -18,20 +18,20 @@ if(CXXTEST_FOUND) generated_main.cpp Fixture.hpp -# test_ArgParse.hpp -# test_Singelton_call_once.hpp -# test_Singleton.hpp -# test_Singleton_meyers.hpp -# test_Singleton_DCLP.hpp -# test_Mutex.hpp -# test_ScopedLock.hpp -# test_ConditionalVariable.hpp + test_ArgParse.hpp + test_Common.hpp + test_ConditionalVariable.hpp + test_Multiton.hpp + test_Mutex.hpp + test_ScopedLock.hpp + test_Semaphore.hpp + test_Singleton_DCLP.hpp + test_Singleton_call_once.hpp + # test_Singleton.hpp Cannot test private member, Ficture.hpp loads it + test_Singleton_meyers.hpp test_Thread.hpp -# test_ThreadPool.hpp -# test_Semaphore.hpp -# test_Timer.hpp -# test_Common.hpp -# test_TimerThreadMultimap.hpp + test_ThreadPool.hpp + # test_Timer.hpp Takes too much time&buggy ) target_link_libraries(testCppUtils CppUtils gcov) endif() diff --git a/test/run_test.sh b/test/run_test.sh index 1d4b474..2322f5b 100755 --- a/test/run_test.sh +++ b/test/run_test.sh @@ -65,8 +65,7 @@ valgrind \ $test | tee $test.out; retval=$PIPESTATUS # NOTE to gen suppressions run: -# valgrind --leak-check=full --show-reachable=yes --show-below-main=no --track-origins=yes --num-callers=30 --malloc-fill=0xaa --free-fill=0xdd --gen-suppressions=yes ./test - +# valgrind --leak-check=full --show-reachable=yes --show-below-main=no --track-origins=yes --num-callers=30 --malloc-fill=0xaa --free-fill=0xdd --suppressions=valgrind.supp --gen-suppressions=yes ./testCppUtils # retval is 0 on success # or the number of failed cases diff --git a/test/test_PThreadWrappers.hpp b/test/test_PThreadWrappers.hpp deleted file mode 100644 index 471e30b..0000000 --- a/test/test_PThreadWrappers.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#include - -#define private public // need to reach private variables - -#include "Common.hpp" -#include "Fixture.hpp" -#include "Mutex.hpp" - -class TestPThreadWrappers : public CxxTest::TestSuite -{ - - -public: - - void testMutexBasic( void ) - { - TEST_HEADER; - Mutex m; - m.lock(); - TS_ASSERT_EQUALS ( m.tryLock(0), 0 ); - m.unlock(); - } - - void testMutexCreate( void ) - { - TEST_HEADER; - Mutex m(PTHREAD_MUTEX_ERRORCHECK); - m.lock(); - TS_ASSERT_EQUALS ( m.lock(), 1 ); - - m.unlock(); - } - - -}; diff --git a/test/test_Semaphore.hpp b/test/test_Semaphore.hpp index 36c8933..036be00 100644 --- a/test/test_Semaphore.hpp +++ b/test/test_Semaphore.hpp @@ -34,6 +34,7 @@ private: { TRACE; } + ~ThreadClassWithSemaphore() { TRACE; } @@ -94,6 +95,8 @@ public: TS_ASSERT_EQUALS( t2->release(), true ); TS_ASSERT_EQUALS( semaphore.getCount(), 1 ); + + // t2 releases instead of the using t1 TS_ASSERT_EQUALS( t2->release(), true ); TS_ASSERT_EQUALS( semaphore.getCount(), 2 ); TS_ASSERT_EQUALS( t2->release(), false ); @@ -102,6 +105,8 @@ public: t2->stop(); t1->join(); t2->join(); + + sleep(1); delete t1; delete t2; } diff --git a/test/test_Singelton_DCLP.hpp b/test/test_Singelton_DCLP.hpp deleted file mode 100644 index d5089b3..0000000 --- a/test/test_Singelton_DCLP.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#include - -#define private public // need to reach Singleton's private m_instance - -#include "Common.hpp" -#include "Fixture.hpp" -#include "Singleton_DCLP.hpp" - - -class TestSingletonDCLPSuite : public CxxTest::TestSuite -{ - -private: - - class BasicSingleton : public Singleton_DCLP - { - public: - int getSeven() - { - TRACE; - return 7; - } - }; - -public: - - void testBasic( void ) - { - TEST_HEADER; - - TS_ASSERT_EQUALS( BasicSingleton::getInstance()->getSeven(), 7 ); - } - - -}; diff --git a/test/test_Singleton_DCLP.hpp b/test/test_Singleton_DCLP.hpp index d5089b3..8c1fb20 100644 --- a/test/test_Singleton_DCLP.hpp +++ b/test/test_Singleton_DCLP.hpp @@ -29,6 +29,7 @@ public: TEST_HEADER; TS_ASSERT_EQUALS( BasicSingleton::getInstance()->getSeven(), 7 ); + BasicSingleton::destroy(); } diff --git a/test/test_Singelton_call_once.hpp b/test/test_Singleton_call_once.hpp similarity index 94% rename from test/test_Singelton_call_once.hpp rename to test/test_Singleton_call_once.hpp index 873530a..9da22fa 100644 --- a/test/test_Singelton_call_once.hpp +++ b/test/test_Singleton_call_once.hpp @@ -29,6 +29,7 @@ public: TEST_HEADER; TS_ASSERT_EQUALS( BasicSingleton::getInstance()->getSeven(), 7 ); + BasicSingleton::destroy(); } diff --git a/test/test_Thread.hpp b/test/test_Thread.hpp index 7b2a00f..a5fc5fc 100644 --- a/test/test_Thread.hpp +++ b/test/test_Thread.hpp @@ -34,18 +34,14 @@ public: void testBasic( void ) { TEST_HEADER; - ThreadClass *m = new ThreadClass; - m->start(); + ThreadClass m; + m.start(); - void *retVal = m->join(); + void *retVal = m.join(); TS_ASSERT_EQUALS ( *((int*)retVal) , 14 ); free(retVal); - delete m; } -/** - * @note send a signal to a thread - */ private: @@ -62,7 +58,6 @@ private: TRACE; } - private: void* run( void ) { @@ -73,10 +68,9 @@ private: */ sleep(665); -// void* retVal = malloc(sizeof(int)); -// *((int*)retVal) = 15; -// return retVal; - return 0; + void* retVal = malloc(sizeof(int)); + *((int*)retVal) = 15; + return retVal; } static void signal_handler(int sig) @@ -97,18 +91,17 @@ public: void testSignalSend( void ) { TEST_HEADER; - ThreadClassWithSignal *m2 = new ThreadClassWithSignal; - m2->start(); + ThreadClassWithSignal m2 ; + m2.start(); sleep(1); - m2->sendSignal(SIGINT); + m2.sendSignal(SIGINT); - void *retVal = m2->join(); + void *retVal = m2.join(); TS_ASSERT(retVal); if (retVal != 0 ) { TS_ASSERT_EQUALS ( *((int*)retVal) , 16 ); free((int*)retVal); } - delete m2; } @@ -127,7 +120,7 @@ private: public: - void testEmpty( void ) + void eetestEmpty( void ) { TEST_HEADER; @@ -138,4 +131,17 @@ public: void *retVal = e.join(); TS_ASSERT_EQUALS ( retVal , (void *)0 ); } + + void testJoiningNotStartedThread( void ) + { + TEST_HEADER; + + EmptyThreadClass e; + + e.stop(); + e.join(); + void *retVal = e.join(); + TS_ASSERT_EQUALS ( retVal , (void *)0 ); + } + }; diff --git a/test/valgrind.supp b/test/valgrind.supp index 77628ba..edd82f0 100644 --- a/test/valgrind.supp +++ b/test/valgrind.supp @@ -69,7 +69,18 @@ Memcheck:Leak fun:calloc fun:_dl_allocate_tls - fun:pthread_create@@GLIBC_*.*.* + fun:pthread_create@@GLIBC_* + ... + fun:main +} + +{ + create thread2 + Memcheck:Leak + fun:calloc + fun:allocate_dtv + fun:_dl_allocate_tls + fun:pthread_create@@GLIBC_* ... fun:main }