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