filter_test.cc
Go to the documentation of this file.
1 #include "benchmark/benchmark.h"
2 
3 #include <cassert>
4 #include <cmath>
5 #include <cstdint>
6 #include <cstdlib>
7 
8 #include <iostream>
9 #include <limits>
10 #include <sstream>
11 #include <string>
12 
13 namespace {
14 
15 class TestReporter : public benchmark::ConsoleReporter {
16  public:
17  virtual bool ReportContext(const Context& context) {
18  return ConsoleReporter::ReportContext(context);
19  };
20 
21  virtual void ReportRuns(const std::vector<Run>& report) {
22  ++count_;
24  };
25 
26  TestReporter() : count_(0) {}
27 
28  virtual ~TestReporter() {}
29 
30  size_t GetCount() const { return count_; }
31 
32  private:
33  mutable size_t count_;
34 };
35 
36 } // end namespace
37 
38 static void NoPrefix(benchmark::State& state) {
39  while (state.KeepRunning()) {
40  }
41 }
43 
44 static void BM_Foo(benchmark::State& state) {
45  while (state.KeepRunning()) {
46  }
47 }
49 
50 static void BM_Bar(benchmark::State& state) {
51  while (state.KeepRunning()) {
52  }
53 }
55 
56 static void BM_FooBar(benchmark::State& state) {
57  while (state.KeepRunning()) {
58  }
59 }
61 
62 static void BM_FooBa(benchmark::State& state) {
63  while (state.KeepRunning()) {
64  }
65 }
67 
68 int main(int argc, char **argv) {
69  bool list_only = false;
70  for (int i = 0; i < argc; ++i)
71  list_only |= std::string(argv[i]).find("--benchmark_list_tests") !=
72  std::string::npos;
73 
74  benchmark::Initialize(&argc, argv);
75 
76  TestReporter test_reporter;
77  const size_t returned_count =
78  benchmark::RunSpecifiedBenchmarks(&test_reporter);
79 
80  if (argc == 2) {
81  // Make sure we ran all of the tests
82  std::stringstream ss(argv[1]);
83  size_t expected_return;
84  ss >> expected_return;
85 
86  if (returned_count != expected_return) {
87  std::cerr << "ERROR: Expected " << expected_return
88  << " tests to match the filter but returned_count = "
89  << returned_count << std::endl;
90  return -1;
91  }
92 
93  const size_t expected_reports = list_only ? 0 : expected_return;
94  const size_t reports_count = test_reporter.GetCount();
95  if (reports_count != expected_reports) {
96  std::cerr << "ERROR: Expected " << expected_reports
97  << " tests to be run but reported_count = " << reports_count
98  << std::endl;
99  return -1;
100  }
101  }
102 
103  return 0;
104 }
benchmark::ConsoleReporter::ReportContext
virtual bool ReportContext(const Context &context)
Definition: console_reporter.cc:36
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
NoPrefix
static void NoPrefix(benchmark::State &state)
Definition: filter_test.cc:38
benchmark::RunSpecifiedBenchmarks
size_t RunSpecifiedBenchmarks()
Definition: benchmark.cc:571
benchmark::ConsoleReporter::ReportRuns
virtual void ReportRuns(const std::vector< Run > &reports)
Definition: console_reporter.cc:72
benchmark::Initialize
void Initialize(int *argc, char **argv)
Definition: benchmark.cc:703
benchmark::ConsoleReporter
Definition: benchmark.h:1132
benchmark::State::KeepRunning
bool KeepRunning()
Definition: benchmark.h:404
BM_Foo
static void BM_Foo(benchmark::State &state)
Definition: filter_test.cc:44
i
int i
Definition: gmock-matchers_test.cc:764
main
int main(int argc, char **argv)
Definition: filter_test.cc:68
benchmark::State
Definition: benchmark.h:399
BM_FooBa
static void BM_FooBa(benchmark::State &state)
Definition: filter_test.cc:62
BM_Bar
static void BM_Bar(benchmark::State &state)
Definition: filter_test.cc:50
BENCHMARK
BENCHMARK(NoPrefix)
BM_FooBar
static void BM_FooBar(benchmark::State &state)
Definition: filter_test.cc:56
benchmark.h


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:51