320 lines
9.8 KiB
C++
320 lines
9.8 KiB
C++
#include <Void/Containers/Ring.hpp>
|
||
#include <Void/Testing/Assert.hpp>
|
||
#include <Void/Testing/Test.hpp>
|
||
|
||
#include <atomic>
|
||
#include <cstdint>
|
||
#include <string>
|
||
#include <thread>
|
||
#include <utility>
|
||
#include <vector>
|
||
|
||
// ── SpscRing ──────────────────────────────────────────────────────────────────
|
||
|
||
Void::Testing::Test spsc_empty_on_construction()
|
||
{
|
||
Void::Ring::SpscRing<int, 8> ring;
|
||
Void::Testing::expect(ring.empty());
|
||
}
|
||
|
||
Void::Testing::Test spsc_push_pop_single()
|
||
{
|
||
Void::Ring::SpscRing<int, 8> 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<int, 8> ring;
|
||
Void::Testing::expect(!ring.pop().has_value());
|
||
}
|
||
|
||
Void::Testing::Test spsc_full_push_returns_false()
|
||
{
|
||
Void::Ring::SpscRing<int, 4> 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<int, 8> ring;
|
||
for (int i = 0; i < 5; ++i) (void)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<int, 16>::capacity(),
|
||
std::size_t{15});
|
||
}
|
||
|
||
Void::Testing::Test spsc_full_flag()
|
||
{
|
||
Void::Ring::SpscRing<int, 4> ring;
|
||
(void)ring.push(1); (void)ring.push(2); (void)ring.push(3);
|
||
Void::Testing::expect(ring.full());
|
||
}
|
||
|
||
Void::Testing::Test spsc_string_values()
|
||
{
|
||
Void::Ring::SpscRing<std::string, 4> ring;
|
||
(void)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<int, 64> ring;
|
||
constexpr int N = 50;
|
||
std::vector<int> 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<int, 8> 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<int, 8> ring;
|
||
Void::Testing::expect(ring.empty());
|
||
}
|
||
|
||
// @tag threading
|
||
Void::Testing::Test mpsc_two_producers_one_consumer()
|
||
{
|
||
Void::Ring::MpscRing<int, 128> ring;
|
||
constexpr int N = 40;
|
||
std::atomic<int> 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<int>* dtors{nullptr};
|
||
|
||
CountedItem(int v, std::atomic<int>* 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<bool>* entered_move{nullptr};
|
||
std::atomic<bool>* release_move{nullptr};
|
||
|
||
DelayedMoveItem() noexcept = default;
|
||
DelayedMoveItem(int v, std::atomic<bool>* entered, std::atomic<bool>* 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<int> spsc_dtors{0};
|
||
{
|
||
Void::Ring::SpscRing<CountedItem, 4> 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<int> mpsc_dtors{0};
|
||
{
|
||
Void::Ring::MpscRing<CountedItem, 4> 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<DelayedMoveItem, 8> ring;
|
||
std::atomic<bool> entered{false};
|
||
std::atomic<bool> 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<std::uint64_t, 16> ring;
|
||
constexpr int kProducers = 4;
|
||
constexpr std::uint64_t kPerProducer = 200;
|
||
std::vector<std::thread> 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<std::uint64_t>(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<std::uint64_t>(p) << 32) | i;
|
||
}
|
||
}
|
||
Void::Testing::expect_eq(xor_sum, expected_xor);
|
||
}
|