Skip to content

Commit

Permalink
Verify RegisterProfilerManager doesn't overwrite an existing registra…
Browse files Browse the repository at this point in the history
…tion

Tested:
Configure with:
cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on
Then run:
ctest -R profiler_manager_gtest
Before change test fails (expected), after change test passes (expected)
  • Loading branch information
xdje42 committed Oct 23, 2024
1 parent 9bb6adf commit 227bca2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ if (BENCHMARK_ENABLE_GTEST_TESTS)
add_gtest(perf_counters_gtest)
add_gtest(time_unit_gtest)
add_gtest(min_time_parse_gtest)
add_gtest(profiler_manager_gtest)
endif(BENCHMARK_ENABLE_GTEST_TESTS)

###############################################################################
Expand Down
42 changes: 42 additions & 0 deletions test/profiler_manager_gtest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <memory>

#include "benchmark/benchmark.h"
#include "gtest/gtest.h"

namespace {

class TestProfilerManager : public benchmark::ProfilerManager {
public:
void AfterSetupStart() override { ++start_called; }
void BeforeTeardownStop() override { ++stop_called; }

int start_called = 0;
int stop_called = 0;
};

void BM_empty(benchmark::State& state) {
for (auto _ : state) {
auto iterations = state.iterations();
benchmark::DoNotOptimize(iterations);
}
}
BENCHMARK(BM_empty);

TEST(ProfilerManager, ReregisterManager) {
#if GTEST_HAS_DEATH_TEST
// Tests only runnable in debug mode (when BM_CHECK is enabled).
#ifndef NDEBUG
#ifndef TEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS
ASSERT_DEATH_IF_SUPPORTED(
{
std::unique_ptr<TestProfilerManager> pm(new TestProfilerManager());
benchmark::RegisterProfilerManager(pm.get());
benchmark::RegisterProfilerManager(pm.get());
},
"RegisterProfilerManager");
#endif
#endif
#endif
}

} // namespace

0 comments on commit 227bca2

Please sign in to comment.