Merge branch 'issue/10-versioned-rng-state'
This commit is contained in:
commit
52c23836b7
2 files changed
+223
No files matched your search
@@ -146,3 +146,136 @@ Void::Testing::Test sample_indices_in_range()
|
||||
Void::Testing::expect(i < std::size_t{10});
|
||||
}
|
||||
}
|
||||
|
||||
// ── Xoshiro256 versioned state and bounded draws ─────────────────────────────
|
||||
|
||||
Void::Testing::Test xoshiro_frozen_outputs_are_portable()
|
||||
{
|
||||
// These values are part of the algorithm identity: any change means a
|
||||
// new generator version, not a silent reinterpretation of saved worlds.
|
||||
Void::Random::Xoshiro256 raw{42};
|
||||
Void::Testing::expect_eq(raw(), std::uint64_t{1546998764402558742ULL});
|
||||
Void::Testing::expect_eq(raw(), std::uint64_t{6990951692964543102ULL});
|
||||
Void::Testing::expect_eq(raw(),
|
||||
std::uint64_t{12544586762248559009ULL});
|
||||
Void::Testing::expect_eq(raw(),
|
||||
std::uint64_t{17057574109182124193ULL});
|
||||
|
||||
Void::Random::Xoshiro256 bounded{42};
|
||||
Void::Testing::expect_eq(bounded.bounded(1000), std::uint64_t{742});
|
||||
Void::Testing::expect_eq(bounded.bounded(1000), std::uint64_t{102});
|
||||
Void::Testing::expect_eq(bounded.bounded(1000), std::uint64_t{9});
|
||||
Void::Testing::expect_eq(bounded.bounded(1000), std::uint64_t{193});
|
||||
|
||||
Void::Random::Xoshiro256 stateful{42};
|
||||
(void)stateful();
|
||||
(void)stateful();
|
||||
(void)stateful();
|
||||
const auto state = stateful.state();
|
||||
Void::Testing::expect_eq(state.version, std::uint32_t{1});
|
||||
Void::Testing::expect_eq(state.words[0],
|
||||
std::uint64_t{14724789073754520473ULL});
|
||||
Void::Testing::expect_eq(state.words[1],
|
||||
std::uint64_t{2590629650289322887ULL});
|
||||
Void::Testing::expect_eq(state.words[2],
|
||||
std::uint64_t{7959817307922065030ULL});
|
||||
Void::Testing::expect_eq(state.words[3],
|
||||
std::uint64_t{9375168587437865237ULL});
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_state_round_trip_resumes_suffix()
|
||||
{
|
||||
Void::Random::Xoshiro256 original{42};
|
||||
(void)original();
|
||||
(void)original.bounded(1000);
|
||||
(void)original();
|
||||
(void)original.bounded(7);
|
||||
|
||||
const auto saved = original.state();
|
||||
auto restored = Void::Random::Xoshiro256::from_state(saved);
|
||||
Void::Testing::require(restored.has_value());
|
||||
Void::Testing::expect(restored->state() == saved);
|
||||
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
Void::Testing::expect_eq((*restored)(), original());
|
||||
Void::Testing::expect_eq(restored->bounded(999),
|
||||
original.bounded(999));
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_state_rejects_invalid_without_changing_stream()
|
||||
{
|
||||
Void::Random::Xoshiro256 rng{5};
|
||||
(void)rng();
|
||||
const auto before = rng.state();
|
||||
|
||||
Void::Random::Xoshiro256::State zero{.version = 1, .words = {}};
|
||||
auto zero_result = rng.restore(zero);
|
||||
Void::Testing::require(!zero_result.has_value());
|
||||
Void::Testing::expect(zero_result.error().kind ==
|
||||
Void::Random::Xoshiro256::StateErrorKind::
|
||||
invalid_state);
|
||||
Void::Testing::expect(rng.state() == before);
|
||||
|
||||
Void::Random::Xoshiro256::State unknown{.version = 99,
|
||||
.words = before.words};
|
||||
auto unknown_result = rng.restore(unknown);
|
||||
Void::Testing::require(!unknown_result.has_value());
|
||||
Void::Testing::expect(unknown_result.error().kind ==
|
||||
Void::Random::Xoshiro256::StateErrorKind::
|
||||
unknown_version);
|
||||
Void::Testing::expect(rng.state() == before);
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_bounded_zero_and_one_do_not_consume()
|
||||
{
|
||||
Void::Random::Xoshiro256 rng{7};
|
||||
const auto before = rng.state();
|
||||
Void::Testing::expect_eq(rng.bounded(0), std::uint64_t{0});
|
||||
Void::Testing::expect_eq(rng.bounded(1), std::uint64_t{0});
|
||||
Void::Testing::expect(rng.state() == before);
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_bounded_is_in_range()
|
||||
{
|
||||
const std::uint64_t bounds[] = {2, 3, 1000,
|
||||
std::uint64_t{0x8000000000000001ULL},
|
||||
std::numeric_limits<std::uint64_t>::max()};
|
||||
Void::Random::Xoshiro256 rng{11};
|
||||
for (const auto bound : bounds) {
|
||||
for (int i = 0; i < 200; ++i) {
|
||||
const auto value = rng.bounded(bound);
|
||||
Void::Testing::expect(value < bound);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_interleaved_streams_are_independent()
|
||||
{
|
||||
Void::Random::Xoshiro256 interleaved_a{1};
|
||||
Void::Random::Xoshiro256 interleaved_b{2};
|
||||
Void::Random::Xoshiro256 sequential_a{1};
|
||||
Void::Random::Xoshiro256 sequential_b{2};
|
||||
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
Void::Testing::expect_eq(interleaved_a(), sequential_a());
|
||||
Void::Testing::expect_eq(interleaved_b(), sequential_b());
|
||||
Void::Testing::expect_eq(interleaved_a.bounded(100),
|
||||
sequential_a.bounded(100));
|
||||
Void::Testing::expect_eq(interleaved_b.bounded(5),
|
||||
sequential_b.bounded(5));
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_discard_matches_draws()
|
||||
{
|
||||
Void::Random::Xoshiro256 discarded{9};
|
||||
discarded.discard(5);
|
||||
Void::Random::Xoshiro256 drawn{9};
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
(void)drawn();
|
||||
}
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
Void::Testing::expect_eq(discarded(), drawn());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user