aboutsummaryrefslogtreecommitdiff
path: root/minitest.h
blob: c566e4824b5e81ab18c54bd734fea2e7b3aa2d50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

/// Minimal testing library tweaked with
/// reference from `minunit.h` from
/// Jera Design LLC:
/// https://jera.com/techinfo/jtns/jtn002

#include <stdio.h>
#define mt_assert(test, message) do { if (!(test)) return message; } while (0)
#define mt_assert_eq(left, right) do { \
	if (left != right) \
		return "`" #left "` didn't match `" #right "`"; \
	return 0; \
} while (0)
#define mt_run_test(test) do { \
	char *message = test(); \
	if (message) { \
		fprintf(stderr, "[FAIL] " #test ": %s\n", message); \
		tests_failed++; \
	} else { \
		fprintf(stderr, "[PASS] " #test "\n"); \
	} \
	tests_run++; \
} while (0)
#define mt_test_report() do { \
	if (tests_failed == 0) \
		fprintf(stderr, "[REPORT] All %d tests passed.\n", tests_run); \
	else \
		fprintf(stderr, "[REPORT] Failure. %d/%d tests failed.\n", tests_failed, tests_run); \
} while (0)
extern int tests_run;
extern int tests_failed;