From d98cb6c4ba0ba5775c2fdb3dfa245eb08b4b829d Mon Sep 17 00:00:00 2001 From: Mineplay5780 Date: Thu, 18 Dec 2025 17:03:38 +0100 Subject: [PATCH] feat(file): implemented read file tests --- include/cosms-core/file.h | 2 +- src/file.c | 6 +- tests/unit/file.c | 162 +++++++++++++++++++++++++++++++++++++- tests/unit/file.h | 16 +++- 4 files changed, 179 insertions(+), 7 deletions(-) diff --git a/include/cosms-core/file.h b/include/cosms-core/file.h index ef6a508..52e5c0e 100644 --- a/include/cosms-core/file.h +++ b/include/cosms-core/file.h @@ -50,7 +50,7 @@ CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file); CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size); -CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char **buffer, unsigned int bytes_to_read, unsigned int *bytes_read); +CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read); CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read); CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written); diff --git a/src/file.c b/src/file.c index 8be8ac1..4cf538f 100644 --- a/src/file.c +++ b/src/file.c @@ -164,13 +164,13 @@ CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsign return COSMS_CORE_FILE_OK; } -CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char **buffer, unsigned int bytes_to_read, unsigned int *bytes_read) { +CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read) { if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) { return COSMS_CORE_FILE_INVALID_OPERATION; } #if defined(__GNUC__) - int read_bytes = read(file->native_file, *buffer, bytes_to_read); + int read_bytes = read(file->native_file, buffer, bytes_to_read); if (read_bytes == -1) { return COSMS_CORE_FILE_FAILED_TO_READ; } @@ -179,7 +179,7 @@ CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char **buf (*bytes_read) = read_bytes; } #elif defined(_WIN32) - if (ReadFile(file->native_file, *buffer, bytes_to_read, (LPDWORD)bytes_read, NULL) == 0) { + if (ReadFile(file->native_file, buffer, bytes_to_read, (LPDWORD)bytes_read, NULL) == 0) { return COSMS_CORE_FILE_FAILED_TO_READ; } #endif diff --git a/tests/unit/file.c b/tests/unit/file.c index f8834da..9a1b1b1 100644 --- a/tests/unit/file.c +++ b/tests/unit/file.c @@ -242,7 +242,167 @@ COSMS_CORE_TEST_TEST(file_size_non_existing_file, return NULL; ) -COSMS_CORE_TEST_TEST(file_read, +COSMS_CORE_TEST_TEST(file_read_small_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) { + return cosms_core_file_error_string(error); + } + + unsigned int bytes_to_read = 13; + unsigned int bytes_read = 0; + + char buffer[14]; + error = cosms_core_file_read(&file, buffer, bytes_to_read, &bytes_read); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + if (bytes_to_read != bytes_read || strcmp(buffer, "Hello, World!") != 0) { + return "result does not match expected result"; + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_read_large_file, + 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) { + return cosms_core_file_error_string(error); + } + + unsigned long long bytes_to_read = 5242880000ULL; + unsigned long long bytes_read = 0; + + char *buffer = NULL; + error = cosms_core_file_read_all(&file, &buffer, &bytes_read); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + char *expected_result = (char*)malloc((bytes_to_read + 1) * sizeof(char)); + if (expected_result == NULL) { + free(buffer); + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + return "Failed to allocate memory"; + } + + for (unsigned long long index = 0; index < bytes_to_read; index += 1) { + expected_result[index] = 'a'; + } + + expected_result[bytes_to_read] = '\0'; + if (bytes_to_read != bytes_read || strcmp(buffer, expected_result) != 0) { + free(expected_result); + free(buffer); + + return "result does not match expected result"; + } + + free(expected_result); + free(buffer); + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_read_non_existing_file, + struct cosms_core_file file; + file.mode = COSMS_CORE_FILE_MODE_READ; + file.native_file = 0; + + unsigned int bytes_to_read = 13; + unsigned int bytes_read = 0; + + char buffer[14]; + CosmsCoreFileError error = cosms_core_file_read(&file, buffer, bytes_to_read, &bytes_read); + if (error != COSMS_CORE_FILE_FAILED_TO_READ) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_read_full_non_existing_file, + struct cosms_core_file file; + file.mode = COSMS_CORE_FILE_MODE_READ; + file.native_file = 0; + + unsigned long long bytes_read = 0; + char *buffer = NULL; + CosmsCoreFileError error = cosms_core_file_read_all(&file, &buffer, &bytes_read); + if (error != COSMS_CORE_FILE_COULD_NOT_READ_SIZE) { + if (error == COSMS_CORE_FILE_OK) { + free(buffer); + } + + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_read_file_using_wrong_mode, + 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) { + return cosms_core_file_error_string(error); + } + + unsigned int bytes_to_read = 13; + unsigned int bytes_read = 0; + + char buffer[14]; + error = cosms_core_file_read(&file, buffer, bytes_to_read, &bytes_read); + if (error != COSMS_CORE_FILE_INVALID_OPERATION) { + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_read_full_file_using_wrong_mode, + 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) { + return cosms_core_file_error_string(error); + } + + unsigned long long bytes_read = 0; + char *buffer = NULL; + error = cosms_core_file_read_all(&file, &buffer, &bytes_read); + if (error != COSMS_CORE_FILE_INVALID_OPERATION) { + if (error == COSMS_CORE_FILE_OK) { + free(buffer); + } + + return cosms_core_file_error_string(error); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + return NULL; ) diff --git a/tests/unit/file.h b/tests/unit/file.h index 2bee9cf..9c662a7 100644 --- a/tests/unit/file.h +++ b/tests/unit/file.h @@ -28,7 +28,13 @@ COSMS_CORE_TEST_DEFINE(file_size_small_file); COSMS_CORE_TEST_DEFINE(file_size_large); COSMS_CORE_TEST_DEFINE(file_size_non_existing_file); -COSMS_CORE_TEST_DEFINE(file_read); +COSMS_CORE_TEST_DEFINE(file_read_small_file); +COSMS_CORE_TEST_DEFINE(file_read_large_file); +COSMS_CORE_TEST_DEFINE(file_read_non_existing_file); +COSMS_CORE_TEST_DEFINE(file_read_full_non_existing_file); +COSMS_CORE_TEST_DEFINE(file_read_file_using_wrong_mode); +COSMS_CORE_TEST_DEFINE(file_read_full_file_using_wrong_mode); + COSMS_CORE_TEST_DEFINE(file_write); COSMS_CORE_TEST_DEFINE(file_delete); @@ -50,7 +56,13 @@ COSMS_CORE_TEST_EXPORT(file, COSMS_CORE_TEST_EXPORT_TEST(file_size_large), COSMS_CORE_TEST_EXPORT_TEST(file_size_non_existing_file), - COSMS_CORE_TEST_EXPORT_TEST(file_read), + COSMS_CORE_TEST_EXPORT_TEST(file_read_small_file), + COSMS_CORE_TEST_EXPORT_TEST(file_read_large_file), + COSMS_CORE_TEST_EXPORT_TEST(file_read_non_existing_file), + COSMS_CORE_TEST_EXPORT_TEST(file_read_full_non_existing_file), + COSMS_CORE_TEST_EXPORT_TEST(file_read_file_using_wrong_mode), + COSMS_CORE_TEST_EXPORT_TEST(file_read_full_file_using_wrong_mode), + COSMS_CORE_TEST_EXPORT_TEST(file_write), COSMS_CORE_TEST_EXPORT_TEST(file_delete) );