Skip to content

Commit 1d0d08c

Browse files
committed
add test
1 parent 51a4fa0 commit 1d0d08c

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ cmake_minimum_required (VERSION 3.13...3.22)
33

44
project (benchmark VERSION 1.9.1 LANGUAGES CXX)
55

6+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
7+
68
option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON)
79
option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON)
810
option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF)

test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ benchmark_add_test(NAME spec_arg_verbosity COMMAND spec_arg_verbosity_test --v=4
8585
compile_benchmark_test(benchmark_setup_teardown_test)
8686
benchmark_add_test(NAME benchmark_setup_teardown COMMAND benchmark_setup_teardown_test)
8787

88+
compile_benchmark_test(benchmark_setup_teardown_cb_types_test)
89+
benchmark_add_test(NAME benchmark_setup_teardown_cb_types COMMAND benchmark_setup_teardown_cb_types_test)
90+
8891
compile_benchmark_test(filter_test)
8992
macro(add_filter_test name filter expect)
9093
benchmark_add_test(NAME ${name} COMMAND filter_test --benchmark_min_time=0.01s --benchmark_filter=${filter} ${expect})
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <iostream>
2+
#include <memory>
3+
4+
#include "benchmark/benchmark.h"
5+
6+
using benchmark::callback_function;
7+
8+
namespace counters {
9+
static int functor_calls = 0;
10+
static int lambda_calls = 0;
11+
static int ctr_copy_calls = 0;
12+
static int ctr_move_calls = 0;
13+
};
14+
15+
struct Functor {
16+
Functor() {}
17+
Functor(const Functor& /*unused*/){
18+
counters::ctr_copy_calls++;
19+
}
20+
Functor(Functor&& /*unused*/){
21+
counters::ctr_move_calls++;
22+
}
23+
//void operator()(const benchmark::State& /*unused*/) { counters::functor_calls++; }
24+
void operator()(const benchmark::State& /*unused*/) { }
25+
};
26+
27+
void BM_DoSomething(benchmark::State& state){
28+
for(auto _ : state) {
29+
}
30+
}
31+
32+
33+
int main(int argc, char** argv) {
34+
benchmark::Initialize(&argc, argv);
35+
auto bm = benchmark::RegisterBenchmark("BM_CtrCopy", BM_DoSomething);
36+
37+
{
38+
Functor func;
39+
bm->Setup(func);
40+
bm->Teardown(func);
41+
assert(counters::ctr_copy_calls == 2);
42+
}
43+
{
44+
Functor func;
45+
Functor& r_func{func};
46+
bm->Setup(r_func);
47+
bm->Teardown(r_func);
48+
assert(counters::ctr_copy_calls == 4);
49+
}
50+
{
51+
Functor func;
52+
const Functor& cr_func{func};
53+
bm->Setup(cr_func);
54+
bm->Teardown(cr_func);
55+
assert(counters::ctr_copy_calls == 6);
56+
}
57+
{
58+
Functor func1;
59+
Functor func2;
60+
bm->Setup(std::move(func1));
61+
bm->Teardown(std::move(func2));
62+
assert(counters::ctr_move_calls == 2);
63+
}
64+
{
65+
bm->Setup(Functor{});
66+
bm->Teardown(Functor{});
67+
assert(counters::ctr_move_calls == 4);
68+
}
69+
{
70+
bm->Setup([](const benchmark::State& /*unused*/){});
71+
bm->Teardown([](const benchmark::State& /*unused*/){});
72+
}
73+
{
74+
callback_function func1 = [](const benchmark::State& /*unused*/){};
75+
callback_function func2 = func1;
76+
bm->Setup(std::move(func1));
77+
bm->Teardown(std::move(func2));
78+
assert(func1 == nullptr);
79+
assert(func2 == nullptr);
80+
}
81+
{
82+
callback_function func1 = [](const benchmark::State& /*unused*/){};
83+
callback_function func2 = func1;
84+
bm->Setup(func1);
85+
bm->Teardown(func2);
86+
assert(func1 != nullptr);
87+
assert(func2 != nullptr);
88+
}
89+
{
90+
auto func = [](const benchmark::State& /*unused*/){};
91+
bm->Setup(func);
92+
bm->Teardown(func);
93+
assert(func != nullptr);
94+
}
95+
{
96+
auto func1 = [](const benchmark::State& /*unused*/){};
97+
auto func2 = func1;
98+
bm->Setup(std::move(func1));
99+
bm->Teardown(std::move(func2));
100+
}
101+
}

0 commit comments

Comments
 (0)