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