aboutsummaryrefslogtreecommitdiff
path: root/src/unittest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/unittest.c')
-rw-r--r--src/unittest.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/unittest.c b/src/unittest.c
new file mode 100644
index 0000000..753dac3
--- /dev/null
+++ b/src/unittest.c
@@ -0,0 +1,33 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include "unittest.h"
+
+static int TOTAL = 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 unit_test(bool expression, const char* subject) {
+ TOTAL++;
+ printf( "%d. %s: %s\n", TOTAL, subject, expression? draw(GRN, PASS) : draw(RED, FAIL));
+ if (expression) SUCCESS++;
+}
+
+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;
+}
+