2016-05-24 05:27:43 +08:00
|
|
|
// Testing:
|
|
|
|
// State::PauseTiming()
|
|
|
|
// State::ResumeTiming()
|
|
|
|
// Test that CHECK's within these function diagnose when they are called
|
|
|
|
// outside of the KeepRunning() loop.
|
|
|
|
//
|
|
|
|
// NOTE: Users should NOT include or use src/check.h. This is only done in
|
|
|
|
// order to test library internals.
|
2016-05-24 05:05:55 +08:00
|
|
|
|
2016-05-24 05:38:30 +08:00
|
|
|
#include <cstdlib>
|
2016-10-08 02:04:50 +08:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include "../src/check.h"
|
2017-07-05 06:31:47 +08:00
|
|
|
#include "benchmark/benchmark.h"
|
2016-05-24 05:38:30 +08:00
|
|
|
|
|
|
|
#if defined(__GNUC__) && !defined(__EXCEPTIONS)
|
|
|
|
#define TEST_HAS_NO_EXCEPTIONS
|
|
|
|
#endif
|
2016-05-24 05:05:55 +08:00
|
|
|
|
2016-05-24 05:27:43 +08:00
|
|
|
void TestHandler() {
|
2016-05-24 05:38:30 +08:00
|
|
|
#ifndef TEST_HAS_NO_EXCEPTIONS
|
2016-05-24 05:05:55 +08:00
|
|
|
throw std::logic_error("");
|
2016-05-24 05:38:30 +08:00
|
|
|
#else
|
|
|
|
std::abort();
|
|
|
|
#endif
|
2016-05-24 05:05:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void try_invalid_pause_resume(benchmark::State& state) {
|
2021-11-11 00:22:31 +08:00
|
|
|
#if !defined(TEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS) && \
|
|
|
|
!defined(TEST_HAS_NO_EXCEPTIONS)
|
2016-05-24 05:05:55 +08:00
|
|
|
try {
|
|
|
|
state.PauseTiming();
|
|
|
|
std::abort();
|
2016-10-08 02:04:50 +08:00
|
|
|
} catch (std::logic_error const&) {
|
|
|
|
}
|
2016-05-24 05:05:55 +08:00
|
|
|
try {
|
|
|
|
state.ResumeTiming();
|
|
|
|
std::abort();
|
2016-10-08 02:04:50 +08:00
|
|
|
} catch (std::logic_error const&) {
|
|
|
|
}
|
2016-05-24 05:05:55 +08:00
|
|
|
#else
|
2016-10-08 02:04:50 +08:00
|
|
|
(void)state; // avoid unused warning
|
2016-05-24 05:05:55 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void BM_diagnostic_test(benchmark::State& state) {
|
|
|
|
static bool called_once = false;
|
|
|
|
|
|
|
|
if (called_once == false) try_invalid_pause_resume(state);
|
|
|
|
|
2017-10-18 02:17:02 +08:00
|
|
|
for (auto _ : state) {
|
2023-03-06 22:47:54 +08:00
|
|
|
auto iterations = state.iterations();
|
|
|
|
benchmark::DoNotOptimize(iterations);
|
2016-05-24 05:05:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (called_once == false) try_invalid_pause_resume(state);
|
|
|
|
|
|
|
|
called_once = true;
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_diagnostic_test);
|
|
|
|
|
2017-10-18 02:17:02 +08:00
|
|
|
void BM_diagnostic_test_keep_running(benchmark::State& state) {
|
|
|
|
static bool called_once = false;
|
|
|
|
|
|
|
|
if (called_once == false) try_invalid_pause_resume(state);
|
|
|
|
|
2021-11-11 00:22:31 +08:00
|
|
|
while (state.KeepRunning()) {
|
2023-03-06 22:47:54 +08:00
|
|
|
auto iterations = state.iterations();
|
|
|
|
benchmark::DoNotOptimize(iterations);
|
2017-10-18 02:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (called_once == false) try_invalid_pause_resume(state);
|
|
|
|
|
|
|
|
called_once = true;
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_diagnostic_test_keep_running);
|
|
|
|
|
2016-10-08 02:04:50 +08:00
|
|
|
int main(int argc, char* argv[]) {
|
2023-05-10 17:18:43 +08:00
|
|
|
#ifdef NDEBUG
|
|
|
|
// This test is exercising functionality for debug builds, which are not
|
|
|
|
// available in release builds. Skip the test if we are in that environment
|
|
|
|
// to avoid a test failure.
|
|
|
|
std::cout << "Diagnostic test disabled in release build" << std::endl;
|
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
#else
|
2016-05-24 05:27:43 +08:00
|
|
|
benchmark::internal::GetAbortHandler() = &TestHandler;
|
2016-05-24 05:05:55 +08:00
|
|
|
benchmark::Initialize(&argc, argv);
|
|
|
|
benchmark::RunSpecifiedBenchmarks();
|
2023-05-10 17:18:43 +08:00
|
|
|
#endif
|
2016-05-24 05:07:54 +08:00
|
|
|
}
|