2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file libtests/test.h @brief Library tests */
27 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
47 #include "inputline.h"
53 #include "selection.h"
60 #include "configuration.h"
66 extern long long tests, errors;
67 extern int fail_first;
71 /** @brief Checks that @p expr is nonzero
72 * @param expr Expression to check
74 * If @p expr is nonzero then logs an error (and continues).
76 #define insist(expr) do { \
79 fprintf(stderr, "%s:%d: error checking %s\n", \
80 __FILE__, __LINE__, #expr); \
85 /** @brief Check that a pair of strings match
86 * @param GOT What we actually got
87 * @param WANT What we wanted
89 * If @p GOT and @p WANT differ then logs an error (and continues).
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).
94 #define check_string(GOT, WANT) do { \
95 const char *got = GOT; \
96 const char *want = WANT; \
99 fprintf(stderr, "%s:%d: %s returned 0\n", \
100 __FILE__, __LINE__, #GOT); \
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)); \
110 /** @brief Check that a string prefix matches
111 * @param GOT What we actually got
112 * @param WANT What we wanted
114 * If @p WANT is not a prefix of @p GOT then logs an error (and continues).
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).
119 #define check_string_prefix(GOT, WANT) do { \
120 const char *got = GOT; \
121 const char *want = WANT; \
124 fprintf(stderr, "%s:%d: %s returned 0\n", \
125 __FILE__, __LINE__, #GOT); \
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)); \
135 /** @brief Check that a pair of integers match.
136 * @param GOT What we actually got
137 * @param WANT What we wanted
139 * If @p GOT and @p WANT differ then logs an error (and continues).
141 #define check_integer(GOT, WANT) do { \
142 const intmax_t got = GOT, want = WANT; \
144 fprintf(stderr, "%s:%d: %s returned: %jd expected: %jd\n", \
145 __FILE__, __LINE__, #GOT, got, want); \
151 /** @brief Check that a function calls fatal()
152 * @param WHAT Expression to evaluate
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.
157 #define check_fatal(WHAT) do { \
158 void (*const save_exitfn)(int) attribute((noreturn)) = exitfn; \
160 exitfn = test_exitfn; \
161 if(setjmp(fatal_env) == 0) { \
162 fprintf(stderr, "Expect an error:\n "); \
164 fprintf(stderr, "\n%s:%d: %s unexpectedly returned\n", \
165 __FILE__, __LINE__, #WHAT); \
169 exitfn = save_exitfn; \
172 void count_error(void);
173 const char *format(const char *s);
174 const char *format_utf32(const uint32_t *s);
175 uint32_t *ucs4parse(const char *s);
176 const char *do_printf(const char *fmt, ...);
177 void test_init(int argc, char **argv);
179 extern jmp_buf fatal_env;
180 void test_exitfn(int) attribute((noreturn));
182 /** @brief Common code for each test source file
183 * @param name Name of test
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
192 int main(int argc, char **argv) { \
193 test_init(argc, argv); \
195 if(errors || verbose) \
196 fprintf(stderr, "test_"#name": %lld errors out of %lld tests\n", \
205 struct swallow_semicolon