chiark / gitweb /
Source code reorganization:
[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 <sys/wait.h>
34 #include <stddef.h>
35 #include <sys/socket.h>
36 #include <netdb.h>
37 #include <netinet/in.h>
38 #include <sys/un.h>
39 #include <pcre.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 void count_error(void);
125 const char *format(const char *s);
126 const char *format_utf32(const uint32_t *s);
127 uint32_t *ucs4parse(const char *s);
128 const char *do_printf(const char *fmt, ...);
129 void test_init(int argc, char **argv);
130
131 #define TEST(name)                                                      \
132   int main(int argc, char **argv) {                                     \
133     test_init(argc, argv);                                              \
134     test_##name();                                                      \
135     if(errors || verbose)                                               \
136       fprintf(stderr, "test_"#name": %lld errors out of %lld tests\n",  \
137               errors, tests);                                           \
138     return !!errors;                                                    \
139   }                                                                     \
140                                                                         \
141   struct swallow_semicolon
142
143 #endif /* TEST_H */
144
145 /*
146 Local Variables:
147 c-basic-offset:2
148 comment-column:40
149 fill-column:79
150 indent-tabs-mode:nil
151 End:
152 */