diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..19b358c --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +*CMakeCache* +*CMakeFiles* +*Makefile* +*cmake +*.a +*.so +*/cov +*/lcov.info +*/core +*.kdev_include_paths diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index 19f422b..468109e 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -1,17 +1,11 @@ cmake_minimum_required (VERSION 2.6) project (CPP_UTILS_LIB) -message(STATUS "Lib dir:") - -set (CXX_FLAGS "-Wall -Wextra -pedantic") +set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow -ggdb -fprofile-arcs -ftest-coverage") add_definitions( ${CXX_FLAGS} ) -message(STATUS "g++ flags: ${CXX_FLAGS}") include_directories (../include) -message(STATUS "include dir: ${CPP_UTILS_LIB}/include") - aux_source_directory(../src CPP_UTILS_LIB_SOURCES) -message(STATUS "sources: ${CPP_UTILS_LIB_SOURCES}") -add_library (CppUtils ${CPP_UTILS_LIB_SOURCES}) -target_link_libraries(CppUtils pthread rt) +add_library (CppUtils SHARED ${CPP_UTILS_LIB_SOURCES}) +target_link_libraries(CppUtils pthread rt gcov) diff --git a/include/Singleton.hpp b/include/Singleton.hpp index 643785a..5d243ef 100644 --- a/include/Singleton.hpp +++ b/include/Singleton.hpp @@ -21,10 +21,9 @@ public: static void createInstance() { TRACE("Simgleton::createInstance()"); - if ( m_instance ) { - delete m_instance; + if ( not m_instance ) { + m_instance = new T(); } - m_instance = new T(); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index cebf214..b35b630 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,15 +1,11 @@ -add_definitions(-Wall -Werror -pedantic -Weffc++ -Wshadow -ggdb -fprofile-arcs -ftest-coverage) - -include_directories(../include) -#add_library (CppUtils src/*) - +set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow -ggdb -fprofile-arcs -ftest-coverage") +add_definitions( ${CXX_FLAGS} ) find_package(CxxTest) if(CXXTEST_FOUND) set(CXXTEST_USE_PERL TRUE) include_directories(${CXXTEST_INCLUDE_DIR} ../include) enable_testing() -CXXTEST_ADD_TEST(unittest_multiple generated_main.cpp unittest_multiple.hpp ) +CXXTEST_ADD_TEST(unittest_multiple generated_main.cpp test_threadpool.hpp ) target_link_libraries(unittest_multiple CppUtils gcov) endif() - diff --git a/test/main_scopedLock.cpp b/test/main_scopedLock.cpp index b953cd5..5dde860 100644 --- a/test/main_scopedLock.cpp +++ b/test/main_scopedLock.cpp @@ -4,8 +4,8 @@ #include pthread_mutex_t p_mutex; -#include -boost::mutex b_mutex; +// #include +// boost::mutex b_mutex; #define TRACE std::cout << __FILE__ << " @ " << __PRETTY_FUNCTION__ << ":" << __LINE__ << std::endl; @@ -15,7 +15,7 @@ class ScopedLock public: - ScopedLock(pthread_mutex_t const& mutex) : m_mutex(mutex) + ScopedLock(pthread_mutex_t & mutex) : m_mutex(mutex) { TRACE; pthread_mutex_lock( &m_mutex ); @@ -30,7 +30,7 @@ public: private: - pthread_mutex_t m_mutex; + pthread_mutex_t& m_mutex; }; @@ -43,12 +43,12 @@ void fv () } -void fv2 () -{ - boost::mutex::scoped_lock lock(b_mutex); - - throw std::logic_error("boost_thread stuff"); -} +// void fv2 () +// { +// boost::mutex::scoped_lock lock(&b_mutex); +// +// throw std::logic_error("boost_thread stuff"); +// } int main() @@ -60,26 +60,26 @@ int main() TRACE; if (pthread_mutex_trylock( &p_mutex) == 0 ) { - std::cout << "pthread mutex is OK, unlocked " << std::endl; - pthread_mutex_unlock( &p_mutex ); +// std::cout << "pthread mutex is OK, unlocked " << std::endl; +// pthread_mutex_unlock( &p_mutex ); } else { - std::cout << "pthread mutex is STILL LOCKED!" << std::endl; +// std::cout << "pthread mutex is STILL LOCKED!" << std::endl; } } pthread_mutex_destroy( &p_mutex ); - try { - fv2(); - } catch (...) { - TRACE; - if ( b_mutex.try_lock() == true ) { - std::cout << "boost mutex is OK, unlocked " << std::endl; - b_mutex.unlock(); - } else { - std::cout << "boost mutex is STILL LOCKED!" << std::endl; - } - } +// try { +// fv2(); +// } catch (...) { +// TRACE; +// if ( b_mutex.try_lock() == true ) { +// std::cout << "boost mutex is OK, unlocked " << std::endl; +// b_mutex.unlock(); +// } else { +// std::cout << "boost mutex is STILL LOCKED!" << std::endl; +// } +// } return 0; } diff --git a/test/main_standalone_Mutex_ScopedLock.cpp b/test/main_standalone_Mutex_ScopedLock.cpp index c8e5956..1f17e0c 100644 --- a/test/main_standalone_Mutex_ScopedLock.cpp +++ b/test/main_standalone_Mutex_ScopedLock.cpp @@ -1,93 +1,205 @@ +#include + +#ifndef NOTRACE + #define TRACE(x) std::cout << x << " " << __PRETTY_FUNCTION__ << \ + " : " << __LINE__ << std::endl + +#else + /// @todo Get rid of the "warning: statement has no effect" compiler msgs + #define TRACE(x) "" +#endif #include #include #include +#include class Mutex { public: - Mutex() : m_mutex(PTHREAD_MUTEX_INITIALIZER) { + Mutex() { TRACE(this); int ret = pthread_mutex_init( &m_mutex, 0 ); - assert( ret == 0 ); +// assert( ret == 0 ); } ~Mutex() { TRACE(this); int ret = pthread_mutex_destroy ( &m_mutex ); - assert( ret == 0 ); +// assert( ret == 0 ); } void lock() { TRACE(this); int ret = pthread_mutex_lock( &m_mutex ); - assert( ret == 0 ); +// assert( ret == 0 ); } void unlock() { TRACE(this); int ret = pthread_mutex_unlock( &m_mutex ); - assert( ret == 0 ); +// assert( ret == 0 ); + } +// private: + Mutex(const Mutex& m){ + TRACE(this); + } + Mutex& operator=(const Mutex& m) { + TRACE(this); + return *this; + } + + int tryLock() { + TRACE(this); +// return pthread_mutex_trylock(&m_mutex); + int interval = 1; + + 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 pthread_mutex_timedlock ( &m_mutex, &abs_time ); } -private: - Mutex(const Mutex& m); - Mutex& operator=(const Mutex& m); private: pthread_mutex_t m_mutex; }; -class ScopedLock +class BadScopedLock { public: - ScopedLock(Mutex* m) : m_mutex(m) { + BadScopedLock(Mutex& m) + : m_mutex(m) /// @note cctor called! + { TRACE(this); - m_mutex->lock(); + m_mutex.lock(); } - ~ScopedLock() { + ~BadScopedLock() { TRACE(this); - m_mutex->unlock(); + m_mutex.unlock(); } private: - ScopedLock(const ScopedLock&); - ScopedLock& operator=(const ScopedLock&); + BadScopedLock(const BadScopedLock&); + BadScopedLock& operator=(const BadScopedLock&); - Mutex* m_mutex; + Mutex& m_mutex; }; -class User +class UnluckyUser { public: - User() : m_mutex() { + UnluckyUser() : m_mutex() { TRACE(this); } - ~User() { + ~UnluckyUser() { TRACE(this); } void fv() { TRACE(this); - ScopedLock sl(&m_mutex); + BadScopedLock sl(m_mutex); throw std::logic_error("whoops"); } + int tryLock() { + TRACE(this); + return m_mutex.tryLock(); + } private: Mutex m_mutex; }; -int main() +// class GoodScopedLock +// { +// public: +// +// GoodScopedLock(Mutex* m) : m_mutex(m) +// { +// TRACE(this); +// m_mutex->lock(); +// } +// ~GoodScopedLock() { +// TRACE(this); +// m_mutex->unlock(); +// } +// +// private: +// GoodScopedLock(const GoodScopedLock&); +// GoodScopedLock& operator=(const GoodScopedLock&); +// +// Mutex* m_mutex; +// }; +/* +class LuckyUser { - TRACE("main begin"); +public: + LuckyUser() : m_mutex() { + TRACE(this); + } + ~LuckyUser() { + TRACE(this); + } + void fv() { + TRACE(this); + GoodScopedLock sl(&m_mutex); + throw std::logic_error("whoops"); + } + int tryLock() { + TRACE(this); + return m_mutex.tryLock(); + } +private: + Mutex m_mutex; +};*/ - User u; +int main() +{ + TRACE("main begin"); +// pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +// // pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; +// std::cout << "bla1" << std::endl; +// std::cout << pthread_mutex_unlock( &mutex )<< std::endl; +// std::cout << pthread_mutex_trylock( &mutex ) << std::endl; +// std::cout << "bla2" << std::endl; +// // std::cout << pthread_mutex_unlock( &mutex ) << std::endl; +// +// std::cout << "bla3" << std::endl; +// +// std::cout << pthread_mutex_lock( &mutex ) << std::endl; +// std::cout << pthread_mutex_trylock( &mutex ) << std::endl; +// // std::cout << pthread_mutex_lock( &mutex ) << std::endl; + + UnluckyUser u; try { u.fv(); } catch (std::logic_error ex) { std::cout << "std::logicexception: " << ex.what() << std::endl; + if (u.tryLock() == 0) { + std::cout << "UnluckyUser: Ok, mutex is unlocked" << std::endl; + } else { + std::cout << "UnluckyUser: Failed, mutex is still locked" << std::endl; + } } +// TRACE("main middle"); +// +// LuckyUser u2; +// try { +// u2.fv(); +// } catch (std::logic_error ex) { +// std::cout << "std::logicexception: " << ex.what() << std::endl; +// if (u2.tryLock() == 0) { +// std::cout << "LuckyUser: Ok, mutex is unlocked" << std::endl; +// } else { +// std::cout << "LuckyUser: Failed, mutex is still locked" << std::endl; +// } +// } + TRACE("main end"); return 0; diff --git a/test/run_test.sh b/test/run_test.sh index 5322faf..2a0b9ad 100755 --- a/test/run_test.sh +++ b/test/run_test.sh @@ -13,12 +13,12 @@ echo -e "${pre}Run tests${post}" $1 echo -e "${pre}Capture coverage info${post}" -lcov --directory . --capture -o lcov.info +lcov --directory ../build --capture -o 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 "$(PWD)/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 new file mode 100644 index 0000000..b578799 --- /dev/null +++ b/test/test_Singelton.hpp @@ -0,0 +1,13 @@ +#include + +#include "Singleton.hpp" + +class MyTestSuite : public CxxTest::TestSuite +{ +public: + void testAddition( void ) + { + TS_ASSERT( 1 + 1 > 1 ); + TS_ASSERT_EQUALS( 1 + 1, 2 ); + } +}; diff --git a/test/test_threadpool.hpp b/test/test_threadpool.hpp new file mode 100644 index 0000000..2d13354 --- /dev/null +++ b/test/test_threadpool.hpp @@ -0,0 +1,36 @@ +#include +/* + +#include +#include +#include */ + +#include "ThreadPool.hpp" +#include "Task.hpp" +#include "Common.hpp" + + +class MyTestSuite : public CxxTest::TestSuite +{ + +public: + + void testACica() + { + TRACE("Main start"); + + ThreadPool* tp = new ThreadPool(5); + tp->startWorkerThreads(); + + Task* t1 = new Task(); + tp->pushTask(t1); + + sleep(2); + + tp->stop(); + tp->join(); + delete tp; + + TRACE("Main end"); + } +}; diff --git a/test/unittest_multiple.hpp b/test/unittest_multiple.hpp index fa42b9b..5d0ff58 100644 --- a/test/unittest_multiple.hpp +++ b/test/unittest_multiple.hpp @@ -1,6 +1,6 @@ #include - +#include "Singleton.hpp" class MyTestSuite : public CxxTest::TestSuite {