diff --git a/.gitignore b/.gitignore index 331e00c..7f83c5f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ */lcov.info */lcov2.info */core +*.gcno *.kdev_include_paths test/generated_main.cpp test/test diff --git a/include/Common.hpp b/include/Common.hpp index 9cff7c9..7d7a7a6 100644 --- a/include/Common.hpp +++ b/include/Common.hpp @@ -1,3 +1,7 @@ +#ifndef COMMON_HPP +#define COMMON_HPP + + #include #ifndef NOTRACE @@ -8,3 +12,24 @@ /// @todo Get rid of the "warning: statement has no effect" compiler msgs #define TRACE(x) "" #endif + + +#define STRINGIZE(c) #c + + +#include + +inline timespec intIntervalTotimespec(const int & interval) +{ + timespec abs_time; + clock_gettime ( CLOCK_REALTIME, &abs_time ); + abs_time.tv_nsec += interval * 1000000; + if ( abs_time.tv_nsec >= 1000000000 ) { + abs_time.tv_nsec -= 1000000000; + abs_time.tv_sec += 1; + } + return abs_time; +}; + + +#endif // COMMON_HPP diff --git a/include/Task.hpp b/include/Task.hpp index 7f2e385..30fee79 100644 --- a/include/Task.hpp +++ b/include/Task.hpp @@ -8,6 +8,9 @@ class Task public: +// Task() {}; +// virtual ~Task() {}; + virtual void run () = 0; virtual bool isItStucked () const = 0; diff --git a/src/ConditionVariable.cpp b/src/ConditionVariable.cpp index c64ca85..d57d1d7 100644 --- a/src/ConditionVariable.cpp +++ b/src/ConditionVariable.cpp @@ -2,7 +2,7 @@ #include "Common.hpp" #include -#include + ConditionVariable::ConditionVariable(Mutex& mutex) : m_mutex(mutex) @@ -26,16 +26,10 @@ int ConditionVariable::wait(const int interval) return pthread_cond_wait( &m_condVar, m_mutex.getPThreadMutex() ); } else { - timespec abs_time; - clock_gettime ( CLOCK_REALTIME, &abs_time ); - abs_time.tv_nsec += interval * 1000000; - if ( abs_time.tv_nsec >= 1000000000 ) { - abs_time.tv_nsec -= 1000000000; - abs_time.tv_sec += 1; - } + timespec tspec = intIntervalTotimespec(interval); return pthread_cond_timedwait( &m_condVar, m_mutex.getPThreadMutex(), - &abs_time ); + &tspec); } } diff --git a/src/Mutex.cpp b/src/Mutex.cpp index 75b1b7a..cf693f9 100644 --- a/src/Mutex.cpp +++ b/src/Mutex.cpp @@ -46,15 +46,8 @@ bool Mutex::tryLock(const int interval) int result = pthread_mutex_trylock ( &m_mutex ); return result == 0; } else { - timespec abs_time; - clock_gettime ( CLOCK_REALTIME, &abs_time ); - abs_time.tv_nsec += interval * 1000000; - if ( abs_time.tv_nsec >= 1000000000 ) { - abs_time.tv_nsec -= 1000000000; - abs_time.tv_sec += 1; - } - int result = pthread_mutex_timedlock ( &m_mutex, &abs_time ); - return result == 0; + timespec tspec = intIntervalTotimespec(interval); + return pthread_mutex_timedlock ( &m_mutex, &tspec ) == 0; } } diff --git a/test/run_test.sh b/test/run_test.sh index 53f62d1..4908719 100755 --- a/test/run_test.sh +++ b/test/run_test.sh @@ -3,17 +3,68 @@ # Usage: # ./run_test.sh -pre="\E[00;32m" +pre="\E[00;33m" +fail="\E[00;31m" post="\E[00;00m" -echo -e "${pre}Reset coverage files${post}" +ulimit -c unlimited + +# test=$1 +test=./test + +# if [ $# -ne 1 ] +# then +# echo "Usage: `basename $0` " +# exit 1 +# fi + +if [ ! -e $test ]; then + echo -e "The parameter binary: $test does not exists" + exit 1 +fi + +echo -e "${pre}Reset & remove files${post}" +# coverage lcov --directory . -z rm -f ./lcov.info +# cores +rm -f ./leak.log.core.* +# cxxtest output +rm -f $test.out + echo -e "${pre}Run tests${post}" -valgrind --log-file=leak.log --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 $1 + +valgrind \ + --log-file=leak.log \ + --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 \ + $test | tee $test.out; retval=$PIPESTATUS; (( $retval == 0 )); + +# retval is 0 on success +# or the number of failed cases +# or 137 if segfault happens +if [ $retval -ne 0 ]; then + echo -e "${fail}The executed binary: $test failed.${post}" + + if [ $retval -ne 137 ]; then + echo -e "${pre}Failed checks:${post}" + cat $test.out | grep "Error:" | awk -F"/test/" '{ print $2 }' + fi + + cores=$(ls leak.log.core.* 2>/dev/null) + if [ "$cores" != "" ]; then + echo -e "${pre}Core file generated: ${post}" + echo $cores + fi + exit -1 +fi echo -e "${pre}Capture coverage info${post}" diff --git a/test/test_Thread.hpp b/test/test_Thread.hpp index d324a75..9841ff6 100644 --- a/test/test_Thread.hpp +++ b/test/test_Thread.hpp @@ -15,16 +15,16 @@ private: class ThreadClass : public Thread { - public: +// public: - ThreadClass() { TRACE(this); } - ~ThreadClass() { TRACE(this); } +// ThreadClass() { TRACE(this); } +// ~ThreadClass() { TRACE(this); } private: - ThreadClass(const ThreadClass&) { TRACE(this); } - ThreadClass& operator=(const ThreadClass&) { TRACE(this); return*this; } +// ThreadClass(const ThreadClass&) : Thread() { TRACE(this); } +// ThreadClass& operator=(const ThreadClass&) { TRACE(this); return*this; } void* run( void ) { TRACE(this); @@ -69,9 +69,6 @@ private: private: - ThreadClassWithSignal(const ThreadClassWithSignal&) { TRACE(this); } - ThreadClassWithSignal& operator=(const ThreadClassWithSignal&) { TRACE(this); return*this; } - void* run( void ) { TRACE(this); @@ -104,12 +101,15 @@ public: { ThreadClassWithSignal *m2 = new ThreadClassWithSignal; m2->start(); + sleep(3); m2->sendSignal(SIGINT); - sleep(3); void *retVal = m2->join(); - TS_ASSERT_EQUALS ( *((int*)retVal) , 16 ); - free(retVal); + TS_ASSERT(retVal); + if (retVal != 0 ) { + TS_ASSERT_EQUALS ( *((int*)retVal) , 16 ); + free(retVal); + } delete m2; }