#include #include #include #include #include #include #include namespace { using Void::BufferPool::BufferPool; using Void::Testing::Test; struct BufferPoolTests : Void::Testing::Suite { // Logical size is the request; capacity rounds up to a power of // two. Test acquire_rounds_capacity_to_size_class() { BufferPool pool; auto buf = pool.acquire(500); Void::Testing::expect_eq(buf.size(), std::size_t{500}); Void::Testing::expect_eq(buf.capacity(), std::size_t{512}); } // A fresh request allocates; a same-class request after drop // reuses. Test reuses_storage_of_same_size_class() { BufferPool pool; std::int16_t* first = nullptr; { auto buf = pool.acquire(500); first = buf.data(); } // returned to the pool here Void::Testing::expect_eq(pool.stats().allocations, std::uint64_t{1}); Void::Testing::expect_eq(pool.stats().pooled, std::size_t{1}); auto buf2 = pool.acquire(300); // also class 512 Void::Testing::expect_eq(pool.stats().reuses, std::uint64_t{1}); Void::Testing::expect_eq(pool.stats().allocations, std::uint64_t{1}); // Same backing storage -> no reallocation. Void::Testing::expect(buf2.data() == first); Void::Testing::expect_eq(buf2.capacity(), std::size_t{512}); } Test distinct_size_classes_use_distinct_buckets() { BufferPool pool; { auto a = pool.acquire(500); // class 512 auto b = pool.acquire(1000); // class 1024 Void::Testing::expect_eq(a.capacity(), std::size_t{512}); Void::Testing::expect_eq(b.capacity(), std::size_t{1024}); } Void::Testing::expect_eq(pool.stats().allocations, std::uint64_t{2}); Void::Testing::expect_eq(pool.stats().pooled, std::size_t{2}); } Test buffer_is_writable_and_iterable() { BufferPool pool; auto buf = pool.acquire(4); for (std::size_t i = 0; i < buf.size(); ++i) { buf[i] = static_cast(i * i); } int sum = 0; for (int v : buf) { sum += v; } Void::Testing::expect_eq(sum, 0 + 1 + 4 + 9); Void::Testing::expect_eq(buf.span().size(), std::size_t{4}); } Test shrink_drops_pooled_storage() { BufferPool pool; { auto a = pool.acquire(10); } Void::Testing::expect_eq(pool.stats().pooled, std::size_t{1}); pool.shrink(); Void::Testing::expect_eq(pool.stats().pooled, std::size_t{0}); } Test leases_survive_pool_move_and_destruction() { Void::Memory::BudgetResource budget{65536}; BufferPool::Buffer retained; { BufferPool p1{&budget}; auto b = p1.try_acquire(64); Void::Testing::require(b.has_value()); (*b)[0] = 0xCAFEBABEu; auto p2 = std::move(p1); retained = std::move(*b); Void::Testing::expect_eq(p2.stats().allocations, std::uint64_t{1}); } // Pool destroyed; retained buffer still intact and frees cleanly on drop. Void::Testing::expect_eq(retained[0], 0xCAFEBABEu); Void::Testing::expect(budget.stats().committed_live > 0); retained = BufferPool::Buffer{}; Void::Testing::expect_eq(budget.stats().committed_live, std::size_t{0}); } Test enforces_request_idle_total_limits_and_overflow() { using Void::BufferPool::BufferPoolError; BufferPool small_pool{ BufferPool::Limits{ .max_request_bytes = 256, .max_idle_bytes = 256, .max_total_bytes = 512}}; const auto ovf = small_pool.try_acquire( std::numeric_limits::max()); Void::Testing::expect( !ovf.has_value() && ovf.error() == BufferPoolError::size_overflow); const auto too_big = small_pool.try_acquire(64); // 512B > 256B Void::Testing::expect( !too_big.has_value() && too_big.error() == BufferPoolError::request_too_large); auto a = small_pool.try_acquire(32); // 256B auto b = small_pool.try_acquire(32); // 256B (total 512B) auto c = small_pool.try_acquire(32); // exceeds 512B Void::Testing::require(a.has_value() && b.has_value()); Void::Testing::expect( !c.has_value() && c.error() == BufferPoolError::budget_exceeded); a = {}; // fits in max_idle_bytes=256B b = {}; // exceeds max_idle_bytes=256B -> dropped_on_return Void::Testing::expect_eq(small_pool.stats().pooled, std::size_t{1}); Void::Testing::expect_eq(small_pool.stats().idle_bytes, std::size_t{256}); Void::Testing::expect_eq(small_pool.stats().dropped_on_return, std::uint64_t{1}); } Test budget_resource_zero_allocs_on_reuse_and_handles_oom() { using Void::BufferPool::BufferPoolError; Void::Memory::BudgetResource budget{1024}; BufferPool pool{&budget}; { auto b = pool.try_acquire(256); Void::Testing::require(b.has_value()); } const auto allocs = budget.stats().allocations; Void::Testing::expect_eq(allocs, std::uint64_t{1}); { auto b2 = pool.try_acquire(200); // same class 256 Void::Testing::require(b2.has_value()); Void::Testing::expect_eq(budget.stats().allocations, allocs); Void::Testing::expect_eq(pool.stats().reuses, std::uint64_t{1}); } const auto fail_alloc = pool.try_acquire(2048); Void::Testing::expect( !fail_alloc.has_value() && fail_alloc.error() == BufferPoolError::allocation_failed); } }; } // namespace