#include #include #include #include #include namespace { struct WorldTag {}; struct OtherWorldTag {}; using WorldSequence = Void::Core::IdSequence; using SmallSequence = Void::Core::IdSequence; } // 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(*sequence.next()), 254); Void::Testing::expect_eq(static_cast(*sequence.next()), 255); Void::Testing::expect(sequence.exhausted()); Void::Testing::expect_eq(static_cast(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(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(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; static_assert( std::is_same_v); Sequence sequence; (void)sequence.next(); const auto state = sequence.state(); Void::Testing::expect_eq(state.next, std::uint32_t{2}); }