From a74bb3cb45144feafa7b262fd7472e6dd464aedd Mon Sep 17 00:00:00 2001 From: Mineplay5780 Date: Thu, 18 Dec 2025 16:12:23 +0100 Subject: [PATCH] refactor(tests): improved test structure to be easier to use --- tests/test.h | 54 +++--- tests/unit/file.c | 413 ++++++++++++++++++++++++---------------------- tests/unit/file.h | 44 ++++- 3 files changed, 283 insertions(+), 228 deletions(-) diff --git a/tests/test.h b/tests/test.h index 5e9af19..d740e10 100644 --- a/tests/test.h +++ b/tests/test.h @@ -2,42 +2,44 @@ #define COSMS_CORE_TEST #include -#include -#define COSMS_CORE_TEST_FUNCTION_NAME(name) cosms_core_##name##_test -#define COSMS_CORE_TEST_DEFINE(name) void cosms_core_##name##_test(void) - -#define COSMS_CORE_TEST_TEST(name, implementation) void cosms_core_##name##_test(void) { \ - printf("----------------------------------------\n%s\n----------------------------------------\n", #name); \ - implementation \ -} - -#define COSMS_CORE_TEST_SUB_TEST(name, implementation) { \ - const char *cosms_core_test_sub_test_error = NULL; \ - printf("[*] running test %s...\n", #name); \ +#define COSMS_CORE_TEST_EXPORT_TEST(name) { #name, cosms_core_##name##_test } +#define COSMS_CORE_TEST_DEFINE(name) const char *cosms_core_##name##_test(void) +#define COSMS_CORE_TEST_TEST(name, implementation) const char *cosms_core_##name##_test(void) { \ implementation \ - if (cosms_core_test_sub_test_error == NULL) { \ - printf("[+] test %s completed successfully!\n", #name); \ - } else { \ - printf("[-] test %s failed with error: %s\n", #name, cosms_core_test_sub_test_error); \ - } \ } -#define COSMS_CORE_TEST_EXPORT(name, ...) static const cosms_core_test cosms_core_test_##name[] = { \ - __VA_ARGS__ \ +#define COSMS_CORE_TEST_EXPORT(name, ...) static const struct cosms_core_test cosms_core_test_##name[] = { \ + __VA_ARGS__, \ + 0 \ } -#define COSMS_CORE_TEST_START static const cosms_core_test *cosms_core_test_tests[] = { +#define COSMS_CORE_TEST_START static const struct cosms_core_test *cosms_core_test_tests[] = { #define COSMS_CORE_TEST_IMPORT(name) cosms_core_test_##name #define COSMS_CORE_TEST_END }; -#define COSMS_CORE_TEST_RUN() \ - for (unsigned long long index = 0; index < sizeof(cosms_core_test_tests); index += 1) { \ - for (unsigned long long test_index = 0; test_index < sizeof(cosms_core_test_tests[index]); test_index += 1) { \ - cosms_core_test_tests[index][test_index](); \ - } \ +#define COSMS_CORE_TEST_RUN() \ + for (unsigned long long index = 0; index < sizeof(cosms_core_test_tests) / sizeof(cosms_core_test_tests[0]); index += 1) { \ + unsigned long long test_index = 0; \ + struct cosms_core_test current_test = cosms_core_test_tests[index][test_index]; \ + while (current_test.name != 0) { \ + printf("[*] running test %s...", current_test.name); \ + fflush(stdout); \ + const char *result = current_test.function(); \ + if (result == NULL) { \ + printf("\r[PASS] test %s completed successfully!\n", current_test.name); \ + } else { \ + printf("\r[FAIL] test %s failed with error: %s\n", current_test.name, result); \ + } \ + \ + test_index += 1; \ + current_test = cosms_core_test_tests[index][test_index]; \ + } \ } -typedef void (*cosms_core_test)(); +struct cosms_core_test { + const char *name; + const char *(*function)(void); +}; #endif diff --git a/tests/unit/file.c b/tests/unit/file.c index 8b8fadd..f8834da 100644 --- a/tests/unit/file.c +++ b/tests/unit/file.c @@ -11,220 +11,245 @@ #include #include -#include -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_TEST(file_open_small_file_read, + 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); + } + + error = cosms_core_file_close(&file); + if (error != COSMS_CORE_FILE_OK) { + return 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); - } - } - ) - - 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); - } - } - } - ) + return NULL; ) -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_TEST(file_open_large_file_read, + 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); + } - 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); - } - ) + 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_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); +COSMS_CORE_TEST_TEST(file_open_non_existing_file_read, + 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_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); - } 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"; - } - } - - error = cosms_core_file_close(&file); - if (error != COSMS_CORE_FILE_OK) { - cosms_core_test_sub_test_error = cosms_core_file_error_string(error); - } + return cosms_core_file_error_string(error); } - ) - 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"; - } - } + return cosms_core_file_error_string(error); + } - error = cosms_core_file_close(&file); - if (error != COSMS_CORE_FILE_OK) { - cosms_core_test_sub_test_error = cosms_core_file_error_string(error); - } - } - ) + if (error != COSMS_CORE_FILE_NOT_FOUND) { + return cosms_core_file_error_string(error); + } + + return NULL; +) - COSMS_CORE_TEST_SUB_TEST(reading non-existing file size, - struct cosms_core_file file; - file.native_file = 0; +COSMS_CORE_TEST_TEST(file_open_small_file_write, + 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) { + 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_open_large_file_write, + 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) { + 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_open_small_file_append, + 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); + } + + 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_open_large_file_append, + 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) { + 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_open_small_file_read_write, + 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) { + 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_open_large_file_read_write, + 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) { + 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_close, + 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); + } + + 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_close_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) { + return cosms_core_file_error_string(error); + } + + return NULL; +) + +COSMS_CORE_TEST_TEST(file_size_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 long long size = 0; + error = cosms_core_file_get_size(&file, &size); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + if (size != 13) { + 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_size_large, + 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 size = 0; + error = cosms_core_file_get_size(&file, &size); + if (error != COSMS_CORE_FILE_OK) { + return cosms_core_file_error_string(error); + } + + if (size != 5242880000ULL) { + 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_size_non_existing_file, + 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); - } - ) + unsigned long long size = 0; + CosmsCoreFileError error = cosms_core_file_get_size(&file, &size); + if (error != COSMS_CORE_FILE_INVALID_FILE) { + return cosms_core_file_error_string(error); + } + + return NULL; ) COSMS_CORE_TEST_TEST(file_read, - + return NULL; ) COSMS_CORE_TEST_TEST(file_write, - + return NULL; ) COSMS_CORE_TEST_TEST(file_delete, - + return NULL; ) diff --git a/tests/unit/file.h b/tests/unit/file.h index d33929d..2bee9cf 100644 --- a/tests/unit/file.h +++ b/tests/unit/file.h @@ -11,20 +11,48 @@ #include "test.h" -COSMS_CORE_TEST_DEFINE(file_open); +COSMS_CORE_TEST_DEFINE(file_open_small_file_read); +COSMS_CORE_TEST_DEFINE(file_open_large_file_read); +COSMS_CORE_TEST_DEFINE(file_open_non_existing_file_read); +COSMS_CORE_TEST_DEFINE(file_open_small_file_write); +COSMS_CORE_TEST_DEFINE(file_open_large_file_write); +COSMS_CORE_TEST_DEFINE(file_open_small_file_append); +COSMS_CORE_TEST_DEFINE(file_open_large_file_append); +COSMS_CORE_TEST_DEFINE(file_open_small_file_read_write); +COSMS_CORE_TEST_DEFINE(file_open_large_file_read_write); + COSMS_CORE_TEST_DEFINE(file_close); -COSMS_CORE_TEST_DEFINE(file_size); +COSMS_CORE_TEST_DEFINE(file_close_non_existing_file); + +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_write); COSMS_CORE_TEST_DEFINE(file_delete); COSMS_CORE_TEST_EXPORT(file, - COSMS_CORE_TEST_FUNCTION_NAME(file_open), - COSMS_CORE_TEST_FUNCTION_NAME(file_close), - COSMS_CORE_TEST_FUNCTION_NAME(file_size), - COSMS_CORE_TEST_FUNCTION_NAME(file_read), - COSMS_CORE_TEST_FUNCTION_NAME(file_write), - COSMS_CORE_TEST_FUNCTION_NAME(file_delete) + COSMS_CORE_TEST_EXPORT_TEST(file_open_small_file_read), + COSMS_CORE_TEST_EXPORT_TEST(file_open_large_file_read), + COSMS_CORE_TEST_EXPORT_TEST(file_open_non_existing_file_read), + COSMS_CORE_TEST_EXPORT_TEST(file_open_small_file_write), + COSMS_CORE_TEST_EXPORT_TEST(file_open_large_file_write), + COSMS_CORE_TEST_EXPORT_TEST(file_open_small_file_append), + COSMS_CORE_TEST_EXPORT_TEST(file_open_large_file_append), + COSMS_CORE_TEST_EXPORT_TEST(file_open_small_file_read_write), + COSMS_CORE_TEST_EXPORT_TEST(file_open_large_file_read_write), + + COSMS_CORE_TEST_EXPORT_TEST(file_close), + COSMS_CORE_TEST_EXPORT_TEST(file_close_non_existing_file), + + COSMS_CORE_TEST_EXPORT_TEST(file_size_small_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_write), + COSMS_CORE_TEST_EXPORT_TEST(file_delete) ); #endif \ No newline at end of file