1
2#include <cassert>
3#include <memory>
4
5#include "benchmark/benchmark.h"
6
7#define FIXTURE_BECHMARK_NAME MyFixture
8
9class FIXTURE_BECHMARK_NAME : public ::benchmark::Fixture {
10 public:
11 void SetUp(const ::benchmark::State& state) override {
12 if (state.thread_index() == 0) {
13 assert(data.get() == nullptr);
14 data.reset(p: new int(42));
15 }
16 }
17
18 void TearDown(const ::benchmark::State& state) override {
19 if (state.thread_index() == 0) {
20 assert(data.get() != nullptr);
21 data.reset();
22 }
23 }
24
25 ~FIXTURE_BECHMARK_NAME() override { assert(data == nullptr); }
26
27 std::unique_ptr<int> data;
28};
29
30BENCHMARK_F(FIXTURE_BECHMARK_NAME, Foo)(benchmark::State& st) {
31 assert(data.get() != nullptr);
32 assert(*data == 42);
33 for (auto _ : st) {
34 }
35}
36
37BENCHMARK_DEFINE_F(FIXTURE_BECHMARK_NAME, Bar)(benchmark::State& st) {
38 if (st.thread_index() == 0) {
39 assert(data.get() != nullptr);
40 assert(*data == 42);
41 }
42 for (auto _ : st) {
43 assert(data.get() != nullptr);
44 assert(*data == 42);
45 }
46 st.SetItemsProcessed(st.range(pos: 0));
47}
48BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, Bar)->Arg(x: 42);
49BENCHMARK_REGISTER_F(FIXTURE_BECHMARK_NAME, Bar)->Arg(x: 42)->ThreadPerCpu();
50
51BENCHMARK_MAIN();
52

source code of third-party/benchmark/test/fixture_test.cc