diff options
Diffstat (limited to 'tests/test.c')
| -rw-r--r-- | tests/test.c | 58 |
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 | |||
| 13 | unsigned long test_checks; | ||
| 14 | unsigned long test_failures; | ||
| 15 | |||
| 16 | static const char *current_module = ""; | ||
| 17 | static const char *current_case = ""; | ||
| 18 | |||
| 19 | void test_module(const char *name) { | ||
| 20 | current_module = name; | ||
| 21 | current_case = ""; | ||
| 22 | printf("%s\n", name); | ||
| 23 | } | ||
| 24 | |||
| 25 | void test_case(const char *name) { current_case = name; } | ||
| 26 | |||
| 27 | static 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 | |||
| 32 | void 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 | |||
| 41 | void 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 | |||
| 55 | int test_summary(void) { | ||
| 56 | printf("\n%lu checks, %lu failures\n", test_checks, test_failures); | ||
| 57 | return test_failures != 0; | ||
| 58 | } | ||
