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