This commit is contained in:
@ -20,9 +20,6 @@ using namespace IACore;
|
||||
|
||||
IAT_BEGIN_BLOCK(Core, FileOps)
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// -------------------------------------------------------------------------
|
||||
void cleanup_file(const Path &path) {
|
||||
std::error_code ec;
|
||||
if (std::filesystem::exists(path, ec)) {
|
||||
@ -30,41 +27,30 @@ void cleanup_file(const Path &path) {
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 1. Text File I/O
|
||||
// -------------------------------------------------------------------------
|
||||
auto test_text_io() -> bool {
|
||||
const Path path = "iatest_fileops_text.txt";
|
||||
const String content = "Hello IACore FileOps!\nLine 2";
|
||||
|
||||
// 1. Write
|
||||
const auto write_res = FileOps::write_text_file(path, content, true);
|
||||
IAT_CHECK(write_res.has_value());
|
||||
IAT_CHECK_EQ(*write_res, content.size());
|
||||
|
||||
// 2. Read
|
||||
const auto read_res = FileOps::read_text_file(path);
|
||||
IAT_CHECK(read_res.has_value());
|
||||
IAT_CHECK_EQ(*read_res, content);
|
||||
|
||||
// Cleanup
|
||||
cleanup_file(path);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 2. Binary File I/O
|
||||
// -------------------------------------------------------------------------
|
||||
auto test_binary_io() -> bool {
|
||||
const Path path = "iatest_fileops_bin.bin";
|
||||
const Vec<u8> content = {0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0xFF};
|
||||
|
||||
// 1. Write
|
||||
const auto write_res = FileOps::write_binary_file(path, content, true);
|
||||
IAT_CHECK(write_res.has_value());
|
||||
IAT_CHECK_EQ(*write_res, content.size());
|
||||
|
||||
// 2. Read
|
||||
const auto read_res = FileOps::read_binary_file(path);
|
||||
IAT_CHECK(read_res.has_value());
|
||||
IAT_CHECK_EQ(read_res->size(), content.size());
|
||||
@ -73,22 +59,16 @@ auto test_binary_io() -> bool {
|
||||
IAT_CHECK_EQ((*read_res)[i], content[i]);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
cleanup_file(path);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 3. Memory Mapping (File)
|
||||
// -------------------------------------------------------------------------
|
||||
auto test_file_mapping() -> bool {
|
||||
const Path path = "iatest_fileops_map.txt";
|
||||
const String content = "MappedContent";
|
||||
|
||||
// Setup file
|
||||
(void)FileOps::write_text_file(path, content, true);
|
||||
|
||||
// Map
|
||||
usize size = 0;
|
||||
const auto map_res = FileOps::map_file(path, size);
|
||||
IAT_CHECK(map_res.has_value());
|
||||
@ -97,44 +77,34 @@ auto test_file_mapping() -> bool {
|
||||
const u8 *ptr = *map_res;
|
||||
IAT_CHECK(ptr != nullptr);
|
||||
|
||||
// Verify content via pointer
|
||||
String read_back(reinterpret_cast<const char *>(ptr), size);
|
||||
IAT_CHECK_EQ(read_back, content);
|
||||
|
||||
// Unmap
|
||||
FileOps::unmap_file(ptr);
|
||||
|
||||
cleanup_file(path);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 4. Shared Memory
|
||||
// -------------------------------------------------------------------------
|
||||
auto test_shared_memory() -> bool {
|
||||
const String shm_name = "iatest_shm_block";
|
||||
const usize shm_size = 4096;
|
||||
|
||||
// 1. Create as Owner
|
||||
auto owner_res = FileOps::map_shared_memory(shm_name, shm_size, true);
|
||||
IAT_CHECK(owner_res.has_value());
|
||||
u8 *owner_ptr = *owner_res;
|
||||
|
||||
// Write data
|
||||
std::memset(owner_ptr, 0, shm_size);
|
||||
const String msg = "Shared Memory Message";
|
||||
std::memcpy(owner_ptr, msg.data(), msg.size());
|
||||
|
||||
// 2. Open as Client
|
||||
auto client_res = FileOps::map_shared_memory(shm_name, shm_size, false);
|
||||
IAT_CHECK(client_res.has_value());
|
||||
u8 *client_ptr = *client_res;
|
||||
|
||||
// Verify data
|
||||
String read_msg(reinterpret_cast<const char *>(client_ptr), msg.size());
|
||||
IAT_CHECK_EQ(read_msg, msg);
|
||||
|
||||
// 3. Cleanup
|
||||
FileOps::unmap_file(owner_ptr);
|
||||
FileOps::unmap_file(client_ptr);
|
||||
FileOps::unlink_shared_memory(shm_name);
|
||||
@ -142,14 +112,10 @@ auto test_shared_memory() -> bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 5. Stream Integration
|
||||
// -------------------------------------------------------------------------
|
||||
auto test_stream_integration() -> bool {
|
||||
const Path path = "iatest_fileops_stream.bin";
|
||||
cleanup_file(path);
|
||||
|
||||
// Write via StreamWriter
|
||||
{
|
||||
auto writer_res = FileOps::stream_to_file(path, true);
|
||||
IAT_CHECK(writer_res.has_value());
|
||||
@ -157,9 +123,8 @@ auto test_stream_integration() -> bool {
|
||||
|
||||
(void)writer.write<u32>(0x12345678);
|
||||
(void)writer.write<u8>(0xFF);
|
||||
} // Destructor should flush/close
|
||||
}
|
||||
|
||||
// Read via StreamReader
|
||||
{
|
||||
auto reader_res = FileOps::stream_from_file(path);
|
||||
IAT_CHECK(reader_res.has_value());
|
||||
@ -167,7 +132,7 @@ auto test_stream_integration() -> bool {
|
||||
|
||||
auto val_u32 = reader.read<u32>();
|
||||
IAT_CHECK(val_u32.has_value());
|
||||
IAT_CHECK_EQ(*val_u32, 0x12345678);
|
||||
IAT_CHECK_EQ(*val_u32, 0x12345678u);
|
||||
|
||||
auto val_u8 = reader.read<u8>();
|
||||
IAT_CHECK(val_u8.has_value());
|
||||
@ -178,9 +143,6 @@ auto test_stream_integration() -> bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Registration
|
||||
// -------------------------------------------------------------------------
|
||||
IAT_BEGIN_TEST_LIST()
|
||||
IAT_ADD_TEST(test_text_io);
|
||||
IAT_ADD_TEST(test_binary_io);
|
||||
|
||||
Reference in New Issue
Block a user