A round of new universal capabilities (all generic — templated/reflection based, no app coupling), verified by the test suite (1103 passing): Data reflection: - Reflect.hpp now (de)serializes chrono::duration, chrono::time_point, std::variant (tagged), std::array, and non-string-keyed std::map; Meta.hpp gains is_time_point_v et al. Async: - EventBus gains Mode::Async (worker-thread dispatch + flush()). - ThreadPool gains a bounded queue, try_submit(), and rejection stats. New modules / containers: - Resilience: TokenBucket, KeyedRateLimiter<Key>, CircuitBreaker, ResourceMonitor. - Containers: BufferPool<T> (size-class buffer reuse) and Cache<K,V> (per-entry TTL, LRU/LFU/FIFO eviction, hit/miss stats). - System: process/CPU/memory metrics (Metrics.hpp) and a streaming Process API. Plus FSM v2 (typed Success/Failure, sub-machines, parallel regions, history states, deferred events), Configuration/Logging reflection helpers, and the accompanying test suites. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
87 lines
2.5 KiB
C++
87 lines
2.5 KiB
C++
#include <Void/Containers/BufferPool.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Suite.hpp>
|
|
|
|
#include <cstdint>
|
|
|
|
namespace
|
|
{
|
|
using Void::BufferPool::BufferPool;
|
|
using Void::Testing::Test;
|
|
|
|
struct BufferPoolTests : Void::Testing::Suite<BufferPoolTests> {
|
|
// Logical size is the request; capacity rounds up to a power of
|
|
// two.
|
|
Test acquire_rounds_capacity_to_size_class()
|
|
{
|
|
BufferPool<std::int16_t> 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<std::int16_t> 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<float> 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<int> pool;
|
|
auto buf = pool.acquire(4);
|
|
for (std::size_t i = 0; i < buf.size(); ++i) {
|
|
buf[i] = static_cast<int>(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<int> 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
|