#include #include #include #include #include #include #include #include namespace { struct TrackedItem { int id; std::vector* log; TrackedItem(int i, std::vector* l, bool should_throw = false) : id(i), log(l) { if (should_throw) { throw std::runtime_error{"ctor throw"}; } } ~TrackedItem() { if (log != nullptr) { log->push_back(id); } } }; } // namespace Void::Testing::Test arena_fixed_buffer_overalign_and_zero_bytes() { // Deliberately shift base pointer by 1 byte inside buffer to test true // address alignment. std::array raw{}; Void::Memory::Arena arena{std::span{raw}.subspan(1)}; auto z = arena.try_allocate(0, 1); Void::Testing::expect(z.has_value()); Void::Testing::expect_eq(arena.stats().requested_live, std::size_t{1}); auto a64 = arena.try_allocate(16, 64); Void::Testing::expect(a64.has_value()); Void::Testing::expect_eq( reinterpret_cast(*a64) % 64, std::uintptr_t{0}); const auto bad_align = arena.try_allocate(8, 3); Void::Testing::expect(!bad_align.has_value()); Void::Testing::expect(bad_align.error() == Void::Memory::AllocationError::invalid_alignment); const auto too_big = arena.try_allocate(1024, 8); Void::Testing::expect(!too_big.has_value()); Void::Testing::expect(too_big.error() == Void::Memory::AllocationError::exhausted); } Void::Testing::Test arena_grows_slabs_and_rewinds_checkpoints() { Void::Memory::Arena arena{1024, 128}; auto p1 = arena.try_allocate(96, 8); Void::Testing::expect(p1.has_value()); const auto cp1 = arena.checkpoint(); auto p2 = arena.try_allocate(128, 16); Void::Testing::expect(p2.has_value()); const auto cp2 = arena.checkpoint(); Void::Testing::expect(arena.stats().committed_live >= 256); Void::Testing::expect(arena.rewind(cp1).has_value()); Void::Testing::expect_eq(arena.stats().requested_live, std::size_t{96}); // Forward/beyond-cursor checkpoint must be rejected after rewind. const auto bad_cp2 = arena.rewind(cp2); Void::Testing::expect(!bad_cp2.has_value()); Void::Testing::expect(bad_cp2.error() == Void::Memory::AllocationError::invalid_checkpoint); // Reusing space after rewind should not blow past budget unnecessarily. auto p3 = arena.try_allocate(128, 16); Void::Testing::expect(p3.has_value()); Void::Testing::expect(arena.reset().has_value()); Void::Testing::expect_eq(arena.stats().requested_live, std::size_t{0}); // Stale generation checkpoint rejected after reset(). const auto stale = arena.rewind(cp1); Void::Testing::expect(!stale.has_value()); Void::Testing::expect(stale.error() == Void::Memory::AllocationError::invalid_checkpoint); } Void::Testing::Test arena_typed_scope_destroys_reverse_and_blocks_reset() { Void::Memory::Arena arena{4096, 512}; std::vector destroyed; { Void::Memory::Arena::TypedScope scope{arena}; Void::Testing::expect(scope.emplace(1, &destroyed).has_value()); Void::Testing::expect(scope.emplace(2, &destroyed).has_value()); bool threw = false; try { (void)scope.emplace(99, &destroyed, true); } catch (const std::runtime_error&) { threw = true; } Void::Testing::expect(threw); const auto blocked = arena.reset(); Void::Testing::expect(!blocked.has_value()); Void::Testing::expect(blocked.error() == Void::Memory::AllocationError::live_allocations); } Void::Testing::expect_eq(destroyed.size(), std::size_t{2}); Void::Testing::expect_eq(destroyed[0], 2); Void::Testing::expect_eq(destroyed[1], 1); Void::Testing::expect(arena.reset().has_value()); }