From a49034112950a8a6136482b3c556c5dbade1414f Mon Sep 17 00:00:00 2001 From: xavi Date: Sat, 20 Dec 2025 12:16:12 -0800 Subject: [PATCH] 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. --- c_projects/x_tfw4c/src/x_tfw4c.c | 83 ++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 c_projects/x_tfw4c/src/x_tfw4c.c diff --git a/c_projects/x_tfw4c/src/x_tfw4c.c b/c_projects/x_tfw4c/src/x_tfw4c.c new file mode 100644 index 0000000..290ad7e --- /dev/null +++ b/c_projects/x_tfw4c/src/x_tfw4c.c @@ -0,0 +1,83 @@ +#include + +// 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