This commit is contained in:
@ -20,31 +20,23 @@ using namespace IACore;
|
||||
|
||||
IAT_BEGIN_BLOCK(Core, RingBuffer)
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 1. Basic Push Pop
|
||||
// -------------------------------------------------------------------------
|
||||
auto test_push_pop() -> bool {
|
||||
// Allocate raw memory for the ring buffer
|
||||
// ControlBlock (128 bytes) + Data
|
||||
|
||||
Vec<u8> memory(sizeof(RingBufferView::ControlBlock) + 1024);
|
||||
|
||||
// Initialize as OWNER (Producer)
|
||||
auto producer_res = RingBufferView::create(Span<u8>(memory), true);
|
||||
IAT_CHECK(producer_res.has_value());
|
||||
auto producer = std::move(*producer_res);
|
||||
|
||||
// Initialize as CONSUMER (Pointer to same memory)
|
||||
auto consumer_res = RingBufferView::create(Span<u8>(memory), false);
|
||||
IAT_CHECK(consumer_res.has_value());
|
||||
auto consumer = std::move(*consumer_res);
|
||||
|
||||
// Data to send
|
||||
String msg = "Hello RingBuffer";
|
||||
const auto push_res = producer.push(
|
||||
1, Span<const u8>(reinterpret_cast<const u8 *>(msg.data()), msg.size()));
|
||||
IAT_CHECK(push_res.has_value());
|
||||
|
||||
// Read back
|
||||
RingBufferView::PacketHeader header;
|
||||
u8 read_buf[128];
|
||||
|
||||
@ -65,38 +57,28 @@ auto test_push_pop() -> bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 2. Wrap Around
|
||||
// -------------------------------------------------------------------------
|
||||
auto test_wrap_around() -> bool {
|
||||
// Small buffer to force wrapping quickly
|
||||
// Capacity will be 100 bytes
|
||||
|
||||
Vec<u8> memory(sizeof(RingBufferView::ControlBlock) + 100);
|
||||
|
||||
auto rb_res = RingBufferView::create(Span<u8>(memory), true);
|
||||
IAT_CHECK(rb_res.has_value());
|
||||
auto rb = std::move(*rb_res);
|
||||
|
||||
// Fill buffer to near end
|
||||
// Push 80 bytes
|
||||
Vec<u8> junk(80, 0xFF);
|
||||
const auto push1 = rb.push(1, junk);
|
||||
IAT_CHECK(push1.has_value());
|
||||
|
||||
// Pop them to advance READ cursor
|
||||
RingBufferView::PacketHeader header;
|
||||
u8 out_buf[100];
|
||||
const auto pop1 = rb.pop(header, out_buf);
|
||||
IAT_CHECK(pop1.has_value());
|
||||
IAT_CHECK(pop1->has_value());
|
||||
|
||||
// Now READ and WRITE are near index 80.
|
||||
// Pushing 40 bytes should trigger a wrap-around (split write)
|
||||
Vec<u8> wrap_data(40, 0xAA);
|
||||
const auto push2 = rb.push(2, wrap_data);
|
||||
IAT_CHECK(push2.has_value());
|
||||
|
||||
// Pop and verify integrity
|
||||
const auto pop2 = rb.pop(header, out_buf);
|
||||
IAT_CHECK(pop2.has_value());
|
||||
IAT_CHECK(pop2->has_value());
|
||||
@ -104,7 +86,6 @@ auto test_wrap_around() -> bool {
|
||||
const usize pop_size = *pop2.value();
|
||||
IAT_CHECK_EQ(pop_size, static_cast<usize>(40));
|
||||
|
||||
// Check if data is intact
|
||||
bool match = true;
|
||||
for (usize i = 0; i < 40; i++) {
|
||||
if (out_buf[i] != 0xAA) {
|
||||
|
||||
Reference in New Issue
Block a user