Merge branch 'issue/7-restorable-id-sequences'

This commit is contained in:
selikhov committed 2026-12-03 09:41:52 +03:00
commit 7232fa4ba5
2 files changed
+331

No files matched your search

+148
View File
@@ -0,0 +1,148 @@
#include <Void/Core/Sequence.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstdint>
#include <type_traits>
namespace
{
struct WorldTag {};
struct OtherWorldTag {};
using WorldSequence = Void::Core::IdSequence<WorldTag>;
using SmallSequence = Void::Core::IdSequence<WorldTag, std::uint8_t>;
} // namespace
Void::Testing::Test id_sequence_allocates_monotonically()
{
WorldSequence sequence;
Void::Testing::expect_eq(*sequence.next(), std::uint64_t{1});
Void::Testing::expect_eq(*sequence.next(), std::uint64_t{2});
Void::Testing::expect_eq(*sequence.next(), std::uint64_t{3});
Void::Testing::expect_eq(sequence.high_water(), std::uint64_t{3});
Void::Testing::expect_eq(sequence.peek(), std::uint64_t{4});
}
Void::Testing::Test id_sequence_instances_are_independent()
{
WorldSequence first;
WorldSequence second;
Void::Testing::expect_eq(*first.next(), std::uint64_t{1});
Void::Testing::expect_eq(*second.next(), std::uint64_t{1});
Void::Testing::expect_eq(*first.next(), std::uint64_t{2});
Void::Testing::expect_eq(*second.next(), std::uint64_t{2});
}
Void::Testing::Test id_sequence_restore_retires_erased_high_water()
{
WorldSequence sequence;
(void)sequence.next();
(void)sequence.next();
const auto erased = *sequence.next(); // record 3 is erased by the caller
Void::Testing::expect_eq(erased, std::uint64_t{3});
const auto saved = sequence.state();
auto restored = WorldSequence::restore(saved);
Void::Testing::require(restored.has_value());
// The erased id is retired, not reused.
Void::Testing::expect_eq(*restored->next(), std::uint64_t{4});
}
Void::Testing::Test id_sequence_copy_replays_from_snapshot()
{
WorldSequence original;
(void)original.next();
(void)original.next();
auto snapshot = original; // copy at a completed boundary
Void::Testing::expect_eq(*snapshot.next(), std::uint64_t{3});
Void::Testing::expect_eq(*original.next(), std::uint64_t{3});
}
Void::Testing::Test id_sequence_batch_is_atomic()
{
WorldSequence sequence;
auto batch = sequence.next_batch(3);
Void::Testing::require(batch.has_value());
Void::Testing::expect_eq(batch->first, std::uint64_t{1});
Void::Testing::expect_eq(batch->count, std::uint64_t{3});
Void::Testing::expect_eq(*sequence.next(), std::uint64_t{4});
// A batch that does not fit changes nothing.
SmallSequence small{250};
const auto before = small.state();
auto too_large = small.next_batch(10);
Void::Testing::expect(!too_large.has_value());
Void::Testing::expect(small.state() == before);
}
Void::Testing::Test id_sequence_zero_batch_is_rejected()
{
WorldSequence sequence;
auto zero = sequence.next_batch(0);
Void::Testing::require(!zero.has_value());
Void::Testing::expect(zero.error().kind ==
Void::Core::SequenceErrorKind::zero_count);
Void::Testing::expect_eq(sequence.peek(), std::uint64_t{1});
}
Void::Testing::Test id_sequence_checked_exhaustion()
{
SmallSequence sequence{254};
Void::Testing::expect_eq(static_cast<int>(*sequence.next()), 254);
Void::Testing::expect_eq(static_cast<int>(*sequence.next()), 255);
Void::Testing::expect(sequence.exhausted());
Void::Testing::expect_eq(static_cast<int>(sequence.available()), 0);
const auto before = sequence.state();
auto overflow = sequence.next();
Void::Testing::require(!overflow.has_value());
Void::Testing::expect(overflow.error().kind ==
Void::Core::SequenceErrorKind::exhausted);
Void::Testing::expect(sequence.state() == before);
Void::Testing::expect_eq(static_cast<int>(sequence.high_water()), 255);
}
Void::Testing::Test id_sequence_batch_at_final_boundary()
{
SmallSequence sequence{255};
// Only max is available: a batch of two is rejected without change.
const auto before = sequence.state();
auto two = sequence.next_batch(2);
Void::Testing::require(!two.has_value());
Void::Testing::expect(two.error().kind ==
Void::Core::SequenceErrorKind::exhausted);
Void::Testing::expect(sequence.state() == before);
// A batch of exactly the remaining one succeeds and exhausts.
auto one = sequence.next_batch(1);
Void::Testing::require(one.has_value());
Void::Testing::expect_eq(static_cast<int>(one->first), 255);
Void::Testing::expect(sequence.exhausted());
}
Void::Testing::Test id_sequence_restore_rejects_invalid_state()
{
using State = SmallSequence::State;
auto zero = SmallSequence::restore(State{.next = 0, .exhausted = false});
Void::Testing::expect(!zero.has_value());
Void::Testing::expect(zero.error().kind ==
Void::Core::SequenceErrorKind::invalid_state);
auto inconsistent =
SmallSequence::restore(State{.next = 10, .exhausted = true});
Void::Testing::expect(!inconsistent.has_value());
}
Void::Testing::Test id_sequence_state_is_fixed_width()
{
using Sequence = Void::Core::IdSequence<WorldTag, std::uint32_t>;
static_assert(
std::is_same_v<decltype(Sequence::State{}.next), std::uint32_t>);
Sequence sequence;
(void)sequence.next();
const auto state = sequence.state();
Void::Testing::expect_eq(state.next, std::uint32_t{2});
}