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.c @brief Library tests */
24 /** @brief Count of tests */
27 /** @brief Count of errors */
30 /** @brief If set, first error will fail whole test */
33 /** @brief Verbose mode */
36 /** @brief If set, test will return 'skipped' indicator */
39 /** @brief Count up an error
41 * If @ref fail_first is set then the test run is aborted.
43 void count_error(void) {
49 /** @brief Render a string into printable ASCII
50 * @param s String to format
51 * @return Allocated copy of formatted string
53 * Replaces any non-ASCII characters with a hex escape.
55 const char *format(const char *s
) {
61 while((c
= (unsigned char)*s
++)) {
62 if(c
>= ' ' && c
<= '~')
65 sprintf(buf
, "\\x%02X", (unsigned)c
);
66 dynstr_append_string(&d
, buf
);
73 /** @brief Format a UTF-32 string into hex
74 * @param s String to format
75 * @return Allocated copy of formatted string
77 * Returns the hex codes of @p s separated by spaces.
79 const char *format_utf32(const uint32_t *s
) {
86 sprintf(buf
, " %04lX", (long)c
);
87 dynstr_append_string(&d
, buf
);
93 /** @brief Convert a string of hex codes to a UTF-32 string
94 * @param s String of hex codes, separated by spaces
95 * @return Allocated string, 0-terminated
97 uint32_t *ucs4parse(const char *s
) {
101 dynstr_ucs4_init(&d
);
104 dynstr_ucs4_append(&d
, strtoul(s
, &e
, 0));
105 if(errno
) fatal(errno
, "strtoul (%s)", s
);
108 dynstr_ucs4_terminate(&d
);
112 /** @brief Format a string like asprintf()
113 * @param fmt Format string, per printf(3)
114 * @param ... Arguments
115 * @return Formatted string or null pointer on error
117 const char *do_printf(const char *fmt
, ...) {
123 rc
= byte_vasprintf(&s
, fmt
, ap
);
130 /** @brief Jump buffer for exitfn() testing */
133 /** @brief exitfn() callback for testing
134 * @param rc Value to return from setjmp()
135 * Jumps to @ref fatal_env
137 void test_exitfn(int rc
) {
139 longjmp(fatal_env
, rc
);
142 static const struct option options
[] = {
143 { "verbose", no_argument
, 0, 'v' },
144 { "fail-first", no_argument
, 0, 'F' },
145 { "help", no_argument
, 0, 'h' },
146 { "version", no_argument
, 0, 'V' },
149 /* display usage message and terminate */
150 static void help(void) {
154 " --help, -h Display usage message\n"
155 " --version, -V Display version number\n"
156 " --verbose, -v Verbose output\n"
157 " --fail-first, -F Stop on first failure\n",
163 /** @brief Standard test program initialization
164 * @param argc Argument count
165 * @param argv Arguments
167 void test_init(int argc
, char **argv
) {
172 while((n
= getopt_long(argc
, argv
, "vFhV", options
, 0)) >= 0) {
174 case 'v': verbose
= 1; break;
175 case 'F': fail_first
= 1; break;
177 case 'V': version(progname
);
181 if(getenv("FAIL_FIRST"))