Files
IACore/Tests/Unit/RingBuffer.cpp
dev0 3ad1e3a2fb
Some checks failed
CI / build-linux-and-wasm (x64-linux) (push) Has been cancelled
Base IACoreV2
2026-01-22 05:54:26 +05:30

105 lines
3.0 KiB
C++

// IACore-OSS; The Core Library for All IA Open Source Projects
// Copyright (C) 2026 IAS (ias@iasoft.dev)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <IACore/ADT/RingBuffer.hpp>
#include <IACore/IATest.hpp>
using namespace IACore;
IAT_BEGIN_BLOCK(Core, RingBuffer)
// -------------------------------------------------------------------------
// 1. Basic Push Pop
// -------------------------------------------------------------------------
bool TestPushPop() {
// Allocate raw memory for the ring buffer
// ControlBlock (128 bytes) + Data
std::vector<u8> memory(sizeof(RingBufferView::ControlBlock) + 1024);
// Initialize as OWNER (Producer)
RingBufferView producer(std::span<u8>(memory), TRUE);
// Initialize as CONSUMER (Pointer to same memory)
RingBufferView consumer(std::span<u8>(memory), FALSE);
// Data to send
String msg = "Hello RingBuffer";
bool pushed = producer.Push(1, {(const u8 *)msg.data(), msg.size()});
IAT_CHECK(pushed);
// Read back
RingBufferView::PacketHeader header;
u8 readBuf[128];
i32 bytesRead = consumer.Pop(header, std::span<u8>(readBuf, 128));
IAT_CHECK_EQ(header.ID, (u16)1);
IAT_CHECK_EQ(bytesRead, (i32)msg.size());
String readMsg((char *)readBuf, bytesRead);
IAT_CHECK_EQ(readMsg, msg);
return TRUE;
}
// -------------------------------------------------------------------------
// 2. Wrap Around
// -------------------------------------------------------------------------
bool TestWrapAround() {
// 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);
// Fill buffer to near end
// Push 80 bytes
std::vector<u8> junk(80, 0xFF);
rb.Push(1, junk);
// Pop them to advance READ cursor
RingBufferView::PacketHeader header;
u8 outBuf[100];
rb.Pop(header, outBuf);
// 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);
// Pop and verify integrity
i32 popSize = rb.Pop(header, outBuf);
IAT_CHECK_EQ(popSize, 40);
// Check if data is intact
bool match = TRUE;
for (int i = 0; i < 40; i++) {
if (outBuf[i] != 0xAA)
match = FALSE;
}
IAT_CHECK(match);
return TRUE;
}
IAT_BEGIN_TEST_LIST()
IAT_ADD_TEST(TestPushPop);
IAT_ADD_TEST(TestWrapAround);
IAT_END_TEST_LIST()
IAT_END_BLOCK()
IAT_REGISTER_ENTRY(Core, RingBuffer)