diff --git a/tests/data/append-write-file.txt b/tests/data/append-write-file.txt new file mode 100644 index 0000000..b45ef6f --- /dev/null +++ b/tests/data/append-write-file.txt @@ -0,0 +1 @@ +Hello, World! \ No newline at end of file diff --git a/tests/data/large-write-file.txt b/tests/data/large-write-file.txt new file mode 100644 index 0000000..d4c5011 Binary files /dev/null and b/tests/data/large-write-file.txt differ diff --git a/tests/data/small-write-file.txt b/tests/data/small-write-file.txt new file mode 100644 index 0000000..2dd9088 --- /dev/null +++ b/tests/data/small-write-file.txt @@ -0,0 +1 @@ +Hello, World!Hello, World! \ No newline at end of file diff --git a/tests/test.h b/tests/test.h index a1a8ca1..9827c6a 100644 --- a/tests/test.h +++ b/tests/test.h @@ -2,11 +2,21 @@ #define COSMS_TEST #include +#include -#define COSMS_TEST_HEADER(test_module_name) printf("----------------------------------------\n%s\n----------------------------------------\n", test_module_name); +#define COSMS_CORE_TEST_HEADER(test_module_name) printf("----------------------------------------\n%s\n----------------------------------------\n", test_module_name); -#define COSMS_TEST_START(test_name) printf("[*] running test %s...\n", test_name) -#define COSMS_TEST_SUCCESS(test_name) printf("[+] test %s completed successfully!\n", test_name); -#define COSMS_TEST_FAIL(test_name, error) printf("[-] test %s failed with error: %s\n", test_name, error) +#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) + +inline bool cosms_core_test_assert(const char *test_name, bool expression, const char *error_message) { + if (!expression) { + printf("[-] test %s failed with error: %s\n", test_name, error_message); + return false; + } + + return true; +} #endif \ No newline at end of file diff --git a/tests/unit/file.c b/tests/unit/file.c index ba305b8..1f64993 100644 --- a/tests/unit/file.c +++ b/tests/unit/file.c @@ -7,36 +7,30 @@ #include void cosms_core_file_read_test() { - COSMS_TEST_HEADER("file read"); + COSMS_CORE_TEST_HEADER("file read"); const char *current_test_name = "reading small file"; - COSMS_TEST_START(current_test_name); + 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 (error != COSMS_CORE_FILE_OK) { - COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); - } else { + 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 (file_size != strlen(expected_result) || strcmp(expected_result, file_buffer)) { - COSMS_TEST_FAIL(current_test_name, "result does not match expected result"); - } else { - COSMS_TEST_SUCCESS(current_test_name); + 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); } } free(file_buffer); current_test_name = "reading large file"; - COSMS_TEST_START(current_test_name); + 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 (error != COSMS_CORE_FILE_OK) { - COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); - } else { + 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) { @@ -48,10 +42,8 @@ void cosms_core_file_read_test() { memset(expected_result, 'a', expected_size); expected_result[expected_size] = '\0'; - if (file_size != expected_size || strcmp(expected_result, file_buffer)) { - COSMS_TEST_FAIL(current_test_name, "result does not match expected result"); - } else { - COSMS_TEST_SUCCESS(current_test_name); + 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); } free(expected_result); @@ -60,40 +52,32 @@ void cosms_core_file_read_test() { free(file_buffer); current_test_name = "reading non existing file"; - COSMS_TEST_START(current_test_name); + 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 (error == COSMS_CORE_FILE_NOT_FOUND) { - COSMS_TEST_SUCCESS(current_test_name); - } else { - COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + 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() { - COSMS_TEST_HEADER("file write"); + COSMS_CORE_TEST_HEADER("file write"); const char *current_test_name = "writting small file"; - COSMS_TEST_START(current_test_name); + 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) - 1, true); - if (error != COSMS_CORE_FILE_OK) { - COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); - } else { + 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 (error != COSMS_CORE_FILE_OK) { - COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); - } else { - if (strcmp(file_buffer, file_content)) { - COSMS_TEST_SUCCESS(current_test_name); - } else { - COSMS_TEST_FAIL(current_test_name, "file content not what was written"); + 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); } } @@ -101,7 +85,7 @@ void cosms_core_file_write_test() { } current_test_name = "writting big file"; - COSMS_TEST_START(current_test_name); + 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)); @@ -115,18 +99,12 @@ void cosms_core_file_write_test() { 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 (error != COSMS_CORE_FILE_OK) { - COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); - } else { + 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 (error != COSMS_CORE_FILE_OK) { - COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); - } else { - if (strcmp(file_buffer, file_content)) { - COSMS_TEST_SUCCESS(current_test_name); - } else { - COSMS_TEST_FAIL(current_test_name, "file content not what was written"); + 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); } } @@ -136,44 +114,32 @@ void cosms_core_file_write_test() { free(large_file_buffer); current_test_name = "appending file"; - COSMS_TEST_START(current_test_name); + COSMS_CORE_TEST_START(current_test_name); - error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer) - 1, false); - if (error != COSMS_CORE_FILE_OK) { - COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); - } else { + 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 (error != COSMS_CORE_FILE_OK) { - COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); - } else { + 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 (strcmp(expected_result, file_content)) { - COSMS_TEST_SUCCESS(current_test_name); - } else { - COSMS_TEST_FAIL(current_test_name, "file content not what was written"); + 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); } } - + free(file_content); } current_test_name = "appending to non existing file"; - COSMS_TEST_START(current_test_name); + COSMS_CORE_TEST_START(current_test_name); - error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer) - 1, false); - if (error != COSMS_CORE_FILE_OK) { - COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); - } else { + 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/small-write-file.txt", &file_content, NULL); - if (error != COSMS_CORE_FILE_OK) { - COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); - } else { - if (strcmp(file_buffer, file_content)) { - COSMS_TEST_SUCCESS(current_test_name); - } else { - COSMS_TEST_FAIL(current_test_name, "file content not what was written"); + 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); } }