Files
Library/tests/standalone/Channel/Channel.cpp
T
faumarayandClaude Sonnet 4.6 58ff7b38a0 Add 20+ new modules with tests: Pipeline, Matrix, Tokenizer, ScopeGuard, Bitset, Optional, Counted, Curry, TypeMap, Bench, DisjointSet, Histogram, StopWatch, and more
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>
2026-05-14 23:49:09 +03:00

145 lines
3.9 KiB
C++

#include <Void/Channel/Channel.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <thread>
#include <vector>
using namespace Void::Channel;
// ── non-blocking operations ───────────────────────────────────────────────────
Void::Testing::Test channel_try_send_recv()
{
Channel<int> ch{4};
Void::Testing::expect(ch.try_send(1));
Void::Testing::expect(ch.try_send(2));
const auto v = ch.try_recv();
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, 1);
}
Void::Testing::Test channel_fifo_order()
{
Channel<int> ch{8};
for (int i = 0; i < 5; ++i) ch.try_send(i);
for (int i = 0; i < 5; ++i) {
const auto v = ch.try_recv();
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, i);
}
}
Void::Testing::Test channel_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)); // full
}
Void::Testing::Test channel_try_recv_empty_returns_nullopt()
{
Channel<int> ch{4};
Void::Testing::expect(!ch.try_recv().has_value());
}
Void::Testing::Test channel_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 channel_capacity()
{
Channel<int> ch{16};
Void::Testing::expect_eq(ch.capacity(), std::size_t{16});
}
// ── close semantics ───────────────────────────────────────────────────────────
Void::Testing::Test channel_send_after_close_returns_false()
{
Channel<int> ch{4};
ch.close();
Void::Testing::expect(!ch.try_send(1));
}
Void::Testing::Test channel_recv_drains_then_nullopt()
{
Channel<int> ch{4};
ch.try_send(10);
ch.try_send(20);
ch.close();
Void::Testing::expect_eq(*ch.try_recv(), 10);
Void::Testing::expect_eq(*ch.try_recv(), 20);
Void::Testing::expect(!ch.try_recv().has_value());
}
Void::Testing::Test channel_closed_flag()
{
Channel<int> ch{4};
Void::Testing::expect(!ch.closed());
ch.close();
Void::Testing::expect(ch.closed());
}
// ── blocking with threads ─────────────────────────────────────────────────────
Void::Testing::Test channel_producer_consumer_thread()
{
Channel<int> ch{16};
std::vector<int> received;
const int count = 10;
std::thread producer([&] {
for (int i = 0; i < count; ++i) ch.send(i);
ch.close();
});
while (auto v = ch.recv())
received.push_back(*v);
producer.join();
Void::Testing::expect_eq(static_cast<int>(received.size()), count);
for (int i = 0; i < count; ++i)
Void::Testing::expect_eq(received[i], i);
}
Void::Testing::Test channel_multiple_producers()
{
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.recv()) total += *v;
// Sum of 0+1+2+3+4 = 10, two producers → 20
Void::Testing::expect_eq(total.load(), 20);
}
Void::Testing::Test channel_string_values()
{
Channel<std::string> ch{4};
ch.try_send("hello");
ch.try_send("world");
Void::Testing::expect_eq(*ch.try_recv(), std::string{"hello"});
Void::Testing::expect_eq(*ch.try_recv(), std::string{"world"});
}