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 2 of the License, or
8 * (at your option) any later version.
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.
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
20 /** @file lib/test.h @brief Library tests */
29 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
49 #include "inputline.h"
55 #include "selection.h"
62 #include "configuration.h"
68 extern long long tests, errors;
69 extern int fail_first;
73 /** @brief Checks that @p expr is nonzero */
74 #define insist(expr) do { \
77 fprintf(stderr, "%s:%d: error checking %s\n", \
78 __FILE__, __LINE__, #expr); \
83 #define check_string(GOT, WANT) do { \
84 const char *got = GOT; \
85 const char *want = WANT; \
88 fprintf(stderr, "%s:%d: %s returned 0\n", \
89 __FILE__, __LINE__, #GOT); \
91 } else if(strcmp(want, got)) { \
92 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \
93 __FILE__, __LINE__, #GOT, format(got), format(want)); \
99 #define check_string_prefix(GOT, WANT) do { \
100 const char *got = GOT; \
101 const char *want = WANT; \
104 fprintf(stderr, "%s:%d: %s returned 0\n", \
105 __FILE__, __LINE__, #GOT); \
107 } else if(strncmp(want, got, strlen(want))) { \
108 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n", \
109 __FILE__, __LINE__, #GOT, format(got), format(want)); \
115 #define check_integer(GOT, WANT) do { \
116 const intmax_t got = GOT, want = WANT; \
118 fprintf(stderr, "%s:%d: %s returned: %jd expected: %jd\n", \
119 __FILE__, __LINE__, #GOT, got, want); \
125 #define check_fatal(WHAT) do { \
126 void (*const save_exitfn)(int) attribute((noreturn)) = exitfn; \
128 exitfn = test_exitfn; \
129 if(setjmp(fatal_env) == 0) { \
130 fprintf(stderr, "Expect an error:\n "); \
132 fprintf(stderr, "\n%s:%d: %s unexpectedly returned\n", \
133 __FILE__, __LINE__, #WHAT); \
137 exitfn = save_exitfn; \
140 void count_error(void);
141 const char *format(const char *s);
142 const char *format_utf32(const uint32_t *s);
143 uint32_t *ucs4parse(const char *s);
144 const char *do_printf(const char *fmt, ...);
145 void test_init(int argc, char **argv);
147 extern jmp_buf fatal_env;
148 void test_exitfn(int) attribute((noreturn));
151 int main(int argc, char **argv) { \
152 test_init(argc, argv); \
154 if(errors || verbose) \
155 fprintf(stderr, "test_"#name": %lld errors out of %lld tests\n", \
164 struct swallow_semicolon