2025-12-17 23:45:55 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
|
|
|
|
|
*
|
|
|
|
|
* This software is 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 in the file LICENSE or at
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*/
|
2025-12-10 20:39:31 +01:00
|
|
|
#include "unit/file.h"
|
|
|
|
|
|
|
|
|
|
#include <cosms-core/file.h>
|
2025-12-09 22:07:16 +01:00
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2025-12-17 21:16:00 +01:00
|
|
|
COSMS_CORE_TEST_TEST(file_open,
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening small file for reading,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening large file for reading,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_READ);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening small file for writting,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_WRITE);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening large file for writting,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_WRITE);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening small file for appending,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_APPEND);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening large file for appending,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_APPEND);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening small file for reading and writting,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening large file for reading and writting,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_READ | COSMS_CORE_FILE_MODE_WRITE);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-12-17 23:00:42 +01:00
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(opening non-existing file for reading,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "non-existing-file.cosms", COSMS_CORE_FILE_MODE_READ);
|
|
|
|
|
if (error != COSMS_CORE_FILE_NOT_FOUND) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
if (error == COSMS_CORE_FILE_OK) {
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-12-17 21:16:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_TEST(file_close,
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(closing file,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(closing non-existing file,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
file.native_file = 0;
|
|
|
|
|
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_INVALID_FILE) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_TEST(file_size,
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(reading small file size,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/small-file.txt", COSMS_CORE_FILE_MODE_READ);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
unsigned long long size = 0;
|
|
|
|
|
error = cosms_core_file_get_size(&file, &size);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
if (size != 13) {
|
|
|
|
|
cosms_core_test_sub_test_error = "result does not match expected result";
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-17 22:58:02 +01:00
|
|
|
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
2025-12-17 21:16:00 +01:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(reading large file size,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_open(&file, "tests/data/large-file.txt", COSMS_CORE_FILE_MODE_READ);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
unsigned long long size = 0;
|
|
|
|
|
error = cosms_core_file_get_size(&file, &size);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
} else {
|
|
|
|
|
if (size != 5242880000ULL) {
|
|
|
|
|
cosms_core_test_sub_test_error = "result does not match expected result";
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-17 22:58:02 +01:00
|
|
|
|
|
|
|
|
error = cosms_core_file_close(&file);
|
|
|
|
|
if (error != COSMS_CORE_FILE_OK) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
2025-12-17 21:16:00 +01:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_SUB_TEST(reading non-existing file size,
|
|
|
|
|
struct cosms_core_file file;
|
|
|
|
|
file.native_file = 0;
|
|
|
|
|
|
|
|
|
|
unsigned long long size = 0;
|
|
|
|
|
CosmsCoreFileError error = cosms_core_file_get_size(&file, &size);
|
|
|
|
|
if (error != COSMS_CORE_FILE_INVALID_FILE) {
|
|
|
|
|
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-14 17:58:09 +01:00
|
|
|
COSMS_CORE_TEST_TEST(file_read,
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_TEST(file_write,
|
2025-12-17 23:45:55 +01:00
|
|
|
|
2025-12-14 17:58:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
COSMS_CORE_TEST_TEST(file_delete,
|
2025-12-17 23:45:55 +01:00
|
|
|
|
2025-12-14 17:58:09 +01:00
|
|
|
)
|