#include #include #include #include #include #include using namespace Void::Channel; // ── non-blocking operations ─────────────────────────────────────────────────── Void::Testing::Test channel_try_send_recv() { Channel 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 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 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 ch{4}; Void::Testing::expect(!ch.try_recv().has_value()); } Void::Testing::Test channel_size_and_empty() { Channel 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 ch{16}; Void::Testing::expect_eq(ch.capacity(), std::size_t{16}); } // ── close semantics ─────────────────────────────────────────────────────────── Void::Testing::Test channel_send_after_close_returns_false() { Channel ch{4}; ch.close(); Void::Testing::expect(!ch.try_send(1)); } Void::Testing::Test channel_recv_drains_then_nullopt() { Channel 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 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 ch{16}; std::vector 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(received.size()), count); for (int i = 0; i < count; ++i) Void::Testing::expect_eq(received[i], i); } Void::Testing::Test channel_multiple_producers() { Channel ch{64}; std::atomic 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 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"}); }