summaryrefslogtreecommitdiff
path: root/tests/test.c
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-10-24 01:17:50 +0200
committerDirk Engling <erdgeist@erdgeist.org>2026-10-24 01:17:50 +0200
commitf24ec48b27d4cb8dd0555840206dedbb45f1423c (patch)
tree4cf772a476be6b06f52b80abc05f952a33209711 /tests/test.c
parentf33203e15a00449152283cab2b878c0886fcede5 (diff)
Add a unit test harness and a first set of tests
make check builds tests/runner from the modules that link without the io subsystem and runs it.
Diffstat (limited to 'tests/test.c')
-rw-r--r--tests/test.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/test.c b/tests/test.c
new file mode 100644
index 0000000..4686369
--- /dev/null
+++ b/tests/test.c
@@ -0,0 +1,58 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever.
3
4 $id$ */
5
6/* System */
7#include <stdarg.h>
8#include <stdio.h>
9
10/* Opentracker */
11#include "test.h"
12
13unsigned long test_checks;
14unsigned long test_failures;
15
16static const char *current_module = "";
17static const char *current_case = "";
18
19void test_module(const char *name) {
20 current_module = name;
21 current_case = "";
22 printf("%s\n", name);
23}
24
25void test_case(const char *name) { current_case = name; }
26
27static void test_where(const char *file, int line) {
28 ++test_failures;
29 printf(" FAIL %s/%s (%s:%d)\n ", current_module, current_case, file, line);
30}
31
32void test_failf(const char *file, int line, const char *fmt, ...) {
33 va_list ap;
34 test_where(file, line);
35 va_start(ap, fmt);
36 vprintf(fmt, ap);
37 va_end(ap);
38 printf("\n");
39}
40
41void test_failmem(const char *file, int line, const char *what, const void *got, const void *want, size_t len) {
42 const unsigned char *g = got, *w = want;
43 size_t i;
44
45 test_where(file, line);
46 printf("%s: %zu bytes differ\n got ", what, len);
47 for (i = 0; i < len; ++i)
48 printf("%02x", g[i]);
49 printf("\n want ");
50 for (i = 0; i < len; ++i)
51 printf("%02x", w[i]);
52 printf("\n");
53}
54
55int test_summary(void) {
56 printf("\n%lu checks, %lu failures\n", test_checks, test_failures);
57 return test_failures != 0;
58}