diff --git a/tests/main.c b/tests/main.c index 714b09b..150ee87 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,9 +1,6 @@ -#include "unit/file.h" #include "unit/unit.h" int main(void) { - cosms_core_file_read_test(); - cosms_core_file_write_test(); - cosms_core_file_delete_test(); + COSMS_CORE_TEST_RUN() return 0; } diff --git a/tests/test.h b/tests/test.h index 5b53b4b..359d42a 100644 --- a/tests/test.h +++ b/tests/test.h @@ -1,14 +1,42 @@ -#ifndef COSMS_TEST -#define COSMS_TEST +#ifndef COSMS_CORE_TEST +#define COSMS_CORE_TEST #include #include -#define COSMS_CORE_TEST_HEADER(test_module_name) printf("----------------------------------------\n%s\n----------------------------------------\n", test_module_name); +#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) { \ + const char *cosms_core_test_name = #name; \ + printf("----------------------------------------\n%s\n----------------------------------------\n", cosms_core_test_name); \ + implementation \ +} + +#define COSMS_CORE_TEST_SUB_TEST(name, implementation) { \ + const char* cosms_core_sub_test_name = #name; \ + printf("[*] running test %s...\n", cosms_core_sub_test_name); \ + implementation \ +} + +#define COSMS_CORE_TEST_EXPORT(name, ...) static const cosms_core_test cosms_core_test_##name[] = { \ + __VA_ARGS__ \ + } + +#define COSMS_CORE_TEST_START static const 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_START(test_name) printf("[*] running test %s...\n", test_name) #define COSMS_CORE_TEST_SUCCESS(test_name) printf("[+] test %s completed successfully!\n", test_name); -#define COSMS_CORE_TEST_FAIL(test_name, error) printf("[-] test %s failed with error: %s\n", test_name, error) + +typedef void (*cosms_core_test)(); static inline bool cosms_core_test_assert(const char *test_name, bool expression, const char *error_message) { if (!expression) { diff --git a/tests/unit/file.c b/tests/unit/file.c index 9e3276e..5fdee9b 100644 --- a/tests/unit/file.c +++ b/tests/unit/file.c @@ -1,4 +1,3 @@ -#include "test.h" #include "unit/file.h" #include @@ -6,187 +5,187 @@ #include #include -void cosms_core_file_read_test(void) { - COSMS_CORE_TEST_HEADER("file read"); - - const char *current_test_name = "reading small file"; - COSMS_CORE_TEST_START(current_test_name); - - unsigned long long file_size; - char *file_buffer; - CosmsCoreFileError error = cosms_core_file_read("tests/data/small-file.txt", &file_buffer, &file_size); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - const char *expected_result = "Hello, World!"; - if (cosms_core_test_assert(current_test_name, file_size == strlen(expected_result) && strcmp(expected_result, file_buffer) == 0, "result does not match expected result")) { - COSMS_CORE_TEST_SUCCESS(current_test_name); +COSMS_CORE_TEST_TEST(file_read, + COSMS_CORE_TEST_SUB_TEST(reading small file, + unsigned long long file_size = 0; + char *file_buffer = NULL; + CosmsCoreFileError error = cosms_core_file_read("tests/data/small-file.txt", &file_buffer, &file_size); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + const char *expected_result = "Hello, World!"; + if (cosms_core_test_assert(cosms_core_sub_test_name, file_size == strlen(expected_result) && strcmp(expected_result, file_buffer) == 0, "result does not match expected result")) { + COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); + } } - } - free(file_buffer); - - current_test_name = "reading large file"; - COSMS_CORE_TEST_START(current_test_name); - - file_size = 0; - file_buffer = NULL; - error = cosms_core_file_read("tests/data/large-file.txt", &file_buffer, &file_size); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - unsigned long long expected_size = 5242880000ULL; - char *expected_result = (char*)malloc((expected_size + 1) * sizeof(char)); - if (!expected_result) { - printf("[-] failed to allocate memory for test aborting file read tests...\n"); + if (file_buffer != NULL) { free(file_buffer); + } + ) + + COSMS_CORE_TEST_SUB_TEST(reading large file, + unsigned long long file_size = 0; + char *file_buffer = NULL; + CosmsCoreFileError error = cosms_core_file_read("tests/data/large-file.txt", &file_buffer, &file_size); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + unsigned long long expected_size = 5242880000ULL; + char *expected_result = (char*)malloc((expected_size + 1) * sizeof(char)); + if (!expected_result) { + printf("[-] failed to allocate memory for test aborting file read tests...\n"); + free(file_buffer); + return; + } + + memset(expected_result, 'a', expected_size); + expected_result[expected_size] = '\0'; + + if (cosms_core_test_assert(cosms_core_sub_test_name, file_size == expected_size && strcmp(expected_result, file_buffer) == 0, "result does not match expected result")) { + COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); + } + + free(expected_result); + } + + if (file_buffer != NULL) { + free(file_buffer); + } + ) + + COSMS_CORE_TEST_SUB_TEST(reading non existing file, + unsigned long long file_size = 0; + char *file_buffer = NULL; + CosmsCoreFileError error = cosms_core_file_read("non-existing-file.cosms", &file_buffer, &file_size); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { + COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); + } + + if (file_buffer != NULL) { + free(file_buffer); + } + ) +) + +COSMS_CORE_TEST_TEST(file_write, + COSMS_CORE_TEST_SUB_TEST(writting small file, + char *file_buffer = "Hello, World!"; + CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), true); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + char *file_content = NULL; + error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + if (cosms_core_test_assert(cosms_core_sub_test_name, strcmp(file_buffer, file_content) == 0, "file content not what was written")) { + COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); + } + } + + if (file_content != NULL) { + free(file_content); + } + } + ) + + COSMS_CORE_TEST_SUB_TEST(writting big file, + unsigned long long large_file_size = 5242880000ULL; + char *large_file_buffer = (char*)malloc((large_file_size + 1) * sizeof(char)); + if (!large_file_buffer) { + printf("[-] failed to allocate memory for test aborting file read tests...\n"); + free(large_file_buffer); return; } - memset(expected_result, 'a', expected_size); - expected_result[expected_size] = '\0'; + memset(large_file_buffer, 'a', large_file_size); + large_file_buffer[large_file_size] = '\0'; - if (cosms_core_test_assert(current_test_name, file_size == expected_size && strcmp(expected_result, file_buffer) == 0, "result does not match expected result")) { - COSMS_CORE_TEST_SUCCESS(current_test_name); - } + CosmsCoreFileError error = cosms_core_file_write("tests/data/large-write-file.txt", large_file_buffer, large_file_size, true); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + char *file_content = NULL; + error = cosms_core_file_read("tests/data/large-write-file.txt", &file_content, NULL); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + if (cosms_core_test_assert(cosms_core_sub_test_name, strcmp(large_file_buffer, file_content) == 0, "file content not what was written")) { + COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); + } + } - free(expected_result); - } - - free(file_buffer); - - current_test_name = "reading non existing file"; - COSMS_CORE_TEST_START(current_test_name); - - file_size = 0; - file_buffer = NULL; - error = cosms_core_file_read("non-existing-file.cosms", &file_buffer, &file_size); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { - COSMS_CORE_TEST_SUCCESS(current_test_name); - } - - free(file_buffer); -} - -void cosms_core_file_write_test(void) { - COSMS_CORE_TEST_HEADER("file write"); - - const char *current_test_name = "writting small file"; - COSMS_CORE_TEST_START(current_test_name); - - char *file_buffer = "Hello, World!"; - CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), true); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - char *file_content; - error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - if (cosms_core_test_assert(current_test_name, strcmp(file_buffer, file_content) == 0, "file content not what was written")) { - COSMS_CORE_TEST_SUCCESS(current_test_name); + if (file_content != NULL) { + free(file_content); } } - free(file_content); - } - - current_test_name = "writting big file"; - COSMS_CORE_TEST_START(current_test_name); - - unsigned long long large_file_size = 5242880000ULL; - char *large_file_buffer = (char*)malloc((large_file_size + 1) * sizeof(char)); - if (!large_file_buffer) { - printf("[-] failed to allocate memory for test aborting file read tests...\n"); free(large_file_buffer); - return; - } + ) - memset(large_file_buffer, 'a', large_file_size); - large_file_buffer[large_file_size] = '\0'; - - error = cosms_core_file_write("tests/data/large-write-file.txt", large_file_buffer, large_file_size, true); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - char *file_content; - error = cosms_core_file_read("tests/data/large-write-file.txt", &file_content, NULL); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - if (cosms_core_test_assert(current_test_name, strcmp(large_file_buffer, file_content) == 0, "file content not what was written")) { - COSMS_CORE_TEST_SUCCESS(current_test_name); + COSMS_CORE_TEST_SUB_TEST(appending file, + char *file_buffer = "Hello, World!"; + CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), false); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + char *file_content = NULL; + error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + const char *expected_result = "Hello, World!Hello, World!"; + if (cosms_core_test_assert(cosms_core_sub_test_name, strcmp(expected_result, file_content) == 0, "file content not what was written")) { + COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); + } + } + + if (file_content != NULL) { + free(file_content); } } + ) - free(file_content); - } + COSMS_CORE_TEST_SUB_TEST(appending to non existing file, + char *file_buffer = "Hello, World!"; + CosmsCoreFileError error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer), false); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + char *file_content = NULL; + error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + if (cosms_core_test_assert(cosms_core_sub_test_name, strcmp(file_buffer, file_content) == 0, "file content not what was written")) { + COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); + } + } - free(large_file_buffer); - - current_test_name = "appending file"; - COSMS_CORE_TEST_START(current_test_name); - - error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), false); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - char *file_content; - error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - const char *expected_result = "Hello, World!Hello, World!"; - if (cosms_core_test_assert(current_test_name, strcmp(expected_result, file_content) == 0, "file content not what was written")) { - COSMS_CORE_TEST_SUCCESS(current_test_name); + if (file_content != NULL) { + free(file_content); } } - - free(file_content); - } + ) +) - current_test_name = "appending to non existing file"; - COSMS_CORE_TEST_START(current_test_name); +COSMS_CORE_TEST_TEST(file_delete, + COSMS_CORE_TEST_SUB_TEST(deleting small file, + CosmsCoreFileError error = cosms_core_file_delete("tests/data/small-write-file.txt"); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + char *file_content = NULL; + error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { + COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); + } - error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer), false); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - char *file_content; - error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - if (cosms_core_test_assert(current_test_name, strcmp(file_buffer, file_content) == 0, "file content not what was written")) { - COSMS_CORE_TEST_SUCCESS(current_test_name); + if (file_content != NULL) { + free(file_content); } } + ) - free(file_content); - } -} + COSMS_CORE_TEST_SUB_TEST(deleting large file, + CosmsCoreFileError error = cosms_core_file_delete("tests/data/append-write-file.txt"); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { + char *file_content = NULL; + error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { + COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); + } -void cosms_core_file_delete_test(void) { - COSMS_CORE_TEST_HEADER("file delete"); - - const char *current_test_name = "deleting small file"; - COSMS_CORE_TEST_START(current_test_name); - - CosmsCoreFileError error = cosms_core_file_delete("tests/data/small-write-file.txt"); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - char *file_content = NULL; - error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { - COSMS_CORE_TEST_SUCCESS(current_test_name); + if (file_content != NULL) { + free(file_content); + } } + ) - if (file_content != NULL) { - free(file_content); + COSMS_CORE_TEST_SUB_TEST(deleting non existing file, + CosmsCoreFileError error = cosms_core_file_delete("non-existing-file.cosms"); + if (cosms_core_test_assert(cosms_core_sub_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { + COSMS_CORE_TEST_SUCCESS(cosms_core_sub_test_name); } - } - - current_test_name = "deleting large file"; - COSMS_CORE_TEST_START(current_test_name); - - error = cosms_core_file_delete("tests/data/append-write-file.txt"); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_OK, cosms_core_file_error_string(error))) { - char *file_content = NULL; - error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { - COSMS_CORE_TEST_SUCCESS(current_test_name); - } - - if (file_content != NULL) { - free(file_content); - } - } - - current_test_name = "deleting non existing file"; - COSMS_CORE_TEST_START(current_test_name); - - error = cosms_core_file_delete("non-existing-file.cosms"); - if (cosms_core_test_assert(current_test_name, error == COSMS_CORE_FILE_NOT_FOUND, cosms_core_file_error_string(error))) { - COSMS_CORE_TEST_SUCCESS(current_test_name); - } -} + ) +) diff --git a/tests/unit/file.h b/tests/unit/file.h index 1077dc0..1a8b33c 100644 --- a/tests/unit/file.h +++ b/tests/unit/file.h @@ -1,10 +1,16 @@ #ifndef COSMS_CORE_FILE_TEST #define COSMS_CORE_FILE_TEST -#include +#include "test.h" -void cosms_core_file_read_test(); -void cosms_core_file_write_test(); -void cosms_core_file_delete_test(); +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_read), + COSMS_CORE_TEST_FUNCTION_NAME(file_write), + COSMS_CORE_TEST_FUNCTION_NAME(file_delete) +); #endif \ No newline at end of file diff --git a/tests/unit/unit.h b/tests/unit/unit.h index 153c7cc..6079d59 100644 --- a/tests/unit/unit.h +++ b/tests/unit/unit.h @@ -1,6 +1,11 @@ #ifndef COSMS_UNIT #define COSMS_UNIT +#include "test.h" #include "file.h" +COSMS_CORE_TEST_START + COSMS_CORE_TEST_IMPORT(file) +COSMS_CORE_TEST_END + #endif \ No newline at end of file