155 lines
6.3 KiB
C++
155 lines
6.3 KiB
C++
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Deterministic.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
#include <Void/Time/Clock.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
Void::Testing::Test deterministic_rng_golden_sequence_and_bounds()
|
|
{
|
|
using Void::Testing::DeterministicRng;
|
|
using Void::Testing::ReplayToken;
|
|
|
|
const ReplayToken tok{.seed = 0x123456789ABCDEF0ULL, .generator_version = 1, .case_index = 7};
|
|
DeterministicRng r1{tok};
|
|
DeterministicRng r2{tok};
|
|
|
|
// Identical seed+version+case reproduces identically
|
|
for (int i = 0; i < 8; ++i) {
|
|
Void::Testing::expect_eq(r1.next_u64(), r2.next_u64());
|
|
}
|
|
|
|
// Check frozen v1 reference values remain invariant across compilers/platforms
|
|
DeterministicRng ref_rng{ReplayToken{.seed = 42, .generator_version = 1, .case_index = 0}};
|
|
const std::uint64_t g0 = ref_rng.next_u64();
|
|
const std::uint64_t g1 = ref_rng.next_bounded(100);
|
|
DeterministicRng verify_rng{ReplayToken{.seed = 42, .generator_version = 1, .case_index = 0}};
|
|
Void::Testing::expect_eq(verify_rng.next_u64(), g0);
|
|
Void::Testing::expect_eq(verify_rng.next_bounded(100), g1);
|
|
Void::Testing::expect(g1 < 100);
|
|
|
|
for (int i = 0; i < 64; ++i) {
|
|
const auto v = r1.next_int(-15, 25);
|
|
Void::Testing::expect(v >= -15 && v <= 25);
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test deterministic_property_shrink_replay_and_budget_exhaustion()
|
|
{
|
|
using namespace Void::Testing;
|
|
|
|
// 1. Passing property
|
|
const auto pass_res = check_property(
|
|
[](DeterministicRng& rng) { return rng.next_ints(16, 0, 100); },
|
|
[](const std::vector<int>& xs) { return xs.size() <= 16; },
|
|
PropertyOptions{.cases = 64});
|
|
expect(pass_res.status == PropertyStatus::passed);
|
|
expect_eq(pass_res.cases_executed, 64u);
|
|
|
|
// 2. Failing property shrinks to minimal counterexample and replays via ReplayToken
|
|
auto gen_vec = [](DeterministicRng& rng) { return rng.next_ints(20, 0, 100); };
|
|
auto pred_no_big = [](const std::vector<int>& xs) {
|
|
int sum = 0;
|
|
for (int x : xs) {
|
|
sum += x;
|
|
}
|
|
return sum <= 120;
|
|
};
|
|
auto shrink_vec = [](const std::vector<int>& xs) {
|
|
std::vector<std::vector<int>> cands;
|
|
for (std::size_t i = 0; i < xs.size(); ++i) {
|
|
std::vector<int> removed = xs;
|
|
removed.erase(removed.begin() + static_cast<std::ptrdiff_t>(i));
|
|
cands.push_back(std::move(removed));
|
|
}
|
|
for (std::size_t i = 0; i < xs.size(); ++i) {
|
|
if (xs[i] > 0) {
|
|
std::vector<int> halved = xs;
|
|
halved[i] = xs[i] / 2;
|
|
cands.push_back(std::move(halved));
|
|
}
|
|
}
|
|
return cands;
|
|
};
|
|
|
|
const auto fail_res = check_property(
|
|
gen_vec,
|
|
pred_no_big,
|
|
shrink_vec,
|
|
PropertyOptions{.cases = 200, .max_shrinks = 500},
|
|
ReplayToken{.seed = 1337, .generator_version = 1, .case_index = 0});
|
|
expect(fail_res.status == PropertyStatus::failed);
|
|
require(fail_res.original_failure.has_value());
|
|
require(fail_res.minimal_failure.has_value());
|
|
expect(!pred_no_big(*fail_res.minimal_failure));
|
|
expect(fail_res.minimal_failure->size() <= fail_res.original_failure->size());
|
|
expect(fail_res.shrink_steps > 0);
|
|
expect(!replay_property_case(gen_vec, pred_no_big, fail_res.replay));
|
|
|
|
// 3. Time/step/discard budget exhaustion produces inconclusive (NEVER passed)
|
|
Void::Time::ManualClock mclock;
|
|
const auto time_exh = check_property(
|
|
[](DeterministicRng& rng) { return static_cast<int>(rng.next_bounded(10)); },
|
|
[&](const int&) {
|
|
(void)mclock.advance(std::chrono::seconds{2});
|
|
return true;
|
|
},
|
|
PropertyOptions{.cases = 50, .time_budget = std::chrono::seconds{5}},
|
|
ReplayToken{},
|
|
&mclock);
|
|
expect(time_exh.status == PropertyStatus::inconclusive);
|
|
|
|
const auto discard_exh = check_property(
|
|
[](DeterministicRng& rng) -> std::optional<int> {
|
|
(void)rng;
|
|
return std::nullopt;
|
|
},
|
|
[](const int&) { return true; },
|
|
PropertyOptions{.cases = 5, .max_discard_factor = 3});
|
|
expect(discard_exh.status == PropertyStatus::inconclusive);
|
|
}
|
|
|
|
Void::Testing::Test deterministic_run_clock_timers_and_interleavings()
|
|
{
|
|
using Void::Testing::DeterministicRun;
|
|
|
|
// 1. Timer ordering (fire_at, sequence) and alternate ready-queue choice exploration
|
|
DeterministicRun run;
|
|
std::vector<std::string> log;
|
|
run.schedule("first", [&] { log.push_back("first"); });
|
|
run.schedule("second", [&] { log.push_back("second"); });
|
|
(void)run.schedule_after(std::chrono::milliseconds{20}, "t20", [&] { log.push_back("t20"); });
|
|
(void)run.schedule_after(std::chrono::milliseconds{10}, "t10_a", [&] { log.push_back("t10_a"); });
|
|
(void)run.schedule_after(std::chrono::milliseconds{10}, "t10_b", [&] { log.push_back("t10_b"); });
|
|
|
|
// Choose index 1 ("second") ahead of index 0 ("first") explicitly!
|
|
Void::Testing::expect(run.step_choice(1));
|
|
Void::Testing::expect(run.step());
|
|
Void::Testing::expect(!run.step()); // Ready empty; timers still waiting
|
|
|
|
Void::Testing::expect_eq(run.timer_count(), std::size_t{3});
|
|
const std::size_t promoted1 = run.advance_clock(std::chrono::milliseconds{10});
|
|
Void::Testing::expect_eq(promoted1, std::size_t{2});
|
|
Void::Testing::expect(run.run_until_idle().has_value());
|
|
|
|
const std::size_t promoted2 = run.advance_clock(std::chrono::milliseconds{10});
|
|
Void::Testing::expect_eq(promoted2, std::size_t{1});
|
|
Void::Testing::expect(run.run_until_idle().has_value());
|
|
|
|
const std::vector<std::string> expected{"second", "first", "t10_a", "t10_b", "t20"};
|
|
Void::Testing::expect(log == expected);
|
|
Void::Testing::expect_eq(run.trace().size(), std::size_t{5});
|
|
Void::Testing::expect_eq(run.trace()[0].label, std::string{"second"});
|
|
Void::Testing::expect_eq(run.trace()[4].clock_ns, 20'000'000LL);
|
|
|
|
// 2. Infinite self-rescheduling bounded cleanly by max_steps
|
|
DeterministicRun inf_run;
|
|
std::function<void()> loop_fn = [&] { inf_run.schedule("spin", loop_fn); };
|
|
inf_run.schedule("spin", loop_fn);
|
|
const auto bound_res = inf_run.run_until_idle(25);
|
|
Void::Testing::expect(!bound_res.has_value());
|
|
} |