chiark / gitweb /
document new choose screen properly
[disorder] / lib / 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 <config.h>
26 #include "types.h"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <ctype.h>
33 #include <assert.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <sys/wait.h>
39 #include <stddef.h>
40 #include <sys/socket.h>
41 #include <netdb.h>
42 #include <netinet/in.h>
43 #include <sys/un.h>
44 #include <pcre.h>
45
46 #include "mem.h"
47 #include "log.h"
48 #include "vector.h"
49 #include "charset.h"
50 #include "mime.h"
51 #include "hex.h"
52 #include "heap.h"
53 #include "unicode.h"
54 #include "inputline.h"
55 #include "wstat.h"
56 #include "signame.h"
57 #include "cache.h"
58 #include "filepart.h"
59 #include "hash.h"
60 #include "selection.h"
61 #include "syscalls.h"
62 #include "kvp.h"
63 #include "sink.h"
64 #include "printf.h"
65 #include "basen.h"
66 #include "split.h"
67 #include "configuration.h"
68 #include "addr.h"
69 #include "base64.h"
70 #include "url.h"
71 #include "regsub.h"
72
73 extern long long tests, errors;
74 extern int fail_first;
75 extern int verbose;
76
77 /** @brief Checks that @p expr is nonzero */
78 #define insist(expr) do {                               \
79   if(!(expr)) {                                         \
80     count_error();                                              \
81     fprintf(stderr, "%s:%d: error checking %s\n",       \
82             __FILE__, __LINE__, #expr);                 \
83   }                                                     \
84   ++tests;                                              \
85 } while(0)
86
87 #define check_string(GOT, WANT) do {                                    \
88   const char *got = GOT;                                                \
89   const char *want = WANT;                                              \
90                                                                         \
91   if(want == 0) {                                                       \
92     fprintf(stderr, "%s:%d: %s returned 0\n",                           \
93             __FILE__, __LINE__, #GOT);                                  \
94     count_error();                                                      \
95   } else if(strcmp(want, got)) {                                        \
96     fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n",         \
97             __FILE__, __LINE__, #GOT, format(got), format(want));       \
98     count_error();                                                      \
99   }                                                                     \
100   ++tests;                                                              \
101  } while(0)
102
103 #define check_string_prefix(GOT, WANT) do {                             \
104   const char *got = GOT;                                                \
105   const char *want = WANT;                                              \
106                                                                         \
107   if(want == 0) {                                                       \
108     fprintf(stderr, "%s:%d: %s returned 0\n",                           \
109             __FILE__, __LINE__, #GOT);                                  \
110     count_error();                                                      \
111   } else if(strncmp(want, got, strlen(want))) {                         \
112     fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n",      \
113             __FILE__, __LINE__, #GOT, format(got), format(want));       \
114     count_error();                                                      \
115   }                                                                     \
116   ++tests;                                                              \
117  } while(0)
118
119 #define check_integer(GOT, WANT) do {                           \
120   const intmax_t got = GOT, want = WANT;                        \
121   if(got != want) {                                             \
122     fprintf(stderr, "%s:%d: %s returned: %jd  expected: %jd\n", \
123             __FILE__, __LINE__, #GOT, got, want);               \
124     count_error();                                              \
125   }                                                             \
126   ++tests;                                                      \
127 } while(0)
128
129 void count_error(void);
130 const char *format(const char *s);
131 const char *format_utf32(const uint32_t *s);
132 uint32_t *ucs4parse(const char *s);
133 const char *do_printf(const char *fmt, ...);
134 void test_init(int argc, char **argv);
135
136 #define TEST(name)                                                      \
137   int main(int argc, char **argv) {                                     \
138     test_init(argc, argv);                                              \
139     test_##name();                                                      \
140     if(errors || verbose)                                               \
141       fprintf(stderr, "test_"#name": %lld errors out of %lld tests\n",  \
142               errors, tests);                                           \
143     return !!errors;                                                    \
144   }                                                                     \
145                                                                         \
146   struct swallow_semicolon
147
148 #endif /* TEST_H */
149
150 /*
151 Local Variables:
152 c-basic-offset:2
153 comment-column:40
154 fill-column:79
155 indent-tabs-mode:nil
156 End:
157 */