You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.8 KiB

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