Files
IACore/Tests/Unit/StreamReader.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

171 lines
4.6 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/StreamReader.hpp>
#include <IACore/IATest.hpp>
using namespace IACore;
IAT_BEGIN_BLOCK(Core, StreamReader)
// -------------------------------------------------------------------------
// 1. Basic Primitive Reading (u8)
// -------------------------------------------------------------------------
bool TestReadUint8() {
u8 data[] = {0xAA, 0xBB, 0xCC};
StreamReader reader(data);
// Read First Byte
auto val1 = reader.Read<u8>();
IAT_CHECK(val1.has_value());
IAT_CHECK_EQ(*val1, 0xAA);
IAT_CHECK_EQ(reader.Cursor(), (usize)1);
// Read Second Byte
auto val2 = reader.Read<u8>();
IAT_CHECK_EQ(*val2, 0xBB);
return TRUE;
}
// -------------------------------------------------------------------------
// 2. Multi-byte Reading (Endianness check)
// -------------------------------------------------------------------------
bool TestReadMultiByte() {
// 0x04030201 in Little Endian memory layout
// IACore always assumes a Little Endian machine
u8 data[] = {0x01, 0x02, 0x03, 0x04};
StreamReader reader(data);
auto val = reader.Read<u32>();
IAT_CHECK(val.has_value());
IAT_CHECK_EQ(*val, (u32)0x04030201);
IAT_CHECK_EQ(reader.Cursor(), (usize)4);
IAT_CHECK(reader.IsEOF());
return TRUE;
}
// -------------------------------------------------------------------------
// 3. Floating Point (Approx check)
// -------------------------------------------------------------------------
bool TestReadFloat() {
f32 pi = 3.14159f;
// Bit-cast float to bytes for setup
u8 data[4];
std::memcpy(data, &pi, 4);
StreamReader reader(data);
auto val = reader.Read<f32>();
IAT_CHECK(val.has_value());
IAT_CHECK_APPROX(*val, pi);
return TRUE;
}
// -------------------------------------------------------------------------
// 4. Batch Buffer Reading
// -------------------------------------------------------------------------
bool TestReadBuffer() {
u8 src[] = {1, 2, 3, 4, 5};
u8 dst[3] = {0};
StreamReader reader(src);
// Read 3 bytes into dst
auto res = reader.Read(dst, 3);
IAT_CHECK(res.has_value());
// Verify dst content
IAT_CHECK_EQ(dst[0], 1);
IAT_CHECK_EQ(dst[1], 2);
IAT_CHECK_EQ(dst[2], 3);
// Verify cursor
IAT_CHECK_EQ(reader.Cursor(), (usize)3);
return TRUE;
}
// -------------------------------------------------------------------------
// 5. Navigation (Seek, Skip, Remaining)
// -------------------------------------------------------------------------
bool TestNavigation() {
u8 data[10] = {0}; // Zero init
StreamReader reader(data);
IAT_CHECK_EQ(reader.Remaining(), (usize)10);
// Skip
reader.Skip(5);
IAT_CHECK_EQ(reader.Cursor(), (usize)5);
IAT_CHECK_EQ(reader.Remaining(), (usize)5);
// Skip clamping
reader.Skip(100); // Should clamp to 10
IAT_CHECK_EQ(reader.Cursor(), (usize)10);
IAT_CHECK(reader.IsEOF());
// Seek
reader.Seek(2);
IAT_CHECK_EQ(reader.Cursor(), (usize)2);
IAT_CHECK_EQ(reader.Remaining(), (usize)8);
IAT_CHECK_NOT(reader.IsEOF());
return TRUE;
}
// -------------------------------------------------------------------------
// 6. Error Handling (EOF Protection)
// -------------------------------------------------------------------------
bool TestBoundaryChecks() {
u8 data[] = {0x00, 0x00};
StreamReader reader(data);
// Valid read
UNUSED(reader.Read<u16>());
IAT_CHECK(reader.IsEOF());
// Invalid Read Primitive
auto val = reader.Read<u8>();
IAT_CHECK_NOT(val.has_value()); // Should be unexpected
// Invalid Batch Read
u8 buf[1];
auto batch = reader.Read(buf, 1);
IAT_CHECK_NOT(batch.has_value());
return TRUE;
}
// -------------------------------------------------------------------------
// Registration
// -------------------------------------------------------------------------
IAT_BEGIN_TEST_LIST()
IAT_ADD_TEST(TestReadUint8);
IAT_ADD_TEST(TestReadMultiByte);
IAT_ADD_TEST(TestReadFloat);
IAT_ADD_TEST(TestReadBuffer);
IAT_ADD_TEST(TestNavigation);
IAT_ADD_TEST(TestBoundaryChecks);
IAT_END_TEST_LIST()
IAT_END_BLOCK()
IAT_REGISTER_ENTRY(Core, StreamReader)