|
| 1 | +#include "benchmark/benchmark.h" |
| 2 | +#include "gtest/gtest.h" |
| 3 | + |
| 4 | +using benchmark::BenchmarkReporter; |
| 5 | +using benchmark::callback_function; |
| 6 | +using benchmark::ClearRegisteredBenchmarks; |
| 7 | +using benchmark::RegisterBenchmark; |
| 8 | +using benchmark::RunSpecifiedBenchmarks; |
| 9 | +using benchmark::State; |
| 10 | +using benchmark::internal::Benchmark; |
| 11 | + |
| 12 | +static int functor_called = 0; |
| 13 | +struct Functor { |
| 14 | + void operator()(const benchmark::State& /*unused*/) { functor_called++; } |
| 15 | +}; |
| 16 | + |
| 17 | +class NullReporter : public BenchmarkReporter { |
| 18 | + public: |
| 19 | + bool ReportContext(const Context& /*context*/) override { return true; } |
| 20 | + void ReportRuns(const std::vector<Run>& /* report */) override {} |
| 21 | +}; |
| 22 | + |
| 23 | +class BenchmarkTest : public testing::Test { |
| 24 | + public: |
| 25 | + Benchmark* bm; |
| 26 | + NullReporter null_reporter; |
| 27 | + |
| 28 | + int setup_calls; |
| 29 | + int teardown_calls; |
| 30 | + |
| 31 | + void SetUp() override { |
| 32 | + setup_calls = 0; |
| 33 | + teardown_calls = 0; |
| 34 | + functor_called = 0; |
| 35 | + |
| 36 | + bm = RegisterBenchmark("BM", [](State& st) { |
| 37 | + for (auto _ : st) { |
| 38 | + } |
| 39 | + }); |
| 40 | + bm->Iterations(1); |
| 41 | + } |
| 42 | + |
| 43 | + void TearDown() override { ClearRegisteredBenchmarks(); } |
| 44 | +}; |
| 45 | + |
| 46 | +// Test that Setup/Teardown can correctly take a lambda expressions |
| 47 | +TEST_F(BenchmarkTest, LambdaTestCopy) { |
| 48 | + auto setup_lambda = [this](const State&) { setup_calls++; }; |
| 49 | + auto teardown_lambda = [this](const State&) { teardown_calls++; }; |
| 50 | + bm->Setup(setup_lambda); |
| 51 | + bm->Teardown(teardown_lambda); |
| 52 | + RunSpecifiedBenchmarks(&null_reporter); |
| 53 | + EXPECT_EQ(setup_calls, 1); |
| 54 | + EXPECT_EQ(teardown_calls, 1); |
| 55 | +} |
| 56 | + |
| 57 | +// Test that Setup/Teardown can correctly take a lambda expressions |
| 58 | +TEST_F(BenchmarkTest, LambdaTestMove) { |
| 59 | + auto setup_lambda = [this](const State&) { setup_calls++; }; |
| 60 | + auto teardown_lambda = [this](const State&) { teardown_calls++; }; |
| 61 | + bm->Setup(std::move(setup_lambda)); |
| 62 | + bm->Teardown(std::move(teardown_lambda)); |
| 63 | + RunSpecifiedBenchmarks(&null_reporter); |
| 64 | + EXPECT_EQ(setup_calls, 1); |
| 65 | + EXPECT_EQ(teardown_calls, 1); |
| 66 | +} |
| 67 | + |
| 68 | +// Test that Setup/Teardown can correctly take std::function |
| 69 | +TEST_F(BenchmarkTest, CallbackFunctionCopy) { |
| 70 | + callback_function setup_lambda = [this](const State&) { setup_calls++; }; |
| 71 | + callback_function teardown_lambda = [this](const State&) { |
| 72 | + teardown_calls++; |
| 73 | + }; |
| 74 | + bm->Setup(setup_lambda); |
| 75 | + bm->Teardown(teardown_lambda); |
| 76 | + RunSpecifiedBenchmarks(&null_reporter); |
| 77 | + EXPECT_EQ(setup_calls, 1); |
| 78 | + EXPECT_EQ(teardown_calls, 1); |
| 79 | +} |
| 80 | + |
| 81 | +// Test that Setup/Teardown can correctly take std::function |
| 82 | +TEST_F(BenchmarkTest, CallbackFunctionMove) { |
| 83 | + callback_function setup_lambda = [this](const State&) { setup_calls++; }; |
| 84 | + callback_function teardown_lambda = [this](const State&) { |
| 85 | + teardown_calls++; |
| 86 | + }; |
| 87 | + bm->Setup(std::move(setup_lambda)); |
| 88 | + bm->Teardown(std::move(teardown_lambda)); |
| 89 | + RunSpecifiedBenchmarks(&null_reporter); |
| 90 | + EXPECT_EQ(setup_calls, 1); |
| 91 | + EXPECT_EQ(teardown_calls, 1); |
| 92 | +} |
| 93 | + |
| 94 | +// Test that Setup/Teardown can correctly take functors |
| 95 | +TEST_F(BenchmarkTest, FunctorCopy) { |
| 96 | + Functor func; |
| 97 | + bm->Setup(func); |
| 98 | + bm->Teardown(func); |
| 99 | + RunSpecifiedBenchmarks(&null_reporter); |
| 100 | + EXPECT_EQ(functor_called, 2); |
| 101 | +} |
| 102 | + |
| 103 | +// Test that Setup/Teardown can correctly take functors |
| 104 | +TEST_F(BenchmarkTest, FunctorMove) { |
| 105 | + Functor func1; |
| 106 | + Functor func2; |
| 107 | + bm->Setup(std::move(func1)); |
| 108 | + bm->Teardown(std::move(func2)); |
| 109 | + RunSpecifiedBenchmarks(&null_reporter); |
| 110 | + EXPECT_EQ(functor_called, 2); |
| 111 | +} |
| 112 | + |
| 113 | +// Test that Setup/Teardown can not take nullptr |
| 114 | +TEST_F(BenchmarkTest, NullptrTest) { |
| 115 | +#if GTEST_HAS_DEATH_TEST |
| 116 | + // Tests only runnable in debug mode (when BM_CHECK is enabled). |
| 117 | +#ifndef NDEBUG |
| 118 | +#ifndef TEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS |
| 119 | + EXPECT_DEATH(bm->Setup(nullptr), "setup != nullptr"); |
| 120 | + EXPECT_DEATH(bm->Teardown(nullptr), "teardown != nullptr"); |
| 121 | +#else |
| 122 | + GTEST_SKIP() << "Test skipped because BM_CHECK is disabled"; |
| 123 | +#endif |
| 124 | +#endif |
| 125 | +#endif |
| 126 | +} |
0 commit comments