Skip to content

Commit bb3f3db

Browse files
committed
move to gtest && coverage
1 parent a94cf07 commit bb3f3db

File tree

9 files changed

+172
-178
lines changed

9 files changed

+172
-178
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "googletest"]
2+
path = googletest
3+
url = https://github.com/google/googletest.git

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ script:
2323
- cd build
2424
- cmake -DCMAKE_BUILD_TYPE=Release ..
2525
- make VERBOSE=1
26+
- ctest --verbose
2627
- cd ..
2728

2829
after_success:
2930
- mkdir coverage
3031
- cd coverage
31-
- cmake DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=yes ..
32-
- make VERBOSE=1
32+
- cmake -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=yes ..
33+
- make
34+
- ctest
3335
- lcov --gcov-tool gcov-5 --directory . --capture --output-file coverage.info
3436
- lcov --gcov-tool gcov-5 --remove coverage.info '/usr/*' --output-file coverage.info
37+
- lcov --gcov-tool gcov-5 --remove coverage.info 'googletest' --output-file coverage.info
38+
- lcov --gcov-tool gcov-5 --remove coverage.info '*.t.cpp' --output-file coverage.info
3539
- lcov --gcov-tool gcov-5 --list coverage.info
3640
- bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"
3741
- cd ..

CMakeLists.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@ cmake_minimum_required(VERSION 2.8)
22

33
project(thread-pool-cpp CXX)
44

5-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall")
5+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra")
66
if(COVERAGE)
7-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g --coverage")
7+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g --coverage")
8+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
89
endif()
910

10-
# Get all include files
11+
# gtest
12+
enable_testing()
13+
add_subdirectory(googletest)
14+
1115
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
1216

17+
# Tests
1318
add_subdirectory(tests)
19+
20+
# Benchmark
1421
add_subdirectory(benchmark)
1522

16-
# Install as header-only library
23+
# Install
1724
file(GLOB_RECURSE INSTALL_FILES_LIST "${CMAKE_CURRENT_SOURCE_DIR}/include/*")
1825
set_source_files_properties(${INSTALL_FILES_LIST} PROPERTIES HEADER_FILE_ONLY 1)
1926
add_library(HEADER_ONLY_TARGET STATIC ${INSTALL_FILES_LIST})

googletest

Submodule googletest added at 59c795c

tests/CMakeLists.txt

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,11 @@
22

33
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
44

5-
add_executable(fixed_function_test fixed_function.t.cpp)
6-
7-
target_link_libraries(fixed_function_test)
8-
add_custom_command(
9-
TARGET fixed_function_test
10-
POST_BUILD
11-
COMMAND ./fixed_function_test
12-
)
13-
14-
add_executable(thread_pool_test thread_pool.t.cpp thread_pool2.t.cpp)
15-
target_link_libraries(thread_pool_test pthread)
16-
add_custom_command(
17-
TARGET thread_pool_test
18-
POST_BUILD
19-
COMMAND ./thread_pool_test
20-
)
5+
function(build_test test_name)
6+
add_executable(${test_name}_test ${ARGN})
7+
target_link_libraries(${test_name}_test pthread gtest gtest_main)
8+
add_test(${test_name} ./${test_name}_test)
9+
endfunction()
10+
11+
build_test(fixed_function fixed_function.t.cpp)
12+
build_test(thread_pool thread_pool.t.cpp)

tests/fixed_function.t.cpp

Lines changed: 117 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1+
#include <gtest/gtest.h>
2+
13
#include <thread_pool/fixed_function.hpp>
2-
#include <test.hpp>
34

45
#include <string>
5-
#include <cassert>
66
#include <type_traits>
77
#include <functional>
88

9-
using namespace tp;
10-
119
int test_free_func(int i)
1210
{
1311
return i;
@@ -25,10 +23,12 @@ void test_void(int &p, int v)
2523
}
2624

2725
struct A {
28-
int b(const int &p) {
26+
int b(const int &p)
27+
{
2928
return p;
3029
}
31-
void c(int &i) {
30+
void c(int &i)
31+
{
3232
i = 43;
3333
}
3434
};
@@ -37,140 +37,153 @@ template <typename T>
3737
struct Foo
3838
{
3939
template <typename U>
40-
U bar(U p) {
40+
U bar(U p)
41+
{
4142
return p + payload;
4243
}
4344

4445
T payload;
4546
};
4647

4748
template <typename T>
48-
void print_overhead() {
49-
using func_type = FixedFunction<void(), sizeof(T)>;
49+
void print_overhead()
50+
{
51+
using func_type = tp::FixedFunction<void(), sizeof(T)>;
5052
int t_s = sizeof(T);
5153
int f_s = sizeof(func_type);
5254
std::cout << " - for type size " << t_s << "\n"
5355
<< " function size is " << f_s << "\n"
5456
<< " overhead is " << float(f_s - t_s)/t_s * 100 << "%\n";
5557
}
5658

57-
static std::string str_fun() {
59+
static std::string str_fun()
60+
{
5861
return "123";
5962
}
6063

61-
int main()
64+
TEST(FixedFunction, Overhead)
6265
{
63-
std::cout << "*** Testing FixedFunction ***" << std::endl;
64-
6566
print_overhead<char[8]>();
6667
print_overhead<char[16]>();
6768
print_overhead<char[32]>();
6869
print_overhead<char[64]>();
6970
print_overhead<char[128]>();
71+
}
7072

71-
doTest("alloc/dealloc", []() {
72-
static size_t def = 0;
73-
static size_t cop = 0;
74-
static size_t mov = 0;
75-
static size_t cop_ass = 0;
76-
static size_t mov_ass = 0;
77-
static size_t destroyed = 0;
78-
struct cnt {
79-
std::string payload;
80-
cnt() { def++; }
81-
cnt(const cnt &o) { payload = o.payload; cop++;}
82-
cnt(cnt &&o) { payload = std::move(o.payload); mov++;}
83-
cnt & operator=(const cnt &o) { payload = o.payload; cop_ass++; return *this; }
84-
cnt & operator=(cnt &&o) { payload = std::move(o.payload); mov_ass++; return *this; }
85-
~cnt() { destroyed++; }
86-
std::string operator()() { return payload; }
87-
};
88-
89-
{
90-
cnt c1;
91-
c1.payload = "xyz";
92-
FixedFunction<std::string()> f1(c1);
93-
ASSERT(std::string("xyz") == f1());
94-
95-
FixedFunction<std::string()> f2;
96-
f2 = std::move(f1);
97-
ASSERT(std::string("xyz") == f2());
98-
99-
FixedFunction<std::string()> f3(std::move(f2));
100-
ASSERT(std::string("xyz") == f3());
101-
102-
FixedFunction<std::string()> f4(str_fun);
103-
ASSERT(std::string("123") == f4());
104-
105-
f4 = std::move(f3);
106-
ASSERT(std::string("xyz") == f4());
107-
108-
cnt c2;
109-
c2.payload = "qwe";
110-
f4 = std::move(FixedFunction<std::string()>(c2));
111-
ASSERT(std::string("qwe") == f4());
112-
}
113-
114-
ASSERT(def + cop + mov == destroyed);
115-
ASSERT(2 == def);
116-
ASSERT(0 == cop);
117-
ASSERT(6 == mov);
118-
ASSERT(0 == cop_ass);
119-
ASSERT(0 == mov_ass);
120-
});
73+
TEST(FixedFunction, allocDealloc)
74+
{
75+
static size_t def = 0;
76+
static size_t cop = 0;
77+
static size_t mov = 0;
78+
static size_t cop_ass = 0;
79+
static size_t mov_ass = 0;
80+
static size_t destroyed = 0;
81+
struct cnt {
82+
std::string payload;
83+
cnt() { def++; }
84+
cnt(const cnt &o) { payload = o.payload; cop++;}
85+
cnt(cnt &&o) { payload = std::move(o.payload); mov++;}
86+
cnt & operator=(const cnt &o) { payload = o.payload; cop_ass++; return *this; }
87+
cnt & operator=(cnt &&o) { payload = std::move(o.payload); mov_ass++; return *this; }
88+
~cnt() { destroyed++; }
89+
std::string operator()() { return payload; }
90+
};
91+
92+
{
93+
cnt c1;
94+
c1.payload = "xyz";
95+
tp::FixedFunction<std::string()> f1(c1);
96+
ASSERT_EQ(std::string("xyz"), f1());
97+
98+
tp::FixedFunction<std::string()> f2;
99+
f2 = std::move(f1);
100+
ASSERT_EQ(std::string("xyz"), f2());
101+
102+
tp::FixedFunction<std::string()> f3(std::move(f2));
103+
ASSERT_EQ(std::string("xyz"), f3());
104+
105+
tp::FixedFunction<std::string()> f4(str_fun);
106+
ASSERT_EQ(std::string("123"), f4());
107+
108+
f4 = std::move(f3);
109+
ASSERT_EQ(std::string("xyz"), f4());
110+
111+
cnt c2;
112+
c2.payload = "qwe";
113+
f4 = std::move(tp::FixedFunction<std::string()>(c2));
114+
ASSERT_EQ(std::string("qwe"), f4());
115+
}
121116

122-
doTest("free func", []() {
123-
FixedFunction<int(int)> f(test_free_func);
124-
ASSERT(3 == f(3));
125-
});
117+
ASSERT_EQ(def + cop + mov, destroyed);
118+
ASSERT_EQ(2, def);
119+
ASSERT_EQ(0, cop);
120+
ASSERT_EQ(6, mov);
121+
ASSERT_EQ(0, cop_ass);
122+
ASSERT_EQ(0, mov_ass);
123+
}
126124

127-
doTest("free func template", []() {
128-
FixedFunction<std::string(std::string)> f(test_free_func_template<std::string>);
129-
ASSERT(std::string("abc") == f("abc"));
130-
});
125+
TEST(FixedFunction, freeFunc)
126+
{
127+
tp::FixedFunction<int(int)> f(test_free_func);
128+
ASSERT_EQ(3, f(3));
129+
};
131130

131+
TEST(FixedFunction, freeFuncTemplate)
132+
{
133+
tp::FixedFunction<std::string(std::string)> f(test_free_func_template<std::string>);
134+
ASSERT_EQ(std::string("abc"), f("abc"));
135+
}
132136

133-
doTest("void func", []() {
134-
FixedFunction<void(int &, int)> f(test_void);
135-
int p = 0;
136-
f(p, 42);
137-
ASSERT(42 == p);
138-
});
139137

140-
doTest("class method void", []() {
141-
using namespace std::placeholders;
142-
A a;
143-
int i = 0;
144-
FixedFunction<void(int &)> f(std::bind(&A::c, &a, _1));
145-
f(i);
146-
ASSERT(43 == i);
147-
});
138+
TEST(FixedFunction, voidFunc)
139+
{
140+
tp::FixedFunction<void(int &, int)> f(test_void);
141+
int p = 0;
142+
f(p, 42);
143+
ASSERT_EQ(42, p);
144+
}
148145

149-
doTest("class method 1", []() {
150-
using namespace std::placeholders;
151-
A a;
152-
FixedFunction<int(const int&)> f(std::bind(&A::b, &a, _1));
153-
ASSERT(4 == f(4));
154-
});
146+
TEST(FixedFunction, classMethodVoid)
147+
{
148+
using namespace std::placeholders;
149+
A a;
150+
int i = 0;
151+
tp::FixedFunction<void(int &)> f(std::bind(&A::c, &a, _1));
152+
f(i);
153+
ASSERT_EQ(43, i);
154+
}
155155

156-
doTest("class method 2", []() {
157-
using namespace std::placeholders;
158-
Foo<float> foo;
159-
foo.payload = 1.f;
160-
FixedFunction<int(int)> f(std::bind(&Foo<float>::bar<int>, &foo, _1));
161-
ASSERT(2 == f(1));
162-
});
156+
TEST(FixedFunction, classMethod1)
157+
{
158+
using namespace std::placeholders;
159+
A a;
160+
tp::FixedFunction<int(const int&)> f(std::bind(&A::b, &a, _1));
161+
ASSERT_EQ(4, f(4));
162+
}
163163

164-
doTest("lambda", []() {
165-
const std::string s1 = "s1";
166-
FixedFunction<std::string()> f([&s1]() {
167-
return s1;
168-
});
164+
TEST(FixedFunction, classMethod2)
165+
{
166+
using namespace std::placeholders;
167+
Foo<float> foo;
168+
foo.payload = 1.f;
169+
tp::FixedFunction<int(int)> f(std::bind(&Foo<float>::bar<int>, &foo, _1));
170+
ASSERT_EQ(2, f(1));
171+
}
169172

170-
ASSERT(s1 == f());
173+
TEST(FixedFunction, lambda)
174+
{
175+
const std::string s1 = "s1";
176+
tp::FixedFunction<std::string()> f([&s1]()
177+
{
178+
return s1;
171179
});
172180

181+
ASSERT_EQ(s1, f());
173182
}
174183

184+
int main(int argc, char **argv) {
185+
::testing::InitGoogleTest(&argc, argv);
186+
return RUN_ALL_TESTS();
187+
}
175188

176189

tests/test.hpp

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)