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 #define insist(expr) do { \
75 fprintf(stderr, "%s:%d: error checking %s\n", \
76 __FILE__, __LINE__, #expr); \
81 #define check_string(GOT, WANT) do { \
82 const char *got = GOT; \
83 const char *want = WANT; \
86 fprintf(stderr, "%s:%d: %s returned 0\n", \
87 __FILE__, __LINE__, #GOT); \
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)); \
97 #define check_string_prefix(GOT, WANT) do { \
98 const char *got = GOT; \
99 const char *want = WANT; \
102 fprintf(stderr, "%s:%d: %s returned 0\n", \
103 __FILE__, __LINE__, #GOT); \
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)); \
113 #define check_integer(GOT, WANT) do { \
114 const intmax_t got = GOT, want = WANT; \
116 fprintf(stderr, "%s:%d: %s returned: %jd expected: %jd\n", \
117 __FILE__, __LINE__, #GOT, got, want); \
123 #define check_fatal(WHAT) do { \
124 void (*const save_exitfn)(int) attribute((noreturn)) = exitfn; \
126 exitfn = test_exitfn; \
127 if(setjmp(fatal_env) == 0) { \
128 fprintf(stderr, "Expect an error:\n "); \
130 fprintf(stderr, "\n%s:%d: %s unexpectedly returned\n", \
131 __FILE__, __LINE__, #WHAT); \
135 exitfn = save_exitfn; \
138 void count_error(void);
139 const char *format(const char *s);
140 const char *format_utf32(const uint32_t *s);
141 uint32_t *ucs4parse(const char *s);
142 const char *do_printf(const char *fmt, ...);
143 void test_init(int argc, char **argv);
145 extern jmp_buf fatal_env;
146 void test_exitfn(int) attribute((noreturn));
149 int main(int argc, char **argv) { \
150 test_init(argc, argv); \
152 if(errors || verbose) \
153 fprintf(stderr, "test_"#name": %lld errors out of %lld tests\n", \
162 struct swallow_semicolon