summaryrefslogtreecommitdiffstats
path: root/tests/main.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-07-07 12:59:39 -0400
committerTavian Barnes <tavianator@tavianator.com>2024-07-07 13:12:56 -0400
commit3206124fb3af2481fc45e705f7bba3ea56016433 (patch)
tree994f03b0d9d1db2e410a7bef155f0e5b4e03fe28 /tests/main.c
parent144353ab004bcd3e85f4cdd0a848add7811df2fe (diff)
downloadbfs-3206124fb3af2481fc45e705f7bba3ea56016433.tar.xz
tests: Simplify unit tests with a global variable
It's a little awkward to thread the test result through manually; much easier to just make bfs_check() update a global variable.
Diffstat (limited to 'tests/main.c')
-rw-r--r--tests/main.c19
1 files changed, 17 insertions, 2 deletions
diff --git a/tests/main.c b/tests/main.c
index bef2e37..7386469 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -9,14 +9,26 @@
#include "tests.h"
#include "bfstd.h"
#include "color.h"
+#include "thread.h"
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
+/** Result of the current test. */
+static thread_local bool pass;
+
+bool bfs_check_impl(bool result) {
+ pass &= result;
+ return result;
+}
+
+/** Unit test function type. */
+typedef void test_fn(void);
+
/**
- * Test context.
+ * Global test context.
*/
struct test_ctx {
/** Number of command line arguments. */
@@ -80,7 +92,10 @@ static bool should_run(const struct test_ctx *ctx, const char *test) {
/** Run a test if it's enabled. */
static void run_test(struct test_ctx *ctx, const char *test, test_fn *fn) {
if (should_run(ctx, test)) {
- if (fn()) {
+ pass = true;
+ fn();
+
+ if (pass) {
cfprintf(ctx->cout, "${grn}[PASS]${rs} ${bld}%s${rs}\n", test);
} else {
cfprintf(ctx->cout, "${red}[FAIL]${rs} ${bld}%s${rs}\n", test);