#include #include #include #include using namespace Void::Bench; // ── run ─────────────────────────────────────────────────────────────────────── Void::Testing::Test bench_run_correct_iteration_count() { int count = 0; const auto r = run("counter", 10, [&] { ++count; }, /*warmup=*/0); Void::Testing::expect_eq(r.iterations, std::size_t{10}); Void::Testing::expect_eq(count, 10); } Void::Testing::Test bench_run_warmup_not_in_result() { int count = 0; const auto r = run("counter", 5, [&] { ++count; }, /*warmup=*/3); Void::Testing::expect_eq(r.iterations, std::size_t{5}); Void::Testing::expect_eq(count, 8); // 3 warmup + 5 measured } Void::Testing::Test bench_run_name_preserved() { const auto r = run("my_bench", 1, [] {}); Void::Testing::expect_eq(r.name, std::string{"my_bench"}); } Void::Testing::Test bench_run_total_positive() { const auto r = run("work", 100, [] { volatile int x = 0; for (int i = 0; i < 100; ++i) x += i; }, 0); Void::Testing::expect(r.total.count() >= 0); } Void::Testing::Test bench_run_min_le_mean_le_max() { const auto r = run("timing", 50, [] { volatile int x = 1; do_not_optimize(x); }, 0); Void::Testing::expect(r.min_ns.count() <= static_cast(r.mean_ns() + 0.5)); Void::Testing::expect(r.max_ns.count() >= static_cast(r.mean_ns())); } Void::Testing::Test bench_run_ops_per_sec_positive() { const auto r = run("ops", 100, [] { volatile int x = 0; (void)x; }, 0); Void::Testing::expect(r.ops_per_sec() > 0.0); } // ── run_for ─────────────────────────────────────────────────────────────────── Void::Testing::Test bench_run_for_runs_at_least_once() { const auto r = run_for("short", std::chrono::milliseconds{10}, [] {}, 0); Void::Testing::expect(r.iterations >= 1); } Void::Testing::Test bench_run_for_total_within_budget() { const auto budget = std::chrono::milliseconds{20}; const auto r = run_for("budget_check", budget, [] { volatile int x = 0; (void)x; }, 0); // Total time should be close to budget (allow 2× for timer imprecision) const auto budget_ns = std::chrono::duration_cast(budget * 2); Void::Testing::expect(r.total <= budget_ns); } // ── Suite ───────────────────────────────────────────────────────────────────── Void::Testing::Test bench_suite_collects_results() { Suite s; s.add(run("a", 10, [] {}, 0)); s.add(run("b", 10, [] {}, 0)); Void::Testing::expect_eq(s.size(), std::size_t{2}); Void::Testing::expect_eq(s.results()[0].name, std::string{"a"}); Void::Testing::expect_eq(s.results()[1].name, std::string{"b"}); } // ── do_not_optimize ─────────────────────────────────────────────────────────── Void::Testing::Test bench_do_not_optimize_compiles() { int x = 42; do_not_optimize(x); const int y = 7; do_not_optimize(y); Void::Testing::expect_eq(x, 42); }