#include #include #include #include #include #include #include #include #include // ── SpscRing ────────────────────────────────────────────────────────────────── Void::Testing::Test spsc_empty_on_construction() { Void::Ring::SpscRing ring; Void::Testing::expect(ring.empty()); } Void::Testing::Test spsc_push_pop_single() { Void::Ring::SpscRing ring; Void::Testing::expect(ring.push(42)); const auto v = ring.pop(); Void::Testing::expect(v.has_value()); Void::Testing::expect_eq(*v, 42); } Void::Testing::Test spsc_pop_empty_returns_nullopt() { Void::Ring::SpscRing ring; Void::Testing::expect(!ring.pop().has_value()); } Void::Testing::Test spsc_full_push_returns_false() { Void::Ring::SpscRing ring; // capacity = 3 Void::Testing::expect(ring.push(1)); Void::Testing::expect(ring.push(2)); Void::Testing::expect(ring.push(3)); Void::Testing::expect(!ring.push(4)); // full } Void::Testing::Test spsc_fifo_order() { Void::Ring::SpscRing ring; for (int i = 0; i < 5; ++i) ring.push(i); for (int i = 0; i < 5; ++i) { const auto v = ring.pop(); Void::Testing::expect(v.has_value()); Void::Testing::expect_eq(*v, i); } } Void::Testing::Test spsc_capacity_is_n_minus_one() { Void::Testing::expect_eq(Void::Ring::SpscRing::capacity(), std::size_t{15}); } Void::Testing::Test spsc_full_flag() { Void::Ring::SpscRing ring; ring.push(1); ring.push(2); ring.push(3); Void::Testing::expect(ring.full()); } Void::Testing::Test spsc_string_values() { Void::Ring::SpscRing ring; ring.push(std::string{"hello"}); const auto v = ring.pop(); Void::Testing::expect(v.has_value()); Void::Testing::expect_eq(*v, std::string{"hello"}); } // @tag threading Void::Testing::Test spsc_producer_consumer_threads() { Void::Ring::SpscRing ring; constexpr int N = 50; std::vector received; received.reserve(N); std::thread producer([&] { for (int i = 0; i < N; ++i) { while (!ring.push(i)) {} } }); std::thread consumer([&] { int got = 0; while (got < N) { auto v = ring.pop(); if (v) { received.push_back(*v); ++got; } } }); producer.join(); consumer.join(); Void::Testing::expect_eq(received.size(), std::size_t{N}); for (int i = 0; i < N; ++i) { Void::Testing::expect_eq(received[i], i); } } // ── MpscRing ────────────────────────────────────────────────────────────────── Void::Testing::Test mpsc_push_pop_single() { Void::Ring::MpscRing ring; Void::Testing::expect(ring.push(99)); const auto v = ring.pop(); Void::Testing::expect(v.has_value()); Void::Testing::expect_eq(*v, 99); } Void::Testing::Test mpsc_empty_on_construction() { Void::Ring::MpscRing ring; Void::Testing::expect(ring.empty()); } // @tag threading Void::Testing::Test mpsc_two_producers_one_consumer() { Void::Ring::MpscRing ring; constexpr int N = 40; std::atomic total{0}; std::thread p1([&] { for (int i = 0; i < N; ++i) while (!ring.push(1)) {} }); std::thread p2([&] { for (int i = 0; i < N; ++i) while (!ring.push(2)) {} }); std::thread consumer([&] { int got = 0; while (got < N * 2) { auto v = ring.pop(); if (v) { total.fetch_add(*v, std::memory_order_relaxed); ++got; } } }); p1.join(); p2.join(); consumer.join(); Void::Testing::expect_eq(total.load(), N * 3); // N×1 + N×2 = 3N } namespace { struct CountedItem { int value{0}; std::atomic* dtors{nullptr}; CountedItem(int v, std::atomic* d) noexcept : value{v}, dtors{d} {} CountedItem(CountedItem&& o) noexcept : value{o.value}, dtors{std::exchange(o.dtors, nullptr)} { } CountedItem& operator=(CountedItem&& o) noexcept { if (this != &o) { if (dtors) dtors->fetch_add(1, std::memory_order_relaxed); value = o.value; dtors = std::exchange(o.dtors, nullptr); } return *this; } ~CountedItem() { if (dtors) { dtors->fetch_add(1, std::memory_order_relaxed); } } }; struct DelayedMoveItem { int id{0}; std::atomic* entered_move{nullptr}; std::atomic* release_move{nullptr}; DelayedMoveItem() noexcept = default; DelayedMoveItem(int v, std::atomic* entered, std::atomic* release) noexcept : id{v}, entered_move{entered}, release_move{release} { } DelayedMoveItem(DelayedMoveItem&& other) noexcept : id{other.id}, entered_move{nullptr}, release_move{nullptr} { if (other.entered_move && other.release_move) { other.entered_move->store(true, std::memory_order_release); while (!other.release_move->load(std::memory_order_acquire)) { std::this_thread::yield(); } } } DelayedMoveItem& operator=(DelayedMoveItem&&) noexcept = default; ~DelayedMoveItem() noexcept = default; }; } // namespace Void::Testing::Test spsc_and_mpsc_close_wraparound_and_destructor_cleanup() { using Void::Ring::PopStatus; using Void::Ring::PushStatus; std::atomic spsc_dtors{0}; { Void::Ring::SpscRing spsc; for (int i = 0; i < 16; ++i) { Void::Testing::expect(spsc.try_push(CountedItem{i, &spsc_dtors}) == PushStatus::ok); auto popped = spsc.try_pop(); Void::Testing::expect(popped.status == PopStatus::ok && popped.value->value == i); } Void::Testing::expect(spsc.try_push(CountedItem{100, &spsc_dtors}) == PushStatus::ok); Void::Testing::expect(spsc.try_push(CountedItem{101, &spsc_dtors}) == PushStatus::ok); spsc.close(); Void::Testing::expect(spsc.try_push(CountedItem{102, &spsc_dtors}) == PushStatus::closed); } Void::Testing::expect_eq(spsc_dtors.load(), 19); std::atomic mpsc_dtors{0}; { Void::Ring::MpscRing mpsc; for (int i = 0; i < 20; ++i) { Void::Testing::expect(mpsc.try_push(CountedItem{i, &mpsc_dtors}) == PushStatus::ok); auto popped = mpsc.try_pop(); Void::Testing::expect(popped.status == PopStatus::ok && popped.value->value == i); } Void::Testing::expect(mpsc.try_push(CountedItem{200, &mpsc_dtors}) == PushStatus::ok); Void::Testing::expect(mpsc.try_push(CountedItem{201, &mpsc_dtors}) == PushStatus::ok); mpsc.close(); Void::Testing::expect(mpsc.try_push(CountedItem{202, &mpsc_dtors}) == PushStatus::closed); } Void::Testing::expect_eq(mpsc_dtors.load(), 23); } // @tag threading Void::Testing::Test mpsc_delayed_producer_returns_pending_without_spinning() { using Void::Ring::PopStatus; using Void::Ring::PushStatus; Void::Ring::MpscRing ring; std::atomic entered{false}; std::atomic release{false}; std::thread producer([&] { Void::Testing::expect(ring.try_push(DelayedMoveItem{77, &entered, &release}) == PushStatus::ok); }); while (!entered.load(std::memory_order_acquire)) { std::this_thread::yield(); } // Slot 0 is reserved (enqueue_pos_ == 1) but not yet published: must return pending immediately. const auto pending = ring.try_pop(); Void::Testing::expect(pending.status == PopStatus::pending); Void::Testing::expect(!pending.has_value()); release.store(true, std::memory_order_release); producer.join(); const auto ready = ring.try_pop(); Void::Testing::expect(ready.status == PopStatus::ok); Void::Testing::require(ready.has_value()); Void::Testing::expect_eq(ready.value->id, 77); ring.close(); const auto closed = ring.try_pop(); Void::Testing::expect(closed.status == PopStatus::closed); } // @tag threading Void::Testing::Test mpsc_high_contention_wraparound_stress() { Void::Ring::MpscRing ring; constexpr int kProducers = 4; constexpr std::uint64_t kPerProducer = 200; std::vector producers; for (int p = 0; p < kProducers; ++p) { producers.emplace_back([&, p] { for (std::uint64_t i = 0; i < kPerProducer; ++i) { const std::uint64_t val = (static_cast(p) << 32) | i; while (ring.try_push(val) != Void::Ring::PushStatus::ok) { std::this_thread::yield(); } } }); } std::uint64_t consumed = 0; std::uint64_t xor_sum = 0; while (consumed < kProducers * kPerProducer) { auto r = ring.try_pop(); if (r.status == Void::Ring::PopStatus::ok) { xor_sum ^= *r.value; ++consumed; } } for (auto& t : producers) { t.join(); } std::uint64_t expected_xor = 0; for (int p = 0; p < kProducers; ++p) { for (std::uint64_t i = 0; i < kPerProducer; ++i) { expected_xor ^= (static_cast(p) << 32) | i; } } Void::Testing::expect_eq(xor_sum, expected_xor); }