| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2005, 2007, 2008 Richard Kettlewell |
| 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 | */ |
| 20 | /** @file lib/test.c @brief Library tests */ |
| 21 | |
| 22 | #include "test.h" |
| 23 | |
| 24 | int tests, errors; |
| 25 | int fail_first; |
| 26 | |
| 27 | void count_error(void) { |
| 28 | ++errors; |
| 29 | if(fail_first) |
| 30 | abort(); |
| 31 | } |
| 32 | |
| 33 | const char *format(const char *s) { |
| 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 | |
| 51 | const char *format_utf32(const uint32_t *s) { |
| 52 | struct dynstr d; |
| 53 | uint32_t c; |
| 54 | char buf[64]; |
| 55 | |
| 56 | dynstr_init(&d); |
| 57 | while((c = *s++)) { |
| 58 | sprintf(buf, " %04lX", (long)c); |
| 59 | dynstr_append_string(&d, buf); |
| 60 | } |
| 61 | dynstr_terminate(&d); |
| 62 | return d.vec; |
| 63 | } |
| 64 | |
| 65 | uint32_t *ucs4parse(const char *s) { |
| 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 | |
| 80 | const char *do_printf(const char *fmt, ...) { |
| 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 | |
| 93 | int main(void) { |
| 94 | mem_init(); |
| 95 | fail_first = !!getenv("FAIL_FIRST"); |
| 96 | insist('\n' == 0x0A); |
| 97 | insist('\r' == 0x0D); |
| 98 | insist(' ' == 0x20); |
| 99 | insist('0' == 0x30); |
| 100 | insist('9' == 0x39); |
| 101 | insist('A' == 0x41); |
| 102 | insist('Z' == 0x5A); |
| 103 | insist('a' == 0x61); |
| 104 | insist('z' == 0x7A); |
| 105 | /* addr.c */ |
| 106 | test_addr(); |
| 107 | /* asprintf.c */ |
| 108 | /* authhash.c */ |
| 109 | /* basen.c */ |
| 110 | test_basen(); |
| 111 | /* charset.c */ |
| 112 | /* client.c */ |
| 113 | /* configuration.c */ |
| 114 | /* event.c */ |
| 115 | /* filepart.c */ |
| 116 | test_filepart(); |
| 117 | /* fprintf.c */ |
| 118 | /* heap.c */ |
| 119 | test_heap(); |
| 120 | /* hex.c */ |
| 121 | test_hex(); |
| 122 | /* inputline.c */ |
| 123 | /* kvp.c */ |
| 124 | test_kvp(); |
| 125 | /* log.c */ |
| 126 | /* mem.c */ |
| 127 | /* mime.c */ |
| 128 | test_mime(); |
| 129 | test_cookies(); |
| 130 | /* mixer.c */ |
| 131 | /* plugin.c */ |
| 132 | /* printf.c */ |
| 133 | test_printf(); |
| 134 | /* queue.c */ |
| 135 | /* sink.c */ |
| 136 | test_sink(); |
| 137 | /* snprintf.c */ |
| 138 | /* split.c */ |
| 139 | test_split(); |
| 140 | /* syscalls.c */ |
| 141 | /* table.c */ |
| 142 | /* unicode.c */ |
| 143 | test_unicode(); |
| 144 | /* utf8.c */ |
| 145 | test_utf8(); |
| 146 | /* vector.c */ |
| 147 | /* words.c */ |
| 148 | test_casefold(); |
| 149 | test_words(); |
| 150 | /* wstat.c */ |
| 151 | test_wstat(); |
| 152 | /* signame.c */ |
| 153 | test_signame(); |
| 154 | /* cache.c */ |
| 155 | test_cache(); |
| 156 | /* selection.c */ |
| 157 | test_selection(); |
| 158 | test_hash(); |
| 159 | test_url(); |
| 160 | test_regsub(); |
| 161 | fprintf(stderr, "%d errors out of %d tests\n", errors, tests); |
| 162 | return !!errors; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | Local Variables: |
| 167 | c-basic-offset:2 |
| 168 | comment-column:40 |
| 169 | fill-column:79 |
| 170 | indent-tabs-mode:nil |
| 171 | End: |
| 172 | */ |