chiark / gitweb /
lib/ tests now use Automake's test infrastructure, and are split into
[disorder] / lib / 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"
460b9539 23
f9d42b20 24long long tests, errors;
b90f122b 25int fail_first;
460b9539 26
b90f122b 27void count_error(void) {
bb48024f
RK
28 ++errors;
29 if(fail_first)
30 abort();
31}
460b9539 32
b90f122b 33const char *format(const char *s) {
460b9539 34 struct dynstr d;
35 int c;
36 char buf[10];
37
38 dynstr_init(&d);
39 while((c = (unsigned char)*s++)) {
40 if(c >= ' ' && c <= '~')
41 dynstr_append(&d, c);
42 else {
43 sprintf(buf, "\\x%02X", (unsigned)c);
44 dynstr_append_string(&d, buf);
45 }
46 }
47 dynstr_terminate(&d);
48 return d.vec;
49}
50
b90f122b 51const char *format_utf32(const uint32_t *s) {
e5a5a138
RK
52 struct dynstr d;
53 uint32_t c;
54 char buf[64];
55
56 dynstr_init(&d);
57 while((c = *s++)) {
16506c9d
RK
58 sprintf(buf, " %04lX", (long)c);
59 dynstr_append_string(&d, buf);
e5a5a138
RK
60 }
61 dynstr_terminate(&d);
62 return d.vec;
63}
64
b90f122b 65uint32_t *ucs4parse(const char *s) {
460b9539 66 struct dynstr_ucs4 d;
67 char *e;
68
69 dynstr_ucs4_init(&d);
70 while(*s) {
71 errno = 0;
72 dynstr_ucs4_append(&d, strtoul(s, &e, 0));
73 if(errno) fatal(errno, "strtoul (%s)", s);
74 s = e;
75 }
76 dynstr_ucs4_terminate(&d);
77 return d.vec;
78}
79
b90f122b 80const char *do_printf(const char *fmt, ...) {
0c740e59
RK
81 va_list ap;
82 char *s;
83 int rc;
84
85 va_start(ap, fmt);
86 rc = byte_vasprintf(&s, fmt, ap);
87 va_end(ap);
88 if(rc < 0)
89 return 0;
90 return s;
91}
92
460b9539 93/*
94Local Variables:
95c-basic-offset:2
96comment-column:40
56fd389c
RK
97fill-column:79
98indent-tabs-mode:nil
460b9539 99End:
100*/