• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <algorithm>
2 #include <cassert>
3 #include <cmath>
4 #include <cstdint>
5 #include <cstdlib>
6 #include <iostream>
7 #include <limits>
8 #include <sstream>
9 #include <string>
10 
11 #include "benchmark/benchmark.h"
12 
13 namespace {
14 
15 class TestReporter : public benchmark::ConsoleReporter {
16  public:
ReportContext(const Context & context)17   bool ReportContext(const Context& context) override {
18     return ConsoleReporter::ReportContext(context);
19   };
20 
ReportRuns(const std::vector<Run> & report)21   void ReportRuns(const std::vector<Run>& report) override {
22     ++count_;
23     max_family_index_ = std::max(max_family_index_, report[0].family_index);
24     ConsoleReporter::ReportRuns(report);
25   };
26 
TestReporter()27   TestReporter() : count_(0), max_family_index_(0) {}
28 
~TestReporter()29   ~TestReporter() override {}
30 
GetCount() const31   int GetCount() const { return count_; }
32 
GetMaxFamilyIndex() const33   int64_t GetMaxFamilyIndex() const { return max_family_index_; }
34 
35  private:
36   mutable int count_;
37   mutable int64_t max_family_index_;
38 };
39 
40 }  // end namespace
41 
NoPrefix(benchmark::State & state)42 static void NoPrefix(benchmark::State& state) {
43   for (auto _ : state) {
44   }
45 }
46 BENCHMARK(NoPrefix);
47 
BM_Foo(benchmark::State & state)48 static void BM_Foo(benchmark::State& state) {
49   for (auto _ : state) {
50   }
51 }
52 BENCHMARK(BM_Foo);
53 
BM_Bar(benchmark::State & state)54 static void BM_Bar(benchmark::State& state) {
55   for (auto _ : state) {
56   }
57 }
58 BENCHMARK(BM_Bar);
59 
BM_FooBar(benchmark::State & state)60 static void BM_FooBar(benchmark::State& state) {
61   for (auto _ : state) {
62   }
63 }
64 BENCHMARK(BM_FooBar);
65 
BM_FooBa(benchmark::State & state)66 static void BM_FooBa(benchmark::State& state) {
67   for (auto _ : state) {
68   }
69 }
70 BENCHMARK(BM_FooBa);
71 
main(int argc,char ** argv)72 int main(int argc, char** argv) {
73   bool list_only = false;
74   for (int i = 0; i < argc; ++i)
75     list_only |= std::string(argv[i]).find("--benchmark_list_tests") !=
76                  std::string::npos;
77 
78   benchmark::Initialize(&argc, argv);
79 
80   TestReporter test_reporter;
81   const int64_t returned_count =
82       static_cast<int64_t>(benchmark::RunSpecifiedBenchmarks(&test_reporter));
83 
84   if (argc == 2) {
85     // Make sure we ran all of the tests
86     std::stringstream ss(argv[1]);
87     int64_t expected_return;
88     ss >> expected_return;
89 
90     if (returned_count != expected_return) {
91       std::cerr << "ERROR: Expected " << expected_return
92                 << " tests to match the filter but returned_count = "
93                 << returned_count << std::endl;
94       return -1;
95     }
96 
97     const int64_t expected_reports = list_only ? 0 : expected_return;
98     const int64_t reports_count = test_reporter.GetCount();
99     if (reports_count != expected_reports) {
100       std::cerr << "ERROR: Expected " << expected_reports
101                 << " tests to be run but reported_count = " << reports_count
102                 << std::endl;
103       return -1;
104     }
105 
106     const int64_t max_family_index = test_reporter.GetMaxFamilyIndex();
107     const int64_t num_families = reports_count == 0 ? 0 : 1 + max_family_index;
108     if (num_families != expected_reports) {
109       std::cerr << "ERROR: Expected " << expected_reports
110                 << " test families to be run but num_families = "
111                 << num_families << std::endl;
112       return -1;
113     }
114   }
115 
116   return 0;
117 }
118