chiark / gitweb /
Remove further databases on Debian remove/purge.
[disorder] / libtests / test.h
CommitLineData
b90f122b
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
b90f122b 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
b90f122b 8 * (at your option) any later version.
e7eb3a27
RK
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
b90f122b 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b90f122b 17 */
59cf25c4 18/** @file libtests/test.h @brief Library tests */
b90f122b
RK
19
20#ifndef TEST_H
21#define TEST_H
22
05b75f8d 23#include "common.h"
b90f122b 24
b90f122b
RK
25#include <errno.h>
26#include <ctype.h>
b90f122b
RK
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <unistd.h>
30#include <signal.h>
b90f122b
RK
31#include <stddef.h>
32#include <sys/socket.h>
33#include <netdb.h>
34#include <netinet/in.h>
35#include <sys/un.h>
36#include <pcre.h>
3943e9ec 37#include <setjmp.h>
b90f122b
RK
38
39#include "mem.h"
40#include "log.h"
41#include "vector.h"
42#include "charset.h"
43#include "mime.h"
44#include "hex.h"
45#include "heap.h"
46#include "unicode.h"
47#include "inputline.h"
48#include "wstat.h"
49#include "signame.h"
50#include "cache.h"
51#include "filepart.h"
52#include "hash.h"
53#include "selection.h"
54#include "syscalls.h"
55#include "kvp.h"
56#include "sink.h"
57#include "printf.h"
58#include "basen.h"
59#include "split.h"
60#include "configuration.h"
61#include "addr.h"
62#include "base64.h"
63#include "url.h"
64#include "regsub.h"
65
f9d42b20 66extern long long tests, errors;
b90f122b 67extern int fail_first;
2257512d 68extern int verbose;
cca956b1 69extern int skipped;
b90f122b 70
cc3ede61
RK
71/** @brief Checks that @p expr is nonzero
72 * @param expr Expression to check
73 *
74 * If @p expr is nonzero then logs an error (and continues).
75 */
b90f122b
RK
76#define insist(expr) do { \
77 if(!(expr)) { \
78 count_error(); \
79 fprintf(stderr, "%s:%d: error checking %s\n", \
80 __FILE__, __LINE__, #expr); \
81 } \
82 ++tests; \
83} while(0)
84
cc3ede61
RK
85/** @brief Check that a pair of strings match
86 * @param GOT What we actually got
87 * @param WANT What we wanted
88 *
89 * If @p GOT and @p WANT differ then logs an error (and continues).
90 *
91 * @p WANT is allowed to evaluate to a null pointer (but if it comes to
92 * anything else then must be safe to strcmp).
93 */
b90f122b
RK
94#define check_string(GOT, WANT) do { \
95 const char *got = GOT; \
96 const char *want = WANT; \
97 \
98 if(want == 0) { \
99 fprintf(stderr, "%s:%d: %s returned 0\n", \
100 __FILE__, __LINE__, #GOT); \
101 count_error(); \
102 } else if(strcmp(want, got)) { \
103 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \
104 __FILE__, __LINE__, #GOT, format(got), format(want)); \
105 count_error(); \
106 } \
107 ++tests; \
108 } while(0)
109
cc3ede61
RK
110/** @brief Check that a string prefix matches
111 * @param GOT What we actually got
112 * @param WANT What we wanted
113 *
114 * If @p WANT is not a prefix of @p GOT then logs an error (and continues).
115 *
116 * @p WANT is allowed to evaluate to a null pointer (but if it comes to
117 * anything else then must be safe to strcmp).
118 */
b90f122b
RK
119#define check_string_prefix(GOT, WANT) do { \
120 const char *got = GOT; \
121 const char *want = WANT; \
122 \
123 if(want == 0) { \
124 fprintf(stderr, "%s:%d: %s returned 0\n", \
125 __FILE__, __LINE__, #GOT); \
126 count_error(); \
127 } else if(strncmp(want, got, strlen(want))) { \
128 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n", \
129 __FILE__, __LINE__, #GOT, format(got), format(want)); \
130 count_error(); \
131 } \
132 ++tests; \
133 } while(0)
134
cc3ede61
RK
135/** @brief Check that a pair of integers match.
136 * @param GOT What we actually got
137 * @param WANT What we wanted
138 *
139 * If @p GOT and @p WANT differ then logs an error (and continues).
140 */
b90f122b
RK
141#define check_integer(GOT, WANT) do { \
142 const intmax_t got = GOT, want = WANT; \
143 if(got != want) { \
144 fprintf(stderr, "%s:%d: %s returned: %jd expected: %jd\n", \
145 __FILE__, __LINE__, #GOT, got, want); \
146 count_error(); \
147 } \
148 ++tests; \
149} while(0)
150
cc3ede61
RK
151/** @brief Check that a function calls fatal()
152 * @param WHAT Expression to evaluate
153 *
154 * Evaluates WHAT and if it does not call fatal(), logs an error. In any case,
155 * continues. Modifies exitfn() so that fatal() isn't actually fatal.
156 */
3943e9ec
RK
157#define check_fatal(WHAT) do { \
158 void (*const save_exitfn)(int) attribute((noreturn)) = exitfn; \
159 \
160 exitfn = test_exitfn; \
161 if(setjmp(fatal_env) == 0) { \
162 fprintf(stderr, "Expect an error:\n "); \
163 (void)(WHAT); \
164 fprintf(stderr, "\n%s:%d: %s unexpectedly returned\n", \
165 __FILE__, __LINE__, #WHAT); \
166 count_error(); \
167 } \
168 ++tests; \
169 exitfn = save_exitfn; \
170} while(0)
171
b90f122b
RK
172void count_error(void);
173const char *format(const char *s);
174const char *format_utf32(const uint32_t *s);
175uint32_t *ucs4parse(const char *s);
176const char *do_printf(const char *fmt, ...);
2257512d 177void test_init(int argc, char **argv);
b90f122b 178
3943e9ec
RK
179extern jmp_buf fatal_env;
180void test_exitfn(int) attribute((noreturn));
181
cc3ede61
RK
182/** @brief Common code for each test source file
183 * @param name Name of test
184 *
185 * Expands to a @c main function which:
186 * - calls test_init()
187 * - calls test_NAME()
188 * - reports a count of errors
189 * - returns the right exit status
190 */
c68d8eba 191#define TEST(name) \
2257512d
RK
192 int main(int argc, char **argv) { \
193 test_init(argc, argv); \
c68d8eba 194 test_##name(); \
2257512d 195 if(errors || verbose) \
c68d8eba
RK
196 fprintf(stderr, "test_"#name": %lld errors out of %lld tests\n", \
197 errors, tests); \
cca956b1
RK
198 if(errors) \
199 return 1; \
200 if(skipped) \
201 return 77; \
202 return 0; \
c68d8eba
RK
203 } \
204 \
205 struct swallow_semicolon
b90f122b
RK
206
207#endif /* TEST_H */
208
209/*
210Local Variables:
211c-basic-offset:2
212comment-column:40
213fill-column:79
214indent-tabs-mode:nil
215End:
216*/