feat(test): improved testing system and implemented read file tests

This commit is contained in:
Mineplay5780 2025-12-09 22:07:16 +01:00
parent 87fb7556c3
commit ae9b1ed2b9
8 changed files with 106 additions and 19 deletions

View file

@ -14,7 +14,8 @@ else()
target_compile_options(cosms-core PRIVATE -Wall -Wextra -pedantic) target_compile_options(cosms-core PRIVATE -Wall -Wextra -pedantic)
endif() 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_include_directories(cosms-core-test PRIVATE ${PROJECT_SOURCE_DIR}/tests)
target_link_libraries(cosms-core-test cosms-core) target_link_libraries(cosms-core-test cosms-core)

BIN
tests/data/large-file.txt Normal file

Binary file not shown.

View file

@ -0,0 +1 @@
Hello, World!

View file

@ -1,22 +1,6 @@
#include <cosms-core/file.h> #include "unit/unit.h"
#include <stdio.h>
int main(void) { int main(void) {
unsigned long long buffer_size; cosms_file_read_test();
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));
}
return 0; return 0;
} }

12
tests/test.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef COSMS_TEST
#define COSMS_TEST
#include <stdio.h>
#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

75
tests/unit/file.c Normal file
View file

@ -0,0 +1,75 @@
#include "file.h"
#include "../test.h"
//#include <cosms-core/file.h>
#include "../../include/cosms-core/file.h"
#include <string.h>
#include <stdlib.h>
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);
}

8
tests/unit/file.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef COSMS_FILE_TEST
#define COSMS_FILE_TEST
#include <stdbool.h>
void cosms_file_read_test();
#endif

6
tests/unit/unit.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef COSMS_UNIT
#define COSMS_UNIT
#include "file.h"
#endif