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.

82 lines
1.9 KiB

2 months ago
  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) && \
  25. !defined(TEST_HAS_NO_EXCEPTIONS)
  26. try {
  27. state.PauseTiming();
  28. std::abort();
  29. } catch (std::logic_error const&) {
  30. }
  31. try {
  32. state.ResumeTiming();
  33. std::abort();
  34. } catch (std::logic_error const&) {
  35. }
  36. #else
  37. (void)state; // avoid unused warning
  38. #endif
  39. }
  40. void BM_diagnostic_test(benchmark::State& state) {
  41. static bool called_once = false;
  42. if (called_once == false) try_invalid_pause_resume(state);
  43. for (auto _ : state) {
  44. auto iterations = state.iterations();
  45. benchmark::DoNotOptimize(iterations);
  46. }
  47. if (called_once == false) try_invalid_pause_resume(state);
  48. called_once = true;
  49. }
  50. BENCHMARK(BM_diagnostic_test);
  51. void BM_diagnostic_test_keep_running(benchmark::State& state) {
  52. static bool called_once = false;
  53. if (called_once == false) try_invalid_pause_resume(state);
  54. while (state.KeepRunning()) {
  55. auto iterations = state.iterations();
  56. benchmark::DoNotOptimize(iterations);
  57. }
  58. if (called_once == false) try_invalid_pause_resume(state);
  59. called_once = true;
  60. }
  61. BENCHMARK(BM_diagnostic_test_keep_running);
  62. int main(int argc, char* argv[]) {
  63. benchmark::internal::GetAbortHandler() = &TestHandler;
  64. benchmark::Initialize(&argc, argv);
  65. benchmark::RunSpecifiedBenchmarks();
  66. }