Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -0,0 +1,142 @@
|
||||
#include <Void/Async/Channel.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using Void::Concurrency::Channel;
|
||||
|
||||
Void::Testing::Test capacity_is_preserved()
|
||||
{
|
||||
Channel<int> ch{8};
|
||||
Void::Testing::expect_eq(ch.capacity(), std::size_t{8});
|
||||
}
|
||||
|
||||
Void::Testing::Test try_send_and_try_receive_roundtrip()
|
||||
{
|
||||
Channel<int> ch{4};
|
||||
Void::Testing::expect(ch.try_send(10));
|
||||
Void::Testing::expect(ch.try_send(20));
|
||||
Void::Testing::expect_eq(ch.size(), std::size_t{2});
|
||||
auto v1 = ch.try_receive();
|
||||
Void::Testing::expect(v1.has_value());
|
||||
Void::Testing::expect_eq(*v1, 10);
|
||||
auto v2 = ch.try_receive();
|
||||
Void::Testing::expect(v2.has_value());
|
||||
Void::Testing::expect_eq(*v2, 20);
|
||||
auto empty = ch.try_receive();
|
||||
Void::Testing::expect(!empty.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test try_send_fails_when_full()
|
||||
{
|
||||
Channel<int> ch{2};
|
||||
Void::Testing::expect(ch.try_send(1));
|
||||
Void::Testing::expect(ch.try_send(2));
|
||||
Void::Testing::expect(!ch.try_send(3));
|
||||
}
|
||||
|
||||
Void::Testing::Test close_unblocks_receiver()
|
||||
{
|
||||
Channel<int> ch{4};
|
||||
std::thread producer{[&] { ch.close(); }};
|
||||
auto val = ch.receive();
|
||||
producer.join();
|
||||
Void::Testing::expect(!val.has_value());
|
||||
Void::Testing::expect(ch.is_closed());
|
||||
}
|
||||
|
||||
Void::Testing::Test producer_consumer_ordering()
|
||||
{
|
||||
Channel<int> ch{16};
|
||||
constexpr int N = 10;
|
||||
std::thread producer{[&] {
|
||||
for (int i = 0; i < N; ++i) {
|
||||
ch.send(i);
|
||||
}
|
||||
ch.close();
|
||||
}};
|
||||
std::vector<int> received;
|
||||
while (auto v = ch.receive()) {
|
||||
received.push_back(*v);
|
||||
}
|
||||
producer.join();
|
||||
Void::Testing::expect_eq(static_cast<int>(received.size()), N);
|
||||
for (int i = 0; i < N; ++i) {
|
||||
Void::Testing::expect_eq(received[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test try_send_fails_after_close()
|
||||
{
|
||||
Channel<int> ch{4};
|
||||
ch.close();
|
||||
Void::Testing::expect(!ch.try_send(1));
|
||||
}
|
||||
|
||||
Void::Testing::Test send_returns_false_when_closed()
|
||||
{
|
||||
Channel<int> ch{1};
|
||||
ch.close();
|
||||
bool ok = ch.send(42);
|
||||
Void::Testing::expect(!ok);
|
||||
}
|
||||
|
||||
Void::Testing::Test size_and_empty()
|
||||
{
|
||||
Channel<int> ch{4};
|
||||
Void::Testing::expect(ch.empty());
|
||||
Void::Testing::expect_eq(ch.size(), std::size_t{0});
|
||||
ch.try_send(1);
|
||||
ch.try_send(2);
|
||||
Void::Testing::expect_eq(ch.size(), std::size_t{2});
|
||||
Void::Testing::expect(!ch.empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test receive_drains_then_nullopt_after_close()
|
||||
{
|
||||
Channel<int> ch{4};
|
||||
ch.try_send(10);
|
||||
ch.try_send(20);
|
||||
ch.close();
|
||||
Void::Testing::expect_eq(*ch.try_receive(), 10);
|
||||
Void::Testing::expect_eq(*ch.try_receive(), 20);
|
||||
Void::Testing::expect(!ch.try_receive().has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test multiple_producers_sum()
|
||||
{
|
||||
Channel<int> ch{64};
|
||||
std::atomic<int> total{0};
|
||||
|
||||
auto producer = [&](int n) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
ch.send(i);
|
||||
}
|
||||
};
|
||||
|
||||
std::thread t1(producer, 5);
|
||||
std::thread t2(producer, 5);
|
||||
t1.join();
|
||||
t2.join();
|
||||
ch.close();
|
||||
|
||||
while (auto v = ch.receive()) {
|
||||
total += *v;
|
||||
}
|
||||
|
||||
// Sum of 0+1+2+3+4 = 10, two producers → 20
|
||||
Void::Testing::expect_eq(total.load(), 20);
|
||||
}
|
||||
|
||||
Void::Testing::Test string_values_roundtrip()
|
||||
{
|
||||
Channel<std::string> ch{4};
|
||||
ch.try_send("hello");
|
||||
ch.try_send("world");
|
||||
Void::Testing::expect_eq(*ch.try_receive(), std::string{"hello"});
|
||||
Void::Testing::expect_eq(*ch.try_receive(), std::string{"world"});
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
#include <Void/Async/Generator.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace Void::Coro;
|
||||
|
||||
// ── generator helpers ─────────────────────────────────────────────────────────
|
||||
|
||||
static Generator<int> iota_gen(int start = 0, int step = 1)
|
||||
{
|
||||
while (true) { co_yield start; start += step; }
|
||||
}
|
||||
|
||||
static Generator<int> fibonacci_gen()
|
||||
{
|
||||
int a = 0, b = 1;
|
||||
while (true) {
|
||||
co_yield a;
|
||||
int t = a + b; a = b; b = t;
|
||||
}
|
||||
}
|
||||
|
||||
static Generator<int> finite_gen(int lo, int hi)
|
||||
{
|
||||
for (int i = lo; i < hi; ++i) co_yield i;
|
||||
}
|
||||
|
||||
// ── Generator<T> tests ────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test coro_generator_range_for()
|
||||
{
|
||||
std::vector<int> out;
|
||||
for (int v : take(iota_gen(0), 5))
|
||||
out.push_back(v);
|
||||
Void::Testing::expect_eq(out, std::vector<int>{0, 1, 2, 3, 4});
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_generator_finite()
|
||||
{
|
||||
std::vector<int> out;
|
||||
for (int v : finite_gen(3, 7))
|
||||
out.push_back(v);
|
||||
Void::Testing::expect_eq(out, std::vector<int>{3, 4, 5, 6});
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_generator_fibonacci()
|
||||
{
|
||||
std::vector<int> fibs;
|
||||
for (int v : take(fibonacci_gen(), 8))
|
||||
fibs.push_back(v);
|
||||
Void::Testing::expect_eq(fibs, std::vector<int>{0, 1, 1, 2, 3, 5, 8, 13});
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_generator_take_fewer_than_available()
|
||||
{
|
||||
std::vector<int> out;
|
||||
for (int v : take(iota_gen(10), 3))
|
||||
out.push_back(v);
|
||||
Void::Testing::expect_eq(out.size(), std::size_t{3});
|
||||
Void::Testing::expect_eq(out[0], 10);
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_generator_take_zero()
|
||||
{
|
||||
std::vector<int> out;
|
||||
for (int v : take(iota_gen(), 0))
|
||||
out.push_back(v);
|
||||
Void::Testing::expect(out.empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_generator_filter()
|
||||
{
|
||||
std::vector<int> out;
|
||||
auto evens = filter(take(iota_gen(0), 10), [](int v) { return v % 2 == 0; });
|
||||
for (int v : evens)
|
||||
out.push_back(v);
|
||||
Void::Testing::expect_eq(out, std::vector<int>{0, 2, 4, 6, 8});
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_generator_map()
|
||||
{
|
||||
std::vector<int> out;
|
||||
auto doubled = map(take(iota_gen(1), 5), [](int& v) { return v * 2; });
|
||||
for (int v : doubled)
|
||||
out.push_back(v);
|
||||
Void::Testing::expect_eq(out, std::vector<int>{2, 4, 6, 8, 10});
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_generator_next_pull()
|
||||
{
|
||||
auto g = iota_gen(100);
|
||||
Void::Testing::expect_eq(g.next(), std::optional<int>{100});
|
||||
Void::Testing::expect_eq(g.next(), std::optional<int>{101});
|
||||
Void::Testing::expect_eq(g.next(), std::optional<int>{102});
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_generator_finite_exhaustion()
|
||||
{
|
||||
auto g = finite_gen(0, 2);
|
||||
Void::Testing::expect_eq(g.next(), std::optional<int>{0});
|
||||
Void::Testing::expect_eq(g.next(), std::optional<int>{1});
|
||||
Void::Testing::expect(!g.next().has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_generator_move()
|
||||
{
|
||||
auto g1 = iota_gen(5);
|
||||
auto g2 = std::move(g1);
|
||||
Void::Testing::expect(!g1.valid());
|
||||
Void::Testing::expect_eq(g2.next(), std::optional<int>{5});
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_generator_from_range()
|
||||
{
|
||||
const std::vector<int> v{10, 20, 30};
|
||||
std::vector<int> out;
|
||||
for (int x : from_range(v.begin(), v.end()))
|
||||
out.push_back(x);
|
||||
Void::Testing::expect_eq(out, v);
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_generator_step()
|
||||
{
|
||||
std::vector<int> out;
|
||||
for (int v : take(iota_gen(0, 3), 4))
|
||||
out.push_back(v);
|
||||
Void::Testing::expect_eq(out, std::vector<int>{0, 3, 6, 9});
|
||||
}
|
||||
|
||||
// ── Task<T> tests ─────────────────────────────────────────────────────────────
|
||||
|
||||
static Task<int> simple_task(int x) { co_return x * 2; }
|
||||
static Task<void> void_task(std::string& out) { out = "done"; co_return; }
|
||||
static Task<int> chained_task() { int v = co_await simple_task(5); co_return v + 1; }
|
||||
|
||||
Void::Testing::Test coro_task_returns_value()
|
||||
{
|
||||
Void::Testing::expect_eq(sync_wait(simple_task(7)), 14);
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_task_void()
|
||||
{
|
||||
std::string result;
|
||||
sync_wait(void_task(result));
|
||||
Void::Testing::expect_eq(result, std::string{"done"});
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_task_chain()
|
||||
{
|
||||
// chained_task awaits simple_task(5) → 10, then returns 11
|
||||
Void::Testing::expect_eq(sync_wait(chained_task()), 11);
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_task_exception_propagates()
|
||||
{
|
||||
auto throwing = []() -> Task<int> {
|
||||
throw std::runtime_error{"boom"};
|
||||
co_return 0;
|
||||
};
|
||||
|
||||
bool caught = false;
|
||||
try {
|
||||
sync_wait(throwing());
|
||||
} catch (const std::runtime_error& e) {
|
||||
caught = true;
|
||||
Void::Testing::expect(std::string_view{e.what()} == std::string_view{"boom"});
|
||||
}
|
||||
Void::Testing::expect(caught);
|
||||
}
|
||||
|
||||
Void::Testing::Test coro_task_done_after_run()
|
||||
{
|
||||
auto t = simple_task(3);
|
||||
Void::Testing::expect(!t.done());
|
||||
t.run();
|
||||
Void::Testing::expect(t.done());
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
#include <Void/Async/EventBus.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace Void::EventBus;
|
||||
|
||||
struct Clicked { int x, y; };
|
||||
struct TextChanged { std::string text; };
|
||||
|
||||
// ── subscribe / publish ───────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bus_publish_reaches_subscriber()
|
||||
{
|
||||
EventBus bus;
|
||||
int received = 0;
|
||||
bus.subscribe<Clicked>([&](const Clicked& e) { received = e.x; });
|
||||
bus.publish(Clicked{42, 0});
|
||||
Void::Testing::expect_eq(received, 42);
|
||||
}
|
||||
|
||||
Void::Testing::Test bus_multiple_subscribers_same_type()
|
||||
{
|
||||
EventBus bus;
|
||||
int count = 0;
|
||||
bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
||||
bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
||||
bus.publish(Clicked{0, 0});
|
||||
Void::Testing::expect_eq(count, 2);
|
||||
}
|
||||
|
||||
Void::Testing::Test bus_different_event_types_isolated()
|
||||
{
|
||||
EventBus bus;
|
||||
bool got_click = false;
|
||||
bool got_text = false;
|
||||
bus.subscribe<Clicked>([&](const Clicked&) { got_click = true; });
|
||||
bus.subscribe<TextChanged>([&](const TextChanged&) { got_text = true; });
|
||||
bus.publish(Clicked{0, 0});
|
||||
Void::Testing::expect(got_click);
|
||||
Void::Testing::expect(!got_text);
|
||||
}
|
||||
|
||||
Void::Testing::Test bus_no_subscribers_publish_is_noop()
|
||||
{
|
||||
EventBus bus;
|
||||
bus.publish(Clicked{1, 2});
|
||||
}
|
||||
|
||||
// ── unsubscribe ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bus_unsubscribe_stops_delivery()
|
||||
{
|
||||
EventBus bus;
|
||||
int count = 0;
|
||||
const auto id = bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
||||
bus.publish(Clicked{});
|
||||
bus.unsubscribe(id);
|
||||
bus.publish(Clicked{});
|
||||
Void::Testing::expect_eq(count, 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test bus_unsubscribe_nonexistent_is_noop()
|
||||
{
|
||||
EventBus bus;
|
||||
bus.unsubscribe(999);
|
||||
}
|
||||
|
||||
// ── clear ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bus_clear_event_type()
|
||||
{
|
||||
EventBus bus;
|
||||
int count = 0;
|
||||
bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
||||
bus.clear<Clicked>();
|
||||
bus.publish(Clicked{});
|
||||
Void::Testing::expect_eq(count, 0);
|
||||
}
|
||||
|
||||
Void::Testing::Test bus_clear_all()
|
||||
{
|
||||
EventBus bus;
|
||||
int a = 0, b = 0;
|
||||
bus.subscribe<Clicked>([&](const Clicked&) { ++a; });
|
||||
bus.subscribe<TextChanged>([&](const TextChanged&) { ++b; });
|
||||
bus.clear_all();
|
||||
bus.publish(Clicked{});
|
||||
bus.publish(TextChanged{});
|
||||
Void::Testing::expect_eq(a, 0);
|
||||
Void::Testing::expect_eq(b, 0);
|
||||
}
|
||||
|
||||
// ── subscriber_count ──────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bus_subscriber_count()
|
||||
{
|
||||
EventBus bus;
|
||||
Void::Testing::expect_eq(bus.subscriber_count<Clicked>(), std::size_t{0});
|
||||
bus.subscribe<Clicked>([](const Clicked&) {});
|
||||
bus.subscribe<Clicked>([](const Clicked&) {});
|
||||
Void::Testing::expect_eq(bus.subscriber_count<Clicked>(), std::size_t{2});
|
||||
}
|
||||
|
||||
// ── event data forwarded correctly ────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bus_event_data_forwarded()
|
||||
{
|
||||
EventBus bus;
|
||||
std::string received;
|
||||
bus.subscribe<TextChanged>([&](const TextChanged& e) { received = e.text; });
|
||||
bus.publish(TextChanged{"hello"});
|
||||
Void::Testing::expect_eq(received, std::string{"hello"});
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
#include <Void/Async/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"});
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <Void/Async/ParallelFor.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <vector>
|
||||
|
||||
using namespace Void::Concurrency;
|
||||
|
||||
Void::Testing::Test parallel_sum_matches_sequential()
|
||||
{
|
||||
constexpr int N = 1000;
|
||||
std::atomic<long long> total{0};
|
||||
parallel_for(0, N, [&](int i) { total += i; });
|
||||
Void::Testing::expect_eq(total.load(),
|
||||
static_cast<long long>(N) * (N - 1) / 2);
|
||||
}
|
||||
|
||||
Void::Testing::Test empty_range_does_nothing()
|
||||
{
|
||||
std::atomic<int> calls{0};
|
||||
parallel_for(5, 5, [&](int) { ++calls; });
|
||||
Void::Testing::expect_eq(calls.load(), 0);
|
||||
}
|
||||
|
||||
Void::Testing::Test with_explicit_pool()
|
||||
{
|
||||
ThreadPool pool{2};
|
||||
std::atomic<int> count{0};
|
||||
parallel_for(0, 10, [&](int) { ++count; }, pool);
|
||||
Void::Testing::expect_eq(count.load(), 10);
|
||||
}
|
||||
|
||||
Void::Testing::Test results_written_to_indexed_array()
|
||||
{
|
||||
constexpr int N = 64;
|
||||
std::vector<int> out(N, 0);
|
||||
parallel_for(0, N, [&](int i) { out[i] = i * i; });
|
||||
for (int i = 0; i < N; ++i) {
|
||||
Void::Testing::expect_eq(out[i], i * i);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test exception_propagates()
|
||||
{
|
||||
bool caught = false;
|
||||
try {
|
||||
parallel_for(0, 4, [](int i) {
|
||||
if (i == 2) {
|
||||
throw std::runtime_error("bad index");
|
||||
}
|
||||
});
|
||||
} catch (const std::runtime_error&) {
|
||||
caught = true;
|
||||
}
|
||||
Void::Testing::expect(caught);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#include <Void/Async/ThreadPool.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
using Void::Concurrency::ThreadPool;
|
||||
|
||||
Void::Testing::Test thread_count_matches_requested()
|
||||
{
|
||||
ThreadPool pool{4};
|
||||
Void::Testing::expect_eq(pool.thread_count(), std::size_t{4});
|
||||
}
|
||||
|
||||
Void::Testing::Test defaults_to_hardware_concurrency()
|
||||
{
|
||||
ThreadPool pool;
|
||||
Void::Testing::expect(pool.thread_count() >= 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test submit_returns_correct_value()
|
||||
{
|
||||
ThreadPool pool{2};
|
||||
auto fut = pool.submit([] { return 42; });
|
||||
Void::Testing::expect_eq(fut.get(), 42);
|
||||
}
|
||||
|
||||
Void::Testing::Test tasks_run_concurrently()
|
||||
{
|
||||
constexpr int N = 8;
|
||||
ThreadPool pool{N};
|
||||
std::atomic<int> counter{0};
|
||||
std::vector<std::future<void>> futs;
|
||||
futs.reserve(N);
|
||||
for (int i = 0; i < N; ++i) {
|
||||
futs.push_back(pool.submit([&] {
|
||||
++counter;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
||||
}));
|
||||
}
|
||||
for (auto& f : futs) {
|
||||
f.get();
|
||||
}
|
||||
Void::Testing::expect_eq(counter.load(), N);
|
||||
}
|
||||
|
||||
Void::Testing::Test parallel_sum_is_correct()
|
||||
{
|
||||
ThreadPool pool{4};
|
||||
constexpr int N = 100;
|
||||
std::atomic<long long> total{0};
|
||||
std::vector<std::future<void>> futs;
|
||||
futs.reserve(N);
|
||||
for (int i = 1; i <= N; ++i) {
|
||||
futs.push_back(pool.submit([&, i] { total += i; }));
|
||||
}
|
||||
for (auto& f : futs) {
|
||||
f.get();
|
||||
}
|
||||
Void::Testing::expect_eq(total.load(), 5050LL);
|
||||
}
|
||||
|
||||
Void::Testing::Test exception_propagates_through_future()
|
||||
{
|
||||
ThreadPool pool{1};
|
||||
auto fut = pool.submit([] -> int { throw std::runtime_error("boom"); });
|
||||
bool caught = false;
|
||||
try {
|
||||
fut.get();
|
||||
} catch (const std::runtime_error& e) {
|
||||
caught = (std::string{e.what()} == "boom");
|
||||
}
|
||||
Void::Testing::expect(caught);
|
||||
}
|
||||
|
||||
Void::Testing::Test submit_after_destruction_throws()
|
||||
{
|
||||
std::optional<std::future<int>> fut;
|
||||
{
|
||||
ThreadPool pool{1};
|
||||
fut = pool.submit([] { return 1; });
|
||||
fut->get();
|
||||
}
|
||||
// pool is now destroyed; this just verifies destruction was clean
|
||||
Void::Testing::expect(true);
|
||||
}
|
||||
Reference in new issue
Block a user