1 #include "benchmark/benchmark.h"
2
3 #include <cassert>
4 #include <set>
5
6 class MultipleRangesFixture : public ::benchmark::Fixture {
7 public:
MultipleRangesFixture()8 MultipleRangesFixture()
9 : expectedValues({{1, 3, 5},
10 {1, 3, 8},
11 {1, 3, 15},
12 {2, 3, 5},
13 {2, 3, 8},
14 {2, 3, 15},
15 {1, 4, 5},
16 {1, 4, 8},
17 {1, 4, 15},
18 {2, 4, 5},
19 {2, 4, 8},
20 {2, 4, 15},
21 {1, 7, 5},
22 {1, 7, 8},
23 {1, 7, 15},
24 {2, 7, 5},
25 {2, 7, 8},
26 {2, 7, 15},
27 {7, 6, 3}}) {}
28
SetUp(const::benchmark::State & state)29 void SetUp(const ::benchmark::State& state) {
30 std::vector<int> ranges = {state.range(0), state.range(1), state.range(2)};
31
32 assert(expectedValues.find(ranges) != expectedValues.end());
33
34 actualValues.insert(ranges);
35 }
36
~MultipleRangesFixture()37 virtual ~MultipleRangesFixture() {
38 assert(actualValues.size() == expectedValues.size());
39 }
40
41 std::set<std::vector<int>> expectedValues;
42 std::set<std::vector<int>> actualValues;
43 };
44
BENCHMARK_DEFINE_F(MultipleRangesFixture,Empty)45 BENCHMARK_DEFINE_F(MultipleRangesFixture, Empty)(benchmark::State& state) {
46 for (auto _ : state) {
47 int product = state.range(0) * state.range(1) * state.range(2);
48 for (int x = 0; x < product; x++) {
49 benchmark::DoNotOptimize(x);
50 }
51 }
52 }
53
54 BENCHMARK_REGISTER_F(MultipleRangesFixture, Empty)
55 ->RangeMultiplier(2)
56 ->Ranges({{1, 2}, {3, 7}, {5, 15}})
57 ->Args({7, 6, 3});
58
BM_CheckDefaultArgument(benchmark::State & state)59 void BM_CheckDefaultArgument(benchmark::State& state) {
60 // Test that the 'range()' without an argument is the same as 'range(0)'.
61 assert(state.range() == state.range(0));
62 assert(state.range() != state.range(1));
63 for (auto _ : state) {
64 }
65 }
66 BENCHMARK(BM_CheckDefaultArgument)->Ranges({{1, 5}, {6, 10}});
67
BM_MultipleRanges(benchmark::State & st)68 static void BM_MultipleRanges(benchmark::State& st) {
69 for (auto _ : st) {
70 }
71 }
72 BENCHMARK(BM_MultipleRanges)->Ranges({{5, 5}, {6, 6}});
73
74 BENCHMARK_MAIN();
75