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)
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)

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

@ -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()

@ -4,8 +4,8 @@
#include <pthread.h>
pthread_mutex_t p_mutex;
#include <boost/thread/mutex.hpp>
boost::mutex b_mutex;
// #include <boost/thread/mutex.hpp>
// 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;
}

@ -1,92 +1,204 @@
#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 <pthread.h>
#include <stdexcept>
#include <time.h>
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");

@ -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

@ -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 "Singleton.hpp"
class MyTestSuite : public CxxTest::TestSuite
{

Loading…
Cancel
Save