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