Created Initial Attempt Testing Framework

The framework, completely contained in x_tfw4c.c, uses pre-compiler
MACROS to create an environment where it is easy to create unit tests
and run regression tests.

It iterates through a linked list of test structs that contain the test
function that should be executed and the name of the test to be
displayed.

This is a simple rework of a prior test framework that did not
dynamically add new tests at compile time.

Next steps would be to have it display more metadata and have some
resilience to crashes in the tests.
This commit is contained in:
xavi 2025-12-20 12:16:12 -08:00
parent 4320c26105
commit a490341129

View File

@ -0,0 +1,83 @@
#include <stdio.h>
// printf Formating
#define X_RED "\x1B[31m"
#define X_GREEN "\x1B[32m"
#define X_RST "\x1B[0m"
// TEST FRAMEWORK STRUCT
typedef struct testnode{
char* name;
int (*op)(void);
struct testnode* next;
}testnode;
// LL nodes
static testnode* head = NULL;
static testnode* tail = NULL;
static testnode* ptr = NULL;
// TEST FRAMEWORK MACRO
// create a new node and pass in the name of the test. Then make a new function
// that adds the test to the LL. __attribute__((constructor)) has the function
// run before main
#define TEST(UNIT)\
static int UNIT(void);\
static testnode UNIT##node = {#UNIT, UNIT, NULL};\
__attribute__((constructor)) \
static void add##UNIT(void){ \
if (head == NULL){\
head = &UNIT##node;\
tail = head;\
}\
ptr = tail;\
tail = &UNIT##node;\
ptr->next = tail;\
}\
static int UNIT(void)
// DEFINE TESTS
TEST(fail){
return -1;
}
TEST(success){
return 0;
}
// END OF TEST DEFINITIONS
int main(){
int total_tests = 0;
int errors = 0;
ptr = head;
while (ptr != NULL){
total_tests++;
if(!ptr->op()){
printf("%s[PASS]%s ", X_GREEN, X_RST);
}
else{
printf("%s[FAIL]%s ", X_RED, X_RST);
errors++;
}
printf("%s\n", ptr->name);
ptr = ptr->next;
}
printf("Tests completed with %s%d PASS%s", X_GREEN, total_tests - errors, X_RST);
printf(" and %s%d FAIL%s\n", X_RED, errors, X_RST);
return 0;
}
// INSERT HEADERS FOR FUNCS
// DEFINE TESTS
// END OF TEST DEFINITIONS