Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now an alias for Edit<string> so count_op/apply work on line diffs; Memoized uses std::apply instead of type-based get to handle duplicate arg types. 874/877 tests pass (3 are platform-gated plugin tests). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
182 lines
5.1 KiB
C++
182 lines
5.1 KiB
C++
#include <Void/Coro/Generator.hpp>
|
|
#include <Void/Coro/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());
|
|
}
|