Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 1 | // 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 Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 10 | #include <cstdlib> |
Eric Fiselier | fbc9ff2 | 2016-11-05 00:30:27 +0000 | [diff] [blame^] | 11 | #include <stdexcept> |
| 12 | |
| 13 | #include "../src/check.h" |
| 14 | #include "benchmark/benchmark_api.h" |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 15 | |
| 16 | #if defined(__GNUC__) && !defined(__EXCEPTIONS) |
| 17 | #define TEST_HAS_NO_EXCEPTIONS |
| 18 | #endif |
| 19 | |
| 20 | void TestHandler() { |
| 21 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 22 | throw std::logic_error(""); |
| 23 | #else |
| 24 | std::abort(); |
| 25 | #endif |
| 26 | } |
| 27 | |
| 28 | void try_invalid_pause_resume(benchmark::State& state) { |
| 29 | #if !defined(NDEBUG) && !defined(TEST_HAS_NO_EXCEPTIONS) |
| 30 | try { |
| 31 | state.PauseTiming(); |
| 32 | std::abort(); |
Eric Fiselier | fbc9ff2 | 2016-11-05 00:30:27 +0000 | [diff] [blame^] | 33 | } catch (std::logic_error const&) { |
| 34 | } |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 35 | try { |
| 36 | state.ResumeTiming(); |
| 37 | std::abort(); |
Eric Fiselier | fbc9ff2 | 2016-11-05 00:30:27 +0000 | [diff] [blame^] | 38 | } catch (std::logic_error const&) { |
| 39 | } |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 40 | #else |
Eric Fiselier | fbc9ff2 | 2016-11-05 00:30:27 +0000 | [diff] [blame^] | 41 | (void)state; // avoid unused warning |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 42 | #endif |
| 43 | } |
| 44 | |
| 45 | void BM_diagnostic_test(benchmark::State& state) { |
| 46 | static bool called_once = false; |
| 47 | |
| 48 | if (called_once == false) try_invalid_pause_resume(state); |
| 49 | |
| 50 | while (state.KeepRunning()) { |
| 51 | benchmark::DoNotOptimize(state.iterations()); |
| 52 | } |
| 53 | |
| 54 | if (called_once == false) try_invalid_pause_resume(state); |
| 55 | |
| 56 | called_once = true; |
| 57 | } |
| 58 | BENCHMARK(BM_diagnostic_test); |
| 59 | |
Eric Fiselier | fbc9ff2 | 2016-11-05 00:30:27 +0000 | [diff] [blame^] | 60 | int main(int argc, char* argv[]) { |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 61 | benchmark::internal::GetAbortHandler() = &TestHandler; |
| 62 | benchmark::Initialize(&argc, argv); |
| 63 | benchmark::RunSpecifiedBenchmarks(); |
| 64 | } |