#include #include #include #include #include using Void::Concurrency::Channel; Void::Testing::Test capacity_is_preserved() { Channel ch{8}; Void::Testing::expect_eq(ch.capacity(), std::size_t{8}); } Void::Testing::Test try_send_and_try_receive_roundtrip() { Channel 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 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 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 ch{16}; constexpr int N = 10; std::thread producer{[&] { for (int i = 0; i < N; ++i) { ch.send(i); } ch.close(); }}; std::vector received; while (auto v = ch.receive()) { received.push_back(*v); } producer.join(); Void::Testing::expect_eq(static_cast(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 ch{4}; ch.close(); Void::Testing::expect(!ch.try_send(1)); } Void::Testing::Test send_returns_false_when_closed() { Channel ch{1}; ch.close(); bool ok = ch.send(42); Void::Testing::expect(!ok); }