refactor(tests): improved test structure to be easier to use

This commit is contained in:
Mineplay5780 2025-12-18 16:12:23 +01:00
parent cf4755145b
commit a74bb3cb45
3 changed files with 283 additions and 228 deletions

View file

@ -2,42 +2,44 @@
#define COSMS_CORE_TEST
#include <stdio.h>
#include <stdbool.h>
#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); \
#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 \
}
#define COSMS_CORE_TEST_SUB_TEST(name, implementation) { \
const char *cosms_core_test_sub_test_error = NULL; \
printf("[*] running test %s...\n", #name); \
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](); \
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

View file

@ -11,220 +11,245 @@
#include <cosms-core/file.h>
#include <string.h>
#include <stdlib.h>
COSMS_CORE_TEST_TEST(file_open,
COSMS_CORE_TEST_SUB_TEST(opening small file for reading,
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) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
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);
return cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(opening large file for reading,
return NULL;
)
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) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
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);
return 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);
}
}
)
return NULL;
)
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,
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_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 cosms_core_file_error_string(error);
}
return 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_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,
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 {
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);
return cosms_core_file_error_string(error);
}
}
)
COSMS_CORE_TEST_SUB_TEST(closing non-existing file,
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) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
return cosms_core_file_error_string(error);
}
)
return NULL;
)
COSMS_CORE_TEST_TEST(file_size,
COSMS_CORE_TEST_SUB_TEST(reading small file size,
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) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
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) {
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";
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) {
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,
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) {
cosms_core_test_sub_test_error = cosms_core_file_error_string(error);
} else {
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) {
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);
}
if (size != 5242880000ULL) {
return "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 non-existing file size,
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);
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;
)

View file

@ -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