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;
72 /** @brief Checks that @p expr is nonzero */
73 #define insist(expr) do { \
76 fprintf(stderr, "%s:%d: error checking %s\n", \
77 __FILE__, __LINE__, #expr); \
82 #define check_string(GOT, WANT) do { \
83 const char *got = GOT; \
84 const char *want = WANT; \
87 fprintf(stderr, "%s:%d: %s returned 0\n", \
88 __FILE__, __LINE__, #GOT); \
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)); \
98 #define check_string_prefix(GOT, WANT) do { \
99 const char *got = GOT; \
100 const char *want = WANT; \
103 fprintf(stderr, "%s:%d: %s returned 0\n", \
104 __FILE__, __LINE__, #GOT); \
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)); \
114 #define check_integer(GOT, WANT) do { \
115 const intmax_t got = GOT, want = WANT; \
117 fprintf(stderr, "%s:%d: %s returned: %jd expected: %jd\n", \
118 __FILE__, __LINE__, #GOT, got, want); \
124 #define check_fatal(WHAT) do { \
125 void (*const save_exitfn)(int) attribute((noreturn)) = exitfn; \
127 exitfn = test_exitfn; \
128 if(setjmp(fatal_env) == 0) { \
129 fprintf(stderr, "Expect an error:\n "); \
131 fprintf(stderr, "\n%s:%d: %s unexpectedly returned\n", \
132 __FILE__, __LINE__, #WHAT); \
136 exitfn = save_exitfn; \
139 void count_error(void);
140 const char *format(const char *s);
141 const char *format_utf32(const uint32_t *s);
142 uint32_t *ucs4parse(const char *s);
143 const char *do_printf(const char *fmt, ...);
144 void test_init(int argc, char **argv);
146 extern jmp_buf fatal_env;
147 void test_exitfn(int) attribute((noreturn));
150 int main(int argc, char **argv) { \
151 test_init(argc, argv); \
153 if(errors || verbose) \
154 fprintf(stderr, "test_"#name": %lld errors out of %lld tests\n", \
159 struct swallow_semicolon