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