skip_with_error_test.cc
Go to the documentation of this file.
1 
2 #undef NDEBUG
3 #include <cassert>
4 #include <vector>
5 
6 #include "../src/check.h" // NOTE: check.h is for internal use only!
7 #include "benchmark/benchmark.h"
8 
9 namespace {
10 
11 class TestReporter : public benchmark::ConsoleReporter {
12  public:
13  virtual bool ReportContext(const Context& context) {
14  return ConsoleReporter::ReportContext(context);
15  };
16 
17  virtual void ReportRuns(const std::vector<Run>& report) {
18  all_runs_.insert(all_runs_.end(), begin(report), end(report));
20  }
21 
22  TestReporter() {}
23  virtual ~TestReporter() {}
24 
25  mutable std::vector<Run> all_runs_;
26 };
27 
28 struct TestCase {
30  bool error_occurred;
31  std::string error_message;
32 
34 
35  void CheckRun(Run const& run) const {
36  CHECK(name == run.benchmark_name) << "expected " << name << " got "
37  << run.benchmark_name;
38  CHECK(error_occurred == run.error_occurred);
39  CHECK(error_message == run.error_message);
40  if (error_occurred) {
41  // CHECK(run.iterations == 0);
42  } else {
43  CHECK(run.iterations != 0);
44  }
45  }
46 };
47 
48 std::vector<TestCase> ExpectedResults;
49 
50 int AddCases(const char* base_name, std::initializer_list<TestCase> const& v) {
51  for (auto TC : v) {
52  TC.name = base_name + TC.name;
53  ExpectedResults.push_back(std::move(TC));
54  }
55  return 0;
56 }
57 
58 #define CONCAT(x, y) CONCAT2(x, y)
59 #define CONCAT2(x, y) x##y
60 #define ADD_CASES(...) int CONCAT(dummy, __LINE__) = AddCases(__VA_ARGS__)
61 
62 } // end namespace
63 
65  state.SkipWithError("error message");
66  while (state.KeepRunning()) {
67  assert(false);
68  }
69 }
71 ADD_CASES("BM_error_before_running", {{"", true, "error message"}});
72 
74  int first_iter = true;
75  while (state.KeepRunning()) {
76  if (state.range(0) == 1 && state.thread_index <= (state.threads / 2)) {
77  assert(first_iter);
78  first_iter = false;
79  state.SkipWithError("error message");
80  } else {
81  state.PauseTiming();
82  state.ResumeTiming();
83  }
84  }
85 }
86 BENCHMARK(BM_error_during_running)->Arg(1)->Arg(2)->ThreadRange(1, 8);
87 ADD_CASES("BM_error_during_running", {{"/1/threads:1", true, "error message"},
88  {"/1/threads:2", true, "error message"},
89  {"/1/threads:4", true, "error message"},
90  {"/1/threads:8", true, "error message"},
91  {"/2/threads:1", false, ""},
92  {"/2/threads:2", false, ""},
93  {"/2/threads:4", false, ""},
94  {"/2/threads:8", false, ""}});
95 
97  while (state.KeepRunning()) {
99  }
100  if (state.thread_index <= (state.threads / 2))
101  state.SkipWithError("error message");
102 }
103 BENCHMARK(BM_error_after_running)->ThreadRange(1, 8);
104 ADD_CASES("BM_error_after_running", {{"/threads:1", true, "error message"},
105  {"/threads:2", true, "error message"},
106  {"/threads:4", true, "error message"},
107  {"/threads:8", true, "error message"}});
108 
110  bool first_iter = true;
111  while (state.KeepRunning()) {
112  if (state.range(0) == 1 && state.thread_index <= (state.threads / 2)) {
113  assert(first_iter);
114  first_iter = false;
115  state.PauseTiming();
116  state.SkipWithError("error message");
117  } else {
118  state.PauseTiming();
119  state.ResumeTiming();
120  }
121  }
122 }
123 BENCHMARK(BM_error_while_paused)->Arg(1)->Arg(2)->ThreadRange(1, 8);
124 ADD_CASES("BM_error_while_paused", {{"/1/threads:1", true, "error message"},
125  {"/1/threads:2", true, "error message"},
126  {"/1/threads:4", true, "error message"},
127  {"/1/threads:8", true, "error message"},
128  {"/2/threads:1", false, ""},
129  {"/2/threads:2", false, ""},
130  {"/2/threads:4", false, ""},
131  {"/2/threads:8", false, ""}});
132 
133 int main(int argc, char* argv[]) {
134  benchmark::Initialize(&argc, argv);
135 
136  TestReporter test_reporter;
137  benchmark::RunSpecifiedBenchmarks(&test_reporter);
138 
140  auto EB = ExpectedResults.begin();
141 
142  for (Run const& run : test_reporter.all_runs_) {
143  assert(EB != ExpectedResults.end());
144  EB->CheckRun(run);
145  ++EB;
146  }
147  assert(EB == ExpectedResults.end());
148 
149  return 0;
150 }
BM_error_after_running
void BM_error_after_running(benchmark::State &state)
Definition: skip_with_error_test.cc:96
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
BM_error_while_paused
void BM_error_while_paused(benchmark::State &state)
Definition: skip_with_error_test.cc:109
ADD_CASES
#define ADD_CASES(...)
Definition: skip_with_error_test.cc:60
BM_error_during_running
void BM_error_during_running(benchmark::State &state)
Definition: skip_with_error_test.cc:73
end
GLuint GLuint end
Definition: glcorearb.h:2858
BENCHMARK
BENCHMARK(BM_error_before_running)
benchmark::ConsoleReporter::ReportContext
virtual bool ReportContext(const Context &context)
Definition: console_reporter.cc:36
main
int main(int argc, char *argv[])
Definition: skip_with_error_test.cc:133
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark.h:328
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
benchmark::State::PauseTiming
void PauseTiming()
Definition: benchmark.cc:420
benchmark::RunSpecifiedBenchmarks
size_t RunSpecifiedBenchmarks()
Definition: benchmark.cc:571
begin
static size_t begin(const upb_table *t)
Definition: php/ext/google/protobuf/upb.c:4898
AddCases
int AddCases(TestCaseID ID, std::initializer_list< TestCase > il)
Definition: output_test_helper.cc:343
benchmark::State::SkipWithError
void SkipWithError(const char *msg)
Definition: benchmark.cc:431
benchmark::State::iterations
BENCHMARK_ALWAYS_INLINE size_t iterations() const
Definition: benchmark.h:535
benchmark::ConsoleReporter::ReportRuns
virtual void ReportRuns(const std::vector< Run > &reports)
Definition: console_reporter.cc:72
CHECK
#define CHECK(x)
Definition: php/ext/google/protobuf/upb.c:8393
benchmark::Initialize
void Initialize(int *argc, char **argv)
Definition: benchmark.cc:703
TestCase
Definition: output_test.h:31
benchmark::ConsoleReporter
Definition: benchmark.h:1132
benchmark::BenchmarkReporter::Run
Definition: benchmark.h:1017
benchmark::State::KeepRunning
bool KeepRunning()
Definition: benchmark.h:404
benchmark::State::range
BENCHMARK_ALWAYS_INLINE int range(std::size_t pos=0) const
Definition: benchmark.h:523
v
const GLdouble * v
Definition: glcorearb.h:3106
googletest-break-on-failure-unittest.Run
def Run(command)
Definition: googletest-break-on-failure-unittest.py:76
benchmark::State
Definition: benchmark.h:399
BM_error_before_running
void BM_error_before_running(benchmark::State &state)
Definition: skip_with_error_test.cc:64
benchmark::State::threads
const int threads
Definition: benchmark.h:557
benchmark::State::thread_index
const int thread_index
Definition: benchmark.h:555
benchmark::State::ResumeTiming
void ResumeTiming()
Definition: benchmark.cc:426
benchmark.h


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