blob: dd64a336553157dfac9fc327f451030fc10a48cd [file] [log] [blame]
Eric Fiselierb08d8b12016-07-19 23:07:03 +00001// Testing:
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.
6//
7// NOTE: Users should NOT include or use src/check.h. This is only done in
8// order to test library internals.
9
Eric Fiselierb08d8b12016-07-19 23:07:03 +000010#include <cstdlib>
Eric Fiselierfbc9ff22016-11-05 00:30:27 +000011#include <stdexcept>
12
13#include "../src/check.h"
Eric Fiselier19039762018-01-18 04:23:01 +000014#include "benchmark/benchmark.h"
Eric Fiselierb08d8b12016-07-19 23:07:03 +000015
16#if defined(__GNUC__) && !defined(__EXCEPTIONS)
17#define TEST_HAS_NO_EXCEPTIONS
18#endif
19
20void TestHandler() {
21#ifndef TEST_HAS_NO_EXCEPTIONS
22 throw std::logic_error("");
23#else
24 std::abort();
25#endif
26}
27
28void try_invalid_pause_resume(benchmark::State& state) {
Eric Fiselier133a7202017-04-18 07:17:20 +000029#if !defined(TEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS) && !defined(TEST_HAS_NO_EXCEPTIONS)
Eric Fiselierb08d8b12016-07-19 23:07:03 +000030 try {
31 state.PauseTiming();
32 std::abort();
Eric Fiselierfbc9ff22016-11-05 00:30:27 +000033 } catch (std::logic_error const&) {
34 }
Eric Fiselierb08d8b12016-07-19 23:07:03 +000035 try {
36 state.ResumeTiming();
37 std::abort();
Eric Fiselierfbc9ff22016-11-05 00:30:27 +000038 } catch (std::logic_error const&) {
39 }
Eric Fiselierb08d8b12016-07-19 23:07:03 +000040#else
Eric Fiselierfbc9ff22016-11-05 00:30:27 +000041 (void)state; // avoid unused warning
Eric Fiselierb08d8b12016-07-19 23:07:03 +000042#endif
43}
44
45void BM_diagnostic_test(benchmark::State& state) {
46 static bool called_once = false;
47
48 if (called_once == false) try_invalid_pause_resume(state);
49
Eric Fiselier19039762018-01-18 04:23:01 +000050 for (auto _ : state) {
Eric Fiselierb08d8b12016-07-19 23:07:03 +000051 benchmark::DoNotOptimize(state.iterations());
52 }
53
54 if (called_once == false) try_invalid_pause_resume(state);
55
56 called_once = true;
57}
58BENCHMARK(BM_diagnostic_test);
59
Eric Fiselier19039762018-01-18 04:23:01 +000060
61void BM_diagnostic_test_keep_running(benchmark::State& state) {
62 static bool called_once = false;
63
64 if (called_once == false) try_invalid_pause_resume(state);
65
66 while(state.KeepRunning()) {
67 benchmark::DoNotOptimize(state.iterations());
68 }
69
70 if (called_once == false) try_invalid_pause_resume(state);
71
72 called_once = true;
73}
74BENCHMARK(BM_diagnostic_test_keep_running);
75
Eric Fiselierfbc9ff22016-11-05 00:30:27 +000076int main(int argc, char* argv[]) {
Eric Fiselierb08d8b12016-07-19 23:07:03 +000077 benchmark::internal::GetAbortHandler() = &TestHandler;
78 benchmark::Initialize(&argc, argv);
79 benchmark::RunSpecifiedBenchmarks();
80}