chiark / gitweb /
Tidy up
[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 extern int skipped;
72
73 /** @brief Checks that @p expr is nonzero */
74 #define insist(expr) do {                               \
75   if(!(expr)) {                                         \
76     count_error();                                              \
77     fprintf(stderr, "%s:%d: error checking %s\n",       \
78             __FILE__, __LINE__, #expr);                 \
79   }                                                     \
80   ++tests;                                              \
81 } while(0)
82
83 #define check_string(GOT, WANT) do {                                    \
84   const char *got = GOT;                                                \
85   const char *want = WANT;                                              \
86                                                                         \
87   if(want == 0) {                                                       \
88     fprintf(stderr, "%s:%d: %s returned 0\n",                           \
89             __FILE__, __LINE__, #GOT);                                  \
90     count_error();                                                      \
91   } else if(strcmp(want, got)) {                                        \
92     fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n",         \
93             __FILE__, __LINE__, #GOT, format(got), format(want));       \
94     count_error();                                                      \
95   }                                                                     \
96   ++tests;                                                              \
97  } while(0)
98
99 #define check_string_prefix(GOT, WANT) do {                             \
100   const char *got = GOT;                                                \
101   const char *want = WANT;                                              \
102                                                                         \
103   if(want == 0) {                                                       \
104     fprintf(stderr, "%s:%d: %s returned 0\n",                           \
105             __FILE__, __LINE__, #GOT);                                  \
106     count_error();                                                      \
107   } else if(strncmp(want, got, strlen(want))) {                         \
108     fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n",      \
109             __FILE__, __LINE__, #GOT, format(got), format(want));       \
110     count_error();                                                      \
111   }                                                                     \
112   ++tests;                                                              \
113  } while(0)
114
115 #define check_integer(GOT, WANT) do {                           \
116   const intmax_t got = GOT, want = WANT;                        \
117   if(got != want) {                                             \
118     fprintf(stderr, "%s:%d: %s returned: %jd  expected: %jd\n", \
119             __FILE__, __LINE__, #GOT, got, want);               \
120     count_error();                                              \
121   }                                                             \
122   ++tests;                                                      \
123 } while(0)
124
125 #define check_fatal(WHAT) do {                                          \
126   void (*const save_exitfn)(int) attribute((noreturn)) = exitfn;        \
127                                                                         \
128   exitfn = test_exitfn;                                                 \
129   if(setjmp(fatal_env) == 0) {                                          \
130     fprintf(stderr, "Expect an error:\n ");                             \
131     (void)(WHAT);                                                       \
132     fprintf(stderr, "\n%s:%d: %s unexpectedly returned\n",              \
133                      __FILE__, __LINE__, #WHAT);                        \
134     count_error();                                                      \
135   }                                                                     \
136   ++tests;                                                              \
137   exitfn = save_exitfn;                                                 \
138 } while(0)
139
140 void count_error(void);
141 const char *format(const char *s);
142 const char *format_utf32(const uint32_t *s);
143 uint32_t *ucs4parse(const char *s);
144 const char *do_printf(const char *fmt, ...);
145 void test_init(int argc, char **argv);
146
147 extern jmp_buf fatal_env;
148 void test_exitfn(int) attribute((noreturn));
149
150 #define TEST(name)                                                      \
151   int main(int argc, char **argv) {                                     \
152     test_init(argc, argv);                                              \
153     test_##name();                                                      \
154     if(errors || verbose)                                               \
155       fprintf(stderr, "test_"#name": %lld errors out of %lld tests\n",  \
156               errors, tests);                                           \
157     if(errors)                                                          \
158       return 1;                                                         \
159     if(skipped)                                                         \
160       return 77;                                                        \
161     return 0;                                                           \
162   }                                                                     \
163                                                                         \
164   struct swallow_semicolon
165
166 #endif /* TEST_H */
167
168 /*
169 Local Variables:
170 c-basic-offset:2
171 comment-column:40
172 fill-column:79
173 indent-tabs-mode:nil
174 End:
175 */