Files
Library/tests/standalone/Async/Coro.cpp
T
faumarayandClaude Fable 5 28b94883fd 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>
2026-06-12 18:50:02 +03:00

182 lines
5.1 KiB
C++

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