Add Observable, Pool, Ring modules

Void::Observable — Observable<T> reactive value with typed on_change listeners
and unsubscribe-by-handle; Signal<Args...> event broadcast with connect/disconnect.

Void::Pool — ObjectPool<T,ChunkSize> slab-based pool with unique_ptr<T,Deleter>
ownership; FixedPool<T,N> fixed-capacity in-place pool, returns nullptr when full.

Void::Ring — SpscRing<T,N> single-producer/single-consumer wait-free ring buffer;
MpscRing<T,N> multi-producer/single-consumer ring using CAS on the head pointer.
Both tested under real threads.

39/39 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 4.6 committed 2026-05-14 22:54:35 +03:00
1 parent a9a2e2b59a
commit 388e75011f
6 files changed
+876

No files matched your search

+143
View File
@@ -0,0 +1,143 @@
#include <Void/Observable/Observable.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <vector>
// ── Observable<T> ─────────────────────────────────────────────────────────────
Void::Testing::Test observable_initial_value()
{
Void::Observable::Observable<int> obs{42};
Void::Testing::expect_eq(obs.get(), 42);
}
Void::Testing::Test observable_set_changes_value()
{
Void::Observable::Observable<int> obs{0};
obs.set(7);
Void::Testing::expect_eq(obs.get(), 7);
}
Void::Testing::Test observable_listener_called_on_change()
{
Void::Observable::Observable<int> obs{0};
int seen_new = -1;
obs.on_change([&](int, int n) { seen_new = n; });
obs.set(5);
Void::Testing::expect_eq(seen_new, 5);
}
Void::Testing::Test observable_listener_receives_old_and_new()
{
Void::Observable::Observable<int> obs{10};
int old_val = -1, new_val = -1;
obs.on_change([&](int o, int n) { old_val = o; new_val = n; });
obs.set(20);
Void::Testing::expect_eq(old_val, 10);
Void::Testing::expect_eq(new_val, 20);
}
Void::Testing::Test observable_no_notification_on_same_value()
{
Void::Observable::Observable<int> obs{5};
int call_count = 0;
obs.on_change([&](int, int) { ++call_count; });
obs.set(5);
Void::Testing::expect_eq(call_count, 0);
}
Void::Testing::Test observable_multiple_listeners()
{
Void::Observable::Observable<int> obs{0};
int a = 0, b = 0;
obs.on_change([&](int, int n) { a = n; });
obs.on_change([&](int, int n) { b = n * 2; });
obs.set(3);
Void::Testing::expect_eq(a, 3);
Void::Testing::expect_eq(b, 6);
}
Void::Testing::Test observable_unsubscribe_stops_notifications()
{
Void::Observable::Observable<int> obs{0};
int count = 0;
auto h = obs.on_change([&](int, int) { ++count; });
obs.set(1);
obs.unsubscribe(h);
obs.set(2);
Void::Testing::expect_eq(count, 1);
}
Void::Testing::Test observable_unsubscribe_returns_false_for_unknown_handle()
{
Void::Observable::Observable<int> obs{0};
Void::Testing::expect(!obs.unsubscribe(Void::Observable::Handle{999}));
}
Void::Testing::Test observable_assign_operator()
{
Void::Observable::Observable<std::string> obs{"hello"};
int count = 0;
obs.on_change([&](const std::string&, const std::string&) { ++count; });
obs = std::string{"world"};
Void::Testing::expect_eq(obs.get(), std::string{"world"});
Void::Testing::expect_eq(count, 1);
}
// ── Signal<Args...> ───────────────────────────────────────────────────────────
Void::Testing::Test signal_emit_calls_slot()
{
Void::Observable::Signal<int> sig;
int received = -1;
sig.connect([&](const int& v) { received = v; });
sig.emit(42);
Void::Testing::expect_eq(received, 42);
}
Void::Testing::Test signal_multiple_slots()
{
Void::Observable::Signal<int> sig;
std::vector<int> vals;
sig.connect([&](const int& v) { vals.push_back(v); });
sig.connect([&](const int& v) { vals.push_back(v * 10); });
sig.emit(3);
Void::Testing::expect_eq(vals.size(), std::size_t{2});
Void::Testing::expect_eq(vals[0], 3);
Void::Testing::expect_eq(vals[1], 30);
}
Void::Testing::Test signal_disconnect_stops_slot()
{
Void::Observable::Signal<int> sig;
int count = 0;
auto h = sig.connect([&](const int&) { ++count; });
sig.emit(1);
sig.disconnect(h);
sig.emit(2);
Void::Testing::expect_eq(count, 1);
}
Void::Testing::Test signal_clear_removes_all_slots()
{
Void::Observable::Signal<> sig;
int count = 0;
sig.connect([&] { ++count; });
sig.connect([&] { ++count; });
sig.clear();
sig.emit();
Void::Testing::expect_eq(count, 0);
}
Void::Testing::Test signal_multi_arg_emit()
{
Void::Observable::Signal<int, std::string> sig;
int n = 0;
std::string s;
sig.connect([&](const int& a, const std::string& b) { n = a; s = b; });
sig.emit(7, std::string{"hello"});
Void::Testing::expect_eq(n, 7);
Void::Testing::expect_eq(s, std::string{"hello"});
}
+103
View File
@@ -0,0 +1,103 @@
#include <Void/Pool/Pool.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
// ── ObjectPool ────────────────────────────────────────────────────────────────
Void::Testing::Test object_pool_acquire_returns_valid_ptr()
{
Void::Pool::ObjectPool<int> pool;
auto p = pool.acquire(42);
Void::Testing::expect(p != nullptr);
Void::Testing::expect_eq(*p, 42);
}
Void::Testing::Test object_pool_release_on_ptr_destruction()
{
Void::Pool::ObjectPool<int, 4> pool;
Void::Testing::expect_eq(pool.available(), std::size_t{0});
{
auto p = pool.acquire(1);
// capacity grew to 4, 3 slots free
Void::Testing::expect_eq(pool.available(), std::size_t{3});
}
// Released back to pool
Void::Testing::expect_eq(pool.available(), std::size_t{4});
}
Void::Testing::Test object_pool_reuses_memory()
{
Void::Pool::ObjectPool<int, 4> pool;
int* raw1 = nullptr;
{
auto p = pool.acquire(1);
raw1 = p.get();
}
auto p2 = pool.acquire(2);
// After release the same slot should be reused (LIFO from free list)
Void::Testing::expect(p2.get() == raw1);
}
Void::Testing::Test object_pool_string_construction()
{
Void::Pool::ObjectPool<std::string> pool;
auto p = pool.acquire("hello");
Void::Testing::expect_eq(*p, std::string{"hello"});
}
Void::Testing::Test object_pool_capacity_grows()
{
Void::Pool::ObjectPool<int, 4> pool;
Void::Testing::expect_eq(pool.capacity(), std::size_t{0});
auto p = pool.acquire(1);
Void::Testing::expect_eq(pool.capacity(), std::size_t{4});
}
// ── FixedPool ─────────────────────────────────────────────────────────────────
Void::Testing::Test fixed_pool_capacity()
{
Void::Pool::FixedPool<int, 8> pool;
static_assert(pool.capacity() == 8);
Void::Testing::expect_eq(pool.available(), std::size_t{8});
}
Void::Testing::Test fixed_pool_acquire_decrements_available()
{
Void::Pool::FixedPool<int, 4> pool;
auto p = pool.acquire(99);
Void::Testing::expect_eq(pool.available(), std::size_t{3});
Void::Testing::expect_eq(*p, 99);
}
Void::Testing::Test fixed_pool_returns_null_when_full()
{
Void::Pool::FixedPool<int, 2> pool;
auto p1 = pool.acquire(1);
auto p2 = pool.acquire(2);
auto p3 = pool.acquire(3); // should be nullptr
Void::Testing::expect(p3 == nullptr);
}
Void::Testing::Test fixed_pool_release_restores_available()
{
Void::Pool::FixedPool<int, 4> pool;
{
auto p = pool.acquire(1);
Void::Testing::expect_eq(pool.available(), std::size_t{3});
}
Void::Testing::expect_eq(pool.available(), std::size_t{4});
}
Void::Testing::Test fixed_pool_reuse_after_release()
{
Void::Pool::FixedPool<int, 4> pool;
auto p1 = pool.acquire(1);
auto p2 = pool.acquire(2);
p1.reset();
auto p3 = pool.acquire(3);
Void::Testing::expect(p3 != nullptr);
Void::Testing::expect_eq(*p3, 3);
}
+146
View File
@@ -0,0 +1,146 @@
#include <Void/Ring/Ring.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <thread>
#include <vector>
// ── SpscRing ──────────────────────────────────────────────────────────────────
Void::Testing::Test spsc_empty_on_construction()
{
Void::Ring::SpscRing<int, 8> ring;
Void::Testing::expect(ring.empty());
}
Void::Testing::Test spsc_push_pop_single()
{
Void::Ring::SpscRing<int, 8> ring;
Void::Testing::expect(ring.push(42));
const auto v = ring.pop();
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, 42);
}
Void::Testing::Test spsc_pop_empty_returns_nullopt()
{
Void::Ring::SpscRing<int, 8> ring;
Void::Testing::expect(!ring.pop().has_value());
}
Void::Testing::Test spsc_full_push_returns_false()
{
Void::Ring::SpscRing<int, 4> ring; // capacity = 3
Void::Testing::expect(ring.push(1));
Void::Testing::expect(ring.push(2));
Void::Testing::expect(ring.push(3));
Void::Testing::expect(!ring.push(4)); // full
}
Void::Testing::Test spsc_fifo_order()
{
Void::Ring::SpscRing<int, 8> ring;
for (int i = 0; i < 5; ++i) ring.push(i);
for (int i = 0; i < 5; ++i) {
const auto v = ring.pop();
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, i);
}
}
Void::Testing::Test spsc_capacity_is_n_minus_one()
{
Void::Testing::expect_eq(Void::Ring::SpscRing<int, 16>::capacity(),
std::size_t{15});
}
Void::Testing::Test spsc_full_flag()
{
Void::Ring::SpscRing<int, 4> ring;
ring.push(1); ring.push(2); ring.push(3);
Void::Testing::expect(ring.full());
}
Void::Testing::Test spsc_string_values()
{
Void::Ring::SpscRing<std::string, 4> ring;
ring.push(std::string{"hello"});
const auto v = ring.pop();
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, std::string{"hello"});
}
// @tag threading
Void::Testing::Test spsc_producer_consumer_threads()
{
Void::Ring::SpscRing<int, 64> ring;
constexpr int N = 50;
std::vector<int> received;
received.reserve(N);
std::thread producer([&] {
for (int i = 0; i < N; ++i) {
while (!ring.push(i)) {}
}
});
std::thread consumer([&] {
int got = 0;
while (got < N) {
auto v = ring.pop();
if (v) { received.push_back(*v); ++got; }
}
});
producer.join();
consumer.join();
Void::Testing::expect_eq(received.size(), std::size_t{N});
for (int i = 0; i < N; ++i) {
Void::Testing::expect_eq(received[i], i);
}
}
// ── MpscRing ──────────────────────────────────────────────────────────────────
Void::Testing::Test mpsc_push_pop_single()
{
Void::Ring::MpscRing<int, 8> ring;
Void::Testing::expect(ring.push(99));
const auto v = ring.pop();
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, 99);
}
Void::Testing::Test mpsc_empty_on_construction()
{
Void::Ring::MpscRing<int, 8> ring;
Void::Testing::expect(ring.empty());
}
// @tag threading
Void::Testing::Test mpsc_two_producers_one_consumer()
{
Void::Ring::MpscRing<int, 128> ring;
constexpr int N = 40;
std::atomic<int> total{0};
std::thread p1([&] {
for (int i = 0; i < N; ++i) while (!ring.push(1)) {}
});
std::thread p2([&] {
for (int i = 0; i < N; ++i) while (!ring.push(2)) {}
});
std::thread consumer([&] {
int got = 0;
while (got < N * 2) {
auto v = ring.pop();
if (v) { total.fetch_add(*v, std::memory_order_relaxed); ++got; }
}
});
p1.join(); p2.join(); consumer.join();
Void::Testing::expect_eq(total.load(), N * 3); // N×1 + N×2 = 3N
}