diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b179b1..bd6b4d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,8 @@ else() target_compile_options(cosms-core PRIVATE -Wall -Wextra -pedantic) endif() -add_executable(cosms-core-test ${PROJECT_SOURCE_DIR}/tests/main.c) +file(GLOB_RECURSE SRC_TEST_FILES "${PROJECT_SOURCE_DIR}/tests/*.c") +add_executable(cosms-core-test ${SRC_TEST_FILES}) target_include_directories(cosms-core-test PRIVATE ${PROJECT_SOURCE_DIR}/tests) target_link_libraries(cosms-core-test cosms-core) diff --git a/tests/data/large-file.txt b/tests/data/large-file.txt new file mode 100644 index 0000000..d4c5011 Binary files /dev/null and b/tests/data/large-file.txt differ diff --git a/tests/data/small-file.txt b/tests/data/small-file.txt new file mode 100644 index 0000000..b45ef6f --- /dev/null +++ b/tests/data/small-file.txt @@ -0,0 +1 @@ +Hello, World! \ No newline at end of file diff --git a/tests/main.c b/tests/main.c index f6b1706..b348f4f 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,22 +1,6 @@ -#include - -#include +#include "unit/unit.h" int main(void) { - unsigned long long buffer_size; - char *buffer; - CosmsFileError error = cosms_core_file_read("test.txt", &buffer, &buffer_size); - if (error != COSMS_FILE_OK) { - fprintf(stderr, "error(%d): %s\n", error, cosms_core_file_error_string(error)); - } else { - printf("file read with content:\n%s\n", buffer); - } - - char *temp_buffer = "Hello, World!"; - error = cosms_core_file_write("write-file.txt", temp_buffer, 13, true); - if (error != COSMS_FILE_OK) { - fprintf(stderr, "error(%d): %s\n", error, cosms_core_file_error_string(error)); - } - + cosms_file_read_test(); return 0; } diff --git a/tests/test.h b/tests/test.h new file mode 100644 index 0000000..a1a8ca1 --- /dev/null +++ b/tests/test.h @@ -0,0 +1,12 @@ +#ifndef COSMS_TEST +#define COSMS_TEST + +#include + +#define COSMS_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) + +#endif \ No newline at end of file diff --git a/tests/unit/file.c b/tests/unit/file.c new file mode 100644 index 0000000..1052bb1 --- /dev/null +++ b/tests/unit/file.c @@ -0,0 +1,75 @@ +#include "file.h" +#include "../test.h" +//#include +#include "../../include/cosms-core/file.h" + +#include +#include + +void cosms_file_read_test() { + COSMS_TEST_HEADER("file read"); + + const char *current_test_name = "reading small file"; + COSMS_TEST_START(current_test_name); + + unsigned long long file_size; + char *file_buffer; + CosmsFileError error = cosms_core_file_read("tests/data/small-file.txt", &file_buffer, &file_size); + if (error != COSMS_FILE_OK) { + COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + } else { + 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); + } + } + + free(file_buffer); + + current_test_name = "reading large file"; + COSMS_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_FILE_OK) { + COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + } else { + 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 (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); + } + + free(expected_result); + } + + free(file_buffer); + + current_test_name = "reading non existing file"; + COSMS_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_FILE_NOT_FOUND) { + COSMS_TEST_SUCCESS(current_test_name); + } else { + COSMS_TEST_FAIL(current_test_name, cosms_core_file_error_string(error)); + } + + free(file_buffer); +} \ No newline at end of file diff --git a/tests/unit/file.h b/tests/unit/file.h new file mode 100644 index 0000000..da53018 --- /dev/null +++ b/tests/unit/file.h @@ -0,0 +1,8 @@ +#ifndef COSMS_FILE_TEST +#define COSMS_FILE_TEST + +#include + +void cosms_file_read_test(); + +#endif \ No newline at end of file diff --git a/tests/unit/unit.h b/tests/unit/unit.h new file mode 100644 index 0000000..153c7cc --- /dev/null +++ b/tests/unit/unit.h @@ -0,0 +1,6 @@ +#ifndef COSMS_UNIT +#define COSMS_UNIT + +#include "file.h" + +#endif \ No newline at end of file