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