You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.7 KiB

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