summaryrefslogtreecommitdiff
path: root/tests/test.h
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test.h')
-rw-r--r--tests/test.h89
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/test.h b/tests/test.h
new file mode 100644
index 0000000..7449b73
--- /dev/null
+++ b/tests/test.h
@@ -0,0 +1,89 @@
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#ifndef OT_TEST_H__
7#define OT_TEST_H__
8
9#include <stddef.h>
10#include <string.h>
11
12/* Minimal scaffolding for opentracker's unit tests. No external dependencies.
13
14 Tests are grouped into modules, one per source file under test. A module is
15 a list of small case functions, each pinning one behaviour. A failing CHECK
16 reports file, line and the offending values, then returns from the case it
17 is in, so a single run reports as many independent problems as it can.
18 Because the macros return, they may only be used in functions returning
19 void. */
20
21void test_module(const char *name);
22void test_case(const char *name);
23void test_failf(const char *file, int line, const char *fmt, ...);
24void test_failmem(const char *file, int line, const char *what, const void *got, const void *want, size_t len);
25int test_summary(void);
26
27extern unsigned long test_checks;
28extern unsigned long test_failures;
29
30#define CHECK(cond) \
31 do { \
32 ++test_checks; \
33 if (!(cond)) { \
34 test_failf(__FILE__, __LINE__, "expected %s", #cond); \
35 return; \
36 } \
37 } while (0)
38
39#define CHECK_INT(got, want) \
40 do { \
41 long long got_ = (long long)(got), want_ = (long long)(want); \
42 ++test_checks; \
43 if (got_ != want_) { \
44 test_failf(__FILE__, __LINE__, "%s: got %lld, want %lld", #got, got_, want_); \
45 return; \
46 } \
47 } while (0)
48
49#define CHECK_PTR(got, want) \
50 do { \
51 const void *got_ = (const void *)(got), *want_ = (const void *)(want); \
52 ++test_checks; \
53 if (got_ != want_) { \
54 test_failf(__FILE__, __LINE__, "%s: got %p, want %p", #got, got_, want_); \
55 return; \
56 } \
57 } while (0)
58
59#define CHECK_MEM(got, want, len) \
60 do { \
61 ++test_checks; \
62 if (memcmp((got), (want), (len))) { \
63 test_failmem(__FILE__, __LINE__, #got, (got), (want), (len)); \
64 return; \
65 } \
66 } while (0)
67
68#define CHECK_STR(got, want) \
69 do { \
70 const char *got_ = (got), *want_ = (want); \
71 ++test_checks; \
72 if (strcmp(got_, want_)) { \
73 test_failf(__FILE__, __LINE__, "%s: got \"%s\", want \"%s\"", #got, got_, want_); \
74 return; \
75 } \
76 } while (0)
77
78/* One entry point per module under test */
79void test_scan_urlencoded_query(void);
80void test_iovec(void);
81void test_vector(void);
82void test_random(void);
83void test_hex(void);
84void test_accesslist(void);
85void test_scan(void);
86void test_buf(void);
87void test_ip(void);
88
89#endif