diff --git a/c_projects/x_tfw4c/src/x_tfw4c.c b/c_projects/x_tfw4c/src/x_tfw4c.c index 290ad7e..179f5f3 100644 --- a/c_projects/x_tfw4c/src/x_tfw4c.c +++ b/c_projects/x_tfw4c/src/x_tfw4c.c @@ -1,8 +1,12 @@ #include +#include +#include +#include // printf Formating #define X_RED "\x1B[31m" #define X_GREEN "\x1B[32m" +#define X_YELLOW "\x1B[33m" #define X_RST "\x1B[0m" // TEST FRAMEWORK STRUCT @@ -29,6 +33,7 @@ static testnode* ptr = NULL; if (head == NULL){\ head = &UNIT##node;\ tail = head;\ + return; \ }\ ptr = tail;\ tail = &UNIT##node;\ @@ -36,6 +41,9 @@ static testnode* ptr = NULL; }\ static int UNIT(void) +// ASSERT MACRO +#define ASSERT() + // DEFINE TESTS TEST(fail){ return -1; @@ -44,34 +52,68 @@ TEST(fail){ TEST(success){ return 0; } + +TEST(segfault){ + int* x = NULL; + int y = *x; +} // END OF TEST DEFINITIONS int main(){ int total_tests = 0; int errors = 0; + + int status; 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++; + pid_t cpid = fork(); + if (cpid < 0){ + perror("fork"); + exit(EXIT_FAILURE); } - printf("%s\n", ptr->name); + if (cpid == 0){ + int rc = ptr->op(); + _exit(rc); + }else{ + + if (waitpid(cpid, &status, 0) < 0){ + perror("waitpid"); + exit(EXIT_FAILURE); + } + } + + if (WIFEXITED(status)){ + int rc = WEXITSTATUS(status); + if (rc == 0) { + printf("%s[PASS]%s ", X_GREEN, X_RST); + printf("%s", ptr->name); + } + else{ + printf("%s[FAIL]%s ", X_RED, X_RST); + printf("%s ", ptr->name); + printf("- Exit Code %s(%d)%s", X_YELLOW, rc, X_RST); + errors++; + } + }else if (WIFSIGNALED(status)){ + printf("%s[FAIL]%s ", X_RED, X_RST); + printf("%s ", ptr->name); + printf("- Termination Signal %s(%d)%s", X_YELLOW, WTERMSIG(status), X_RST); + errors++; + } + printf("\n"); 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); - - - + + if (errors > 0){ + return 1; + } return 0; }