2 // State::PauseTiming()
3 // State::ResumeTiming()
4 // Test that CHECK's within these function diagnose when they are called
5 // outside of the KeepRunning() loop.
7 // NOTE: Users should NOT include or use src/check.h. This is only done in
8 // order to test library internals.
13 #include "../src/check.h"
14 #include "benchmark/benchmark.h"
16 #if defined(__GNUC__) && !defined(__EXCEPTIONS)
17 #define TEST_HAS_NO_EXCEPTIONS
21 #ifndef TEST_HAS_NO_EXCEPTIONS
22 throw std::logic_error("");
28 void try_invalid_pause_resume(benchmark::State
& state
) {
29 #if !defined(TEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS) && \
30 !defined(TEST_HAS_NO_EXCEPTIONS)
34 } catch (std::logic_error
const&) {
39 } catch (std::logic_error
const&) {
42 (void)state
; // avoid unused warning
46 void BM_diagnostic_test(benchmark::State
& state
) {
47 static bool called_once
= false;
49 if (called_once
== false) try_invalid_pause_resume(state
);
51 for (auto _
: state
) {
52 benchmark::DoNotOptimize(state
.iterations());
55 if (called_once
== false) try_invalid_pause_resume(state
);
59 BENCHMARK(BM_diagnostic_test
);
61 void BM_diagnostic_test_keep_running(benchmark::State
& state
) {
62 static bool called_once
= false;
64 if (called_once
== false) try_invalid_pause_resume(state
);
66 while (state
.KeepRunning()) {
67 benchmark::DoNotOptimize(state
.iterations());
70 if (called_once
== false) try_invalid_pause_resume(state
);
74 BENCHMARK(BM_diagnostic_test_keep_running
);
76 int main(int argc
, char* argv
[]) {
77 benchmark::internal::GetAbortHandler() = &TestHandler
;
78 benchmark::Initialize(&argc
, argv
);
79 benchmark::RunSpecifiedBenchmarks();