CMakeLists.txt cleanup, added minimal unittest, added run_test coeverage tool, added .gitignore file

master
Denes Matetelki 14 years ago
parent 772fc95d56
commit b9f55724d8

10
.gitignore vendored

@ -0,0 +1,10 @@
*CMakeCache*
*CMakeFiles*
*Makefile*
*cmake
*.a
*.so
*/cov
*/lcov.info
*/core
*.kdev_include_paths

@ -1,17 +1,11 @@
cmake_minimum_required (VERSION 2.6) cmake_minimum_required (VERSION 2.6)
project (CPP_UTILS_LIB) project (CPP_UTILS_LIB)
message(STATUS "Lib dir:") set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow -ggdb -fprofile-arcs -ftest-coverage")
set (CXX_FLAGS "-Wall -Wextra -pedantic")
add_definitions( ${CXX_FLAGS} ) add_definitions( ${CXX_FLAGS} )
message(STATUS "g++ flags: ${CXX_FLAGS}")
include_directories (../include) include_directories (../include)
message(STATUS "include dir: ${CPP_UTILS_LIB}/include")
aux_source_directory(../src CPP_UTILS_LIB_SOURCES) aux_source_directory(../src CPP_UTILS_LIB_SOURCES)
message(STATUS "sources: ${CPP_UTILS_LIB_SOURCES}")
add_library (CppUtils ${CPP_UTILS_LIB_SOURCES}) add_library (CppUtils SHARED ${CPP_UTILS_LIB_SOURCES})
target_link_libraries(CppUtils pthread rt) target_link_libraries(CppUtils pthread rt gcov)

@ -21,10 +21,9 @@ public:
static void createInstance() static void createInstance()
{ {
TRACE("Simgleton::createInstance()"); TRACE("Simgleton::createInstance()");
if ( m_instance ) { if ( not m_instance ) {
delete m_instance; m_instance = new T();
} }
m_instance = new T();
} }

@ -1,15 +1,11 @@
add_definitions(-Wall -Werror -pedantic -Weffc++ -Wshadow -ggdb -fprofile-arcs -ftest-coverage) set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow -ggdb -fprofile-arcs -ftest-coverage")
add_definitions( ${CXX_FLAGS} )
include_directories(../include)
#add_library (CppUtils src/*)
find_package(CxxTest) find_package(CxxTest)
if(CXXTEST_FOUND) if(CXXTEST_FOUND)
set(CXXTEST_USE_PERL TRUE) set(CXXTEST_USE_PERL TRUE)
include_directories(${CXXTEST_INCLUDE_DIR} ../include) include_directories(${CXXTEST_INCLUDE_DIR} ../include)
enable_testing() 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) target_link_libraries(unittest_multiple CppUtils gcov)
endif() endif()

@ -4,8 +4,8 @@
#include <pthread.h> #include <pthread.h>
pthread_mutex_t p_mutex; pthread_mutex_t p_mutex;
#include <boost/thread/mutex.hpp> // #include <boost/thread/mutex.hpp>
boost::mutex b_mutex; // boost::mutex b_mutex;
#define TRACE std::cout << __FILE__ << " @ " << __PRETTY_FUNCTION__ << ":" << __LINE__ << std::endl; #define TRACE std::cout << __FILE__ << " @ " << __PRETTY_FUNCTION__ << ":" << __LINE__ << std::endl;
@ -15,7 +15,7 @@ class ScopedLock
public: public:
ScopedLock(pthread_mutex_t const& mutex) : m_mutex(mutex) ScopedLock(pthread_mutex_t & mutex) : m_mutex(mutex)
{ {
TRACE; TRACE;
pthread_mutex_lock( &m_mutex ); pthread_mutex_lock( &m_mutex );
@ -30,7 +30,7 @@ public:
private: private:
pthread_mutex_t m_mutex; pthread_mutex_t& m_mutex;
}; };
@ -43,12 +43,12 @@ void fv ()
} }
void fv2 () // void fv2 ()
{ // {
boost::mutex::scoped_lock lock(b_mutex); // boost::mutex::scoped_lock lock(&b_mutex);
//
throw std::logic_error("boost_thread stuff"); // throw std::logic_error("boost_thread stuff");
} // }
int main() int main()
@ -60,26 +60,26 @@ int main()
TRACE; TRACE;
if (pthread_mutex_trylock( &p_mutex) == 0 ) { if (pthread_mutex_trylock( &p_mutex) == 0 ) {
std::cout << "pthread mutex is OK, unlocked " << std::endl; // std::cout << "pthread mutex is OK, unlocked " << std::endl;
pthread_mutex_unlock( &p_mutex ); // pthread_mutex_unlock( &p_mutex );
} else { } else {
std::cout << "pthread mutex is STILL LOCKED!" << std::endl; // std::cout << "pthread mutex is STILL LOCKED!" << std::endl;
} }
} }
pthread_mutex_destroy( &p_mutex ); pthread_mutex_destroy( &p_mutex );
try { // try {
fv2(); // fv2();
} catch (...) { // } catch (...) {
TRACE; // TRACE;
if ( b_mutex.try_lock() == true ) { // if ( b_mutex.try_lock() == true ) {
std::cout << "boost mutex is OK, unlocked " << std::endl; // std::cout << "boost mutex is OK, unlocked " << std::endl;
b_mutex.unlock(); // b_mutex.unlock();
} else { // } else {
std::cout << "boost mutex is STILL LOCKED!" << std::endl; // std::cout << "boost mutex is STILL LOCKED!" << std::endl;
} // }
} // }
return 0; return 0;
} }

@ -1,93 +1,205 @@
#include <iostream>
#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 <assert.h> #include <assert.h>
#include <pthread.h> #include <pthread.h>
#include <stdexcept> #include <stdexcept>
#include <time.h>
class Mutex class Mutex
{ {
public: public:
Mutex() : m_mutex(PTHREAD_MUTEX_INITIALIZER) { Mutex() {
TRACE(this); TRACE(this);
int ret = pthread_mutex_init( &m_mutex, 0 ); int ret = pthread_mutex_init( &m_mutex, 0 );
assert( ret == 0 ); // assert( ret == 0 );
} }
~Mutex() { ~Mutex() {
TRACE(this); TRACE(this);
int ret = pthread_mutex_destroy ( &m_mutex ); int ret = pthread_mutex_destroy ( &m_mutex );
assert( ret == 0 ); // assert( ret == 0 );
} }
void lock() { void lock() {
TRACE(this); TRACE(this);
int ret = pthread_mutex_lock( &m_mutex ); int ret = pthread_mutex_lock( &m_mutex );
assert( ret == 0 ); // assert( ret == 0 );
} }
void unlock() { void unlock() {
TRACE(this); TRACE(this);
int ret = pthread_mutex_unlock( &m_mutex ); 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: private:
pthread_mutex_t m_mutex; pthread_mutex_t m_mutex;
}; };
class ScopedLock class BadScopedLock
{ {
public: public:
ScopedLock(Mutex* m) : m_mutex(m) { BadScopedLock(Mutex& m)
: m_mutex(m) /// @note cctor called!
{
TRACE(this); TRACE(this);
m_mutex->lock(); m_mutex.lock();
} }
~ScopedLock() { ~BadScopedLock() {
TRACE(this); TRACE(this);
m_mutex->unlock(); m_mutex.unlock();
} }
private: private:
ScopedLock(const ScopedLock&); BadScopedLock(const BadScopedLock&);
ScopedLock& operator=(const ScopedLock&); BadScopedLock& operator=(const BadScopedLock&);
Mutex* m_mutex; Mutex& m_mutex;
}; };
class User class UnluckyUser
{ {
public: public:
User() : m_mutex() { UnluckyUser() : m_mutex() {
TRACE(this); TRACE(this);
} }
~User() { ~UnluckyUser() {
TRACE(this); TRACE(this);
} }
void fv() { void fv() {
TRACE(this); TRACE(this);
ScopedLock sl(&m_mutex); BadScopedLock sl(m_mutex);
throw std::logic_error("whoops"); throw std::logic_error("whoops");
} }
int tryLock() {
TRACE(this);
return m_mutex.tryLock();
}
private: private:
Mutex m_mutex; 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 { try {
u.fv(); u.fv();
} catch (std::logic_error ex) { } catch (std::logic_error ex) {
std::cout << "std::logicexception: " << ex.what() << std::endl; 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"); TRACE("main end");
return 0; return 0;

@ -13,12 +13,12 @@ echo -e "${pre}Run tests${post}"
$1 $1
echo -e "${pre}Capture coverage info${post}" 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}" echo -e "${pre}Filtering coverage tracefile${post}"
lcov -r lcov.info "g++-v*" -o lcov.info lcov -r lcov.info "g++-v*" -o lcov.info
lcov -r lcov.info "/usr/include/cxxtest*" -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}" echo -e "${pre}Generating HTML${post}"
rm -rf ./cov rm -rf ./cov

@ -0,0 +1,13 @@
#include <cxxtest/TestSuite.h>
#include "Singleton.hpp"
class MyTestSuite : public CxxTest::TestSuite
{
public:
void testAddition( void )
{
TS_ASSERT( 1 + 1 > 1 );
TS_ASSERT_EQUALS( 1 + 1, 2 );
}
};

@ -0,0 +1,36 @@
#include <cxxtest/TestSuite.h>
/*
#include <signal.h>
#include <stdlib.h>
#include <string.h>*/
#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");
}
};

@ -1,6 +1,6 @@
#include <cxxtest/TestSuite.h> #include <cxxtest/TestSuite.h>
#include "Singleton.hpp"
class MyTestSuite : public CxxTest::TestSuite class MyTestSuite : public CxxTest::TestSuite
{ {

Loading…
Cancel
Save