#include #include #include "unittest.h" static int TOTAL = 0; static int SECTION_TOTAL = 0; static int SECTION_NUMBER = 0; static int SUCCESS = 0; bool assert(bool expression, const char* message) { if (!expression) { printf("%s\n", draw(RED, DEFAULT_ASSERT_MESSAGE)); printf("%s\n", message); } return expression; } void unitTest(bool expression, const char* subject) { TOTAL++; SECTION_TOTAL++; printf( "%d.%d %s: %s\n", SECTION_NUMBER, SECTION_TOTAL, subject, expression? draw(GRN, PASS) : draw(RED, FAIL)); if (expression) SUCCESS++; } void testSection(const char* name) { SECTION_TOTAL = 0; SECTION_NUMBER++; printf("%d.%s %s %s\n", SECTION_NUMBER, CYAN, name, RESET); } double coverage() { return 100 * (TOTAL? (double)SUCCESS/TOTAL : 1); } bool report() { printf("%s ", SUCCESS == TOTAL ? draw(GRN, "All checks successful!") : draw(RED, "Some tests have failed")); printf("[%d / %d]\n", SUCCESS, TOTAL); printf("Total coverage: %.2f%%\n", coverage()); return TOTAL - SUCCESS; }