TestSuite
Some checks failed
CI / build-linux-and-wasm (x64-linux) (push) Has been cancelled

This commit is contained in:
2026-01-23 05:52:26 +05:30
parent fc18c8667a
commit cfa0759133
37 changed files with 5631 additions and 1083 deletions

View File

@ -23,82 +23,104 @@ IAT_BEGIN_BLOCK(Core, RingBuffer)
// -------------------------------------------------------------------------
// 1. Basic Push Pop
// -------------------------------------------------------------------------
bool TestPushPop() {
auto test_push_pop() -> bool {
// Allocate raw memory for the ring buffer
// ControlBlock (128 bytes) + Data
std::vector<u8> memory(sizeof(RingBufferView::ControlBlock) + 1024);
Vec<u8> memory(sizeof(RingBufferView::ControlBlock) + 1024);
// Initialize as OWNER (Producer)
RingBufferView producer(std::span<u8>(memory), TRUE);
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)
RingBufferView consumer(std::span<u8>(memory), FALSE);
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";
bool pushed = producer.Push(1, {(const u8 *)msg.data(), msg.size()});
IAT_CHECK(pushed);
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 readBuf[128];
u8 read_buf[128];
i32 bytesRead = consumer.Pop(header, std::span<u8>(readBuf, 128));
const auto pop_res = consumer.pop(header, Span<u8>(read_buf, 128));
IAT_CHECK(pop_res.has_value());
IAT_CHECK_EQ(header.ID, (u16)1);
IAT_CHECK_EQ(bytesRead, (i32)msg.size());
const auto bytes_read_opt = *pop_res;
IAT_CHECK(bytes_read_opt.has_value());
String readMsg((char *)readBuf, bytesRead);
IAT_CHECK_EQ(readMsg, msg);
const usize bytes_read = *bytes_read_opt;
return TRUE;
IAT_CHECK_EQ(header.id, static_cast<u16>(1));
IAT_CHECK_EQ(bytes_read, static_cast<usize>(msg.size()));
String read_msg(reinterpret_cast<char *>(read_buf), bytes_read);
IAT_CHECK_EQ(read_msg, msg);
return true;
}
// -------------------------------------------------------------------------
// 2. Wrap Around
// -------------------------------------------------------------------------
bool TestWrapAround() {
auto test_wrap_around() -> bool {
// Small buffer to force wrapping quickly
// Capacity will be 100 bytes
std::vector<u8> memory(sizeof(RingBufferView::ControlBlock) + 100);
RingBufferView rb(std::span<u8>(memory), TRUE);
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
std::vector<u8> junk(80, 0xFF);
rb.Push(1, junk);
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 outBuf[100];
rb.Pop(header, outBuf);
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)
std::vector<u8> wrapData(40, 0xAA);
bool pushed = rb.Push(2, wrapData);
IAT_CHECK(pushed);
Vec<u8> wrap_data(40, 0xAA);
const auto push2 = rb.push(2, wrap_data);
IAT_CHECK(push2.has_value());
// Pop and verify integrity
i32 popSize = rb.Pop(header, outBuf);
IAT_CHECK_EQ(popSize, 40);
const auto pop2 = rb.pop(header, out_buf);
IAT_CHECK(pop2.has_value());
IAT_CHECK(pop2->has_value());
const usize pop_size = *pop2.value();
IAT_CHECK_EQ(pop_size, static_cast<usize>(40));
// Check if data is intact
bool match = TRUE;
for (int i = 0; i < 40; i++) {
if (outBuf[i] != 0xAA)
match = FALSE;
bool match = true;
for (usize i = 0; i < 40; i++) {
if (out_buf[i] != 0xAA) {
match = false;
}
}
IAT_CHECK(match);
return TRUE;
return true;
}
IAT_BEGIN_TEST_LIST()
IAT_ADD_TEST(TestPushPop);
IAT_ADD_TEST(TestWrapAround);
IAT_ADD_TEST(test_push_pop);
IAT_ADD_TEST(test_wrap_around);
IAT_END_TEST_LIST()
IAT_END_BLOCK()
IAT_REGISTER_ENTRY(Core, RingBuffer)
IAT_REGISTER_ENTRY(Core, RingBuffer)