Files
IACore/Tests/Unit/StreamReader.cpp
dev0 cfa0759133
Some checks failed
CI / build-linux-and-wasm (x64-linux) (push) Has been cancelled
TestSuite
2026-01-23 05:52:26 +05:30

170 lines
4.9 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/IATest.hpp>
#include <IACore/StreamReader.hpp>
using namespace IACore;
IAT_BEGIN_BLOCK(Core, StreamReader)
// -------------------------------------------------------------------------
// 1. Basic Primitive Reading (u8)
// -------------------------------------------------------------------------
auto test_read_uint8() -> bool {
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(), static_cast<usize>(1));
// Read Second Byte
auto val2 = reader.read<u8>();
IAT_CHECK(val2.has_value());
IAT_CHECK_EQ(*val2, 0xBB);
return true;
}
// -------------------------------------------------------------------------
// 2. Multi-byte Reading (Endianness check)
// -------------------------------------------------------------------------
auto test_read_multi_byte() -> bool {
// 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, static_cast<u32>(0x04030201));
IAT_CHECK_EQ(reader.cursor(), static_cast<usize>(4));
IAT_CHECK(reader.is_eof());
return true;
}
// -------------------------------------------------------------------------
// 3. Floating Point (Approx check)
// -------------------------------------------------------------------------
auto test_read_float() -> bool {
const 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
// -------------------------------------------------------------------------
auto test_read_buffer() -> bool {
u8 src[] = {1, 2, 3, 4, 5};
u8 dst[3] = {0};
StreamReader reader(src);
// Read 3 bytes into dst
const 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(), static_cast<usize>(3));
return true;
}
// -------------------------------------------------------------------------
// 5. Navigation (Seek, Skip, Remaining)
// -------------------------------------------------------------------------
auto test_navigation() -> bool {
u8 data[10] = {0}; // Zero init
StreamReader reader(data);
IAT_CHECK_EQ(reader.remaining(), static_cast<usize>(10));
// Skip
reader.skip(5);
IAT_CHECK_EQ(reader.cursor(), static_cast<usize>(5));
IAT_CHECK_EQ(reader.remaining(), static_cast<usize>(5));
// Skip clamping
reader.skip(100); // Should clamp to 10
IAT_CHECK_EQ(reader.cursor(), static_cast<usize>(10));
IAT_CHECK(reader.is_eof());
// Seek
reader.seek(2);
IAT_CHECK_EQ(reader.cursor(), static_cast<usize>(2));
IAT_CHECK_EQ(reader.remaining(), static_cast<usize>(8));
IAT_CHECK_NOT(reader.is_eof());
return true;
}
// -------------------------------------------------------------------------
// 6. Error Handling (EOF Protection)
// -------------------------------------------------------------------------
auto test_boundary_checks() -> bool {
u8 data[] = {0x00, 0x00};
StreamReader reader(data);
// Valid read
(void)reader.read<u16>();
IAT_CHECK(reader.is_eof());
// 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(test_read_uint8);
IAT_ADD_TEST(test_read_multi_byte);
IAT_ADD_TEST(test_read_float);
IAT_ADD_TEST(test_read_buffer);
IAT_ADD_TEST(test_navigation);
IAT_ADD_TEST(test_boundary_checks);
IAT_END_TEST_LIST()
IAT_END_BLOCK()
IAT_REGISTER_ENTRY(Core, StreamReader)