#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}); } }; } // namespace