chiark / gitweb /
Build fix for Linux
[disorder] / libtests / test.h
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file lib/test.h @brief Library tests */
21
22 #ifndef TEST_H
23 #define TEST_H
24
25 #include "common.h"
26
27 #include <errno.h>
28 #include <ctype.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <signal.h>
33 #include <stddef.h>
34 #include <sys/socket.h>
35 #include <netdb.h>
36 #include <netinet/in.h>
37 #include <sys/un.h>
38 #include <pcre.h>
39 #include <setjmp.h>
40
41 #include "mem.h"
42 #include "log.h"
43 #include "vector.h"
44 #include "charset.h"
45 #include "mime.h"
46 #include "hex.h"
47 #include "heap.h"
48 #include "unicode.h"
49 #include "inputline.h"
50 #include "wstat.h"
51 #include "signame.h"
52 #include "cache.h"
53 #include "filepart.h"
54 #include "hash.h"
55 #include "selection.h"
56 #include "syscalls.h"
57 #include "kvp.h"
58 #include "sink.h"
59 #include "printf.h"
60 #include "basen.h"
61 #include "split.h"
62 #include "configuration.h"
63 #include "addr.h"
64 #include "base64.h"
65 #include "url.h"
66 #include "regsub.h"
67
68 extern long long tests, errors;
69 extern int fail_first;
70 extern int verbose;
71
72 /** @brief Checks that @p expr is nonzero */
73 #define insist(expr) do {                               \
74   if(!(expr)) {                                         \
75     count_error();                                              \
76     fprintf(stderr, "%s:%d: error checking %s\n",       \
77             __FILE__, __LINE__, #expr);                 \
78   }                                                     \
79   ++tests;                                              \
80 } while(0)
81
82 #define check_string(GOT, WANT) do {                                    \
83   const char *got = GOT;                                                \
84   const char *want = WANT;                                              \
85                                                                         \
86   if(want == 0) {                                                       \
87     fprintf(stderr, "%s:%d: %s returned 0\n",                           \
88             __FILE__, __LINE__, #GOT);                                  \
89     count_error();                                                      \
90   } else if(strcmp(want, got)) {                                        \
91     fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n",         \
92             __FILE__, __LINE__, #GOT, format(got), format(want));       \
93     count_error();                                                      \
94   }                                                                     \
95   ++tests;                                                              \
96  } while(0)
97
98 #define check_string_prefix(GOT, WANT) do {                             \
99   const char *got = GOT;                                                \
100   const char *want = WANT;                                              \
101                                                                         \
102   if(want == 0) {                                                       \
103     fprintf(stderr, "%s:%d: %s returned 0\n",                           \
104             __FILE__, __LINE__, #GOT);                                  \
105     count_error();                                                      \
106   } else if(strncmp(want, got, strlen(want))) {                         \
107     fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n",      \
108             __FILE__, __LINE__, #GOT, format(got), format(want));       \
109     count_error();                                                      \
110   }                                                                     \
111   ++tests;                                                              \
112  } while(0)
113
114 #define check_integer(GOT, WANT) do {                           \
115   const intmax_t got = GOT, want = WANT;                        \
116   if(got != want) {                                             \
117     fprintf(stderr, "%s:%d: %s returned: %jd  expected: %jd\n", \
118             __FILE__, __LINE__, #GOT, got, want);               \
119     count_error();                                              \
120   }                                                             \
121   ++tests;                                                      \
122 } while(0)
123
124 #define check_fatal(WHAT) do {                                          \
125   void (*const save_exitfn)(int) attribute((noreturn)) = exitfn;        \
126                                                                         \
127   exitfn = test_exitfn;                                                 \
128   if(setjmp(fatal_env) == 0) {                                          \
129     fprintf(stderr, "Expect an error:\n ");                             \
130     (void)(WHAT);                                                       \
131     fprintf(stderr, "\n%s:%d: %s unexpectedly returned\n",              \
132                      __FILE__, __LINE__, #WHAT);                        \
133     count_error();                                                      \
134   }                                                                     \
135   ++tests;                                                              \
136   exitfn = save_exitfn;                                                 \
137 } while(0)
138
139 void count_error(void);
140 const char *format(const char *s);
141 const char *format_utf32(const uint32_t *s);
142 uint32_t *ucs4parse(const char *s);
143 const char *do_printf(const char *fmt, ...);
144 void test_init(int argc, char **argv);
145
146 extern jmp_buf fatal_env;
147 void test_exitfn(int) attribute((noreturn));
148
149 #define TEST(name)                                                      \
150   int main(int argc, char **argv) {                                     \
151     test_init(argc, argv);                                              \
152     test_##name();                                                      \
153     if(errors || verbose)                                               \
154       fprintf(stderr, "test_"#name": %lld errors out of %lld tests\n",  \
155               errors, tests);                                           \
156     return !!errors;                                                    \
157   }                                                                     \
158                                                                         \
159   struct swallow_semicolon
160
161 #endif /* TEST_H */
162
163 /*
164 Local Variables:
165 c-basic-offset:2
166 comment-column:40
167 fill-column:79
168 indent-tabs-mode:nil
169 End:
170 */