1 | #include <cassert> |
2 | #include <iostream> |
3 | #include <set> |
4 | #include <vector> |
5 | |
6 | #include "benchmark/benchmark.h" |
7 | |
8 | class MultipleRangesFixture : public ::benchmark::Fixture { |
9 | public: |
10 | MultipleRangesFixture() |
11 | : expectedValues({{1, 3, 5}, |
12 | {1, 3, 8}, |
13 | {1, 3, 15}, |
14 | {2, 3, 5}, |
15 | {2, 3, 8}, |
16 | {2, 3, 15}, |
17 | {1, 4, 5}, |
18 | {1, 4, 8}, |
19 | {1, 4, 15}, |
20 | {2, 4, 5}, |
21 | {2, 4, 8}, |
22 | {2, 4, 15}, |
23 | {1, 7, 5}, |
24 | {1, 7, 8}, |
25 | {1, 7, 15}, |
26 | {2, 7, 5}, |
27 | {2, 7, 8}, |
28 | {2, 7, 15}, |
29 | {7, 6, 3}}) {} |
30 | |
31 | void SetUp(const ::benchmark::State& state) override { |
32 | std::vector<int64_t> ranges = {state.range(pos: 0), state.range(pos: 1), |
33 | state.range(pos: 2)}; |
34 | |
35 | assert(expectedValues.find(ranges) != expectedValues.end()); |
36 | |
37 | actualValues.insert(x: ranges); |
38 | } |
39 | |
40 | // NOTE: This is not TearDown as we want to check after _all_ runs are |
41 | // complete. |
42 | ~MultipleRangesFixture() override { |
43 | if (actualValues != expectedValues) { |
44 | std::cout << "EXPECTED\n" ; |
45 | for (const auto& v : expectedValues) { |
46 | std::cout << "{" ; |
47 | for (int64_t iv : v) { |
48 | std::cout << iv << ", " ; |
49 | } |
50 | std::cout << "}\n" ; |
51 | } |
52 | std::cout << "ACTUAL\n" ; |
53 | for (const auto& v : actualValues) { |
54 | std::cout << "{" ; |
55 | for (int64_t iv : v) { |
56 | std::cout << iv << ", " ; |
57 | } |
58 | std::cout << "}\n" ; |
59 | } |
60 | } |
61 | } |
62 | |
63 | std::set<std::vector<int64_t>> expectedValues; |
64 | std::set<std::vector<int64_t>> actualValues; |
65 | }; |
66 | |
67 | BENCHMARK_DEFINE_F(MultipleRangesFixture, Empty)(benchmark::State& state) { |
68 | for (auto _ : state) { |
69 | int64_t product = state.range(pos: 0) * state.range(pos: 1) * state.range(pos: 2); |
70 | for (int64_t x = 0; x < product; x++) { |
71 | benchmark::DoNotOptimize(value&: x); |
72 | } |
73 | } |
74 | } |
75 | |
76 | BENCHMARK_REGISTER_F(MultipleRangesFixture, Empty) |
77 | ->RangeMultiplier(multiplier: 2) |
78 | ->Ranges(ranges: {{1, 2}, {3, 7}, {5, 15}}) |
79 | ->Args(args: {7, 6, 3}); |
80 | |
81 | void BM_CheckDefaultArgument(benchmark::State& state) { |
82 | // Test that the 'range()' without an argument is the same as 'range(0)'. |
83 | assert(state.range() == state.range(0)); |
84 | assert(state.range() != state.range(1)); |
85 | for (auto _ : state) { |
86 | } |
87 | } |
88 | BENCHMARK(BM_CheckDefaultArgument)->Ranges(ranges: {{1, 5}, {6, 10}}); |
89 | |
90 | static void BM_MultipleRanges(benchmark::State& st) { |
91 | for (auto _ : st) { |
92 | } |
93 | } |
94 | BENCHMARK(BM_MultipleRanges)->Ranges(ranges: {{5, 5}, {6, 6}}); |
95 | |
96 | BENCHMARK_MAIN(); |
97 | |