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