Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
39831a99 | 3 | * Copyright (C) 2005, 2007, 2008 Richard Kettlewell |
460b9539 | 4 | * |
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. | |
9 | * | |
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. | |
14 | * | |
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 | |
18 | * USA | |
19 | */ | |
033fd4e3 | 20 | /** @file lib/test.c @brief Library tests */ |
460b9539 | 21 | |
b90f122b | 22 | #include "test.h" |
2257512d RK |
23 | #include "version.h" |
24 | #include <getopt.h> | |
460b9539 | 25 | |
f9d42b20 | 26 | long long tests, errors; |
b90f122b | 27 | int fail_first; |
2257512d | 28 | int verbose; |
460b9539 | 29 | |
b90f122b | 30 | void count_error(void) { |
bb48024f RK |
31 | ++errors; |
32 | if(fail_first) | |
33 | abort(); | |
34 | } | |
460b9539 | 35 | |
b90f122b | 36 | const char *format(const char *s) { |
460b9539 | 37 | struct dynstr d; |
38 | int c; | |
39 | char buf[10]; | |
2257512d | 40 | |
460b9539 | 41 | dynstr_init(&d); |
42 | while((c = (unsigned char)*s++)) { | |
43 | if(c >= ' ' && c <= '~') | |
44 | dynstr_append(&d, c); | |
45 | else { | |
46 | sprintf(buf, "\\x%02X", (unsigned)c); | |
47 | dynstr_append_string(&d, buf); | |
48 | } | |
49 | } | |
50 | dynstr_terminate(&d); | |
51 | return d.vec; | |
52 | } | |
53 | ||
b90f122b | 54 | const char *format_utf32(const uint32_t *s) { |
e5a5a138 RK |
55 | struct dynstr d; |
56 | uint32_t c; | |
57 | char buf[64]; | |
2257512d | 58 | |
e5a5a138 RK |
59 | dynstr_init(&d); |
60 | while((c = *s++)) { | |
16506c9d RK |
61 | sprintf(buf, " %04lX", (long)c); |
62 | dynstr_append_string(&d, buf); | |
e5a5a138 RK |
63 | } |
64 | dynstr_terminate(&d); | |
65 | return d.vec; | |
66 | } | |
67 | ||
b90f122b | 68 | uint32_t *ucs4parse(const char *s) { |
460b9539 | 69 | struct dynstr_ucs4 d; |
70 | char *e; | |
71 | ||
72 | dynstr_ucs4_init(&d); | |
73 | while(*s) { | |
74 | errno = 0; | |
75 | dynstr_ucs4_append(&d, strtoul(s, &e, 0)); | |
76 | if(errno) fatal(errno, "strtoul (%s)", s); | |
77 | s = e; | |
78 | } | |
79 | dynstr_ucs4_terminate(&d); | |
80 | return d.vec; | |
81 | } | |
82 | ||
b90f122b | 83 | const char *do_printf(const char *fmt, ...) { |
0c740e59 RK |
84 | va_list ap; |
85 | char *s; | |
86 | int rc; | |
87 | ||
88 | va_start(ap, fmt); | |
89 | rc = byte_vasprintf(&s, fmt, ap); | |
90 | va_end(ap); | |
91 | if(rc < 0) | |
92 | return 0; | |
93 | return s; | |
94 | } | |
95 | ||
3943e9ec RK |
96 | jmp_buf fatal_env; |
97 | ||
98 | void test_exitfn(int rc) { | |
99 | assert(rc != 0); | |
100 | longjmp(fatal_env, rc); | |
101 | } | |
102 | ||
2257512d RK |
103 | static const struct option options[] = { |
104 | { "verbose", no_argument, 0, 'v' }, | |
105 | { "fail-first", no_argument, 0, 'F' }, | |
106 | { "help", no_argument, 0, 'h' }, | |
107 | { "version", no_argument, 0, 'V' }, | |
108 | }; | |
109 | ||
110 | /* display usage message and terminate */ | |
111 | static void help(void) { | |
112 | xprintf("Usage:\n" | |
113 | " %s [OPTIONS]\n" | |
114 | "Options:\n" | |
115 | " --help, -h Display usage message\n" | |
116 | " --version, -V Display version number\n" | |
117 | " --verbose, -v Verbose output\n" | |
118 | " --fail-first, -F Stop on first failure\n", | |
119 | progname); | |
120 | xfclose(stdout); | |
121 | exit(0); | |
122 | } | |
123 | ||
124 | void test_init(int argc, char **argv) { | |
125 | int n; | |
126 | ||
127 | set_progname(argv); | |
128 | mem_init(); | |
129 | while((n = getopt_long(argc, argv, "vFhV", options, 0)) >= 0) { | |
130 | switch(n) { | |
131 | case 'v': verbose = 1; break; | |
132 | case 'F': fail_first = 1; break; | |
133 | case 'h': help(); | |
134 | case 'V': version(progname); | |
135 | default: exit(1); | |
136 | } | |
137 | } | |
138 | if(getenv("FAIL_FIRST")) | |
139 | fail_first = 1; | |
140 | } | |
141 | ||
142 | ||
460b9539 | 143 | /* |
144 | Local Variables: | |
145 | c-basic-offset:2 | |
146 | comment-column:40 | |
56fd389c RK |
147 | fill-column:79 |
148 | indent-tabs-mode:nil | |
460b9539 | 149 | End: |
150 | */ |