| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2005, 2007 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 <config.h> |
| 23 | #include "types.h" |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <errno.h> |
| 29 | #include <ctype.h> |
| 30 | #include <assert.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <unistd.h> |
| 34 | #include <signal.h> |
| 35 | |
| 36 | #include "utf8.h" |
| 37 | #include "mem.h" |
| 38 | #include "log.h" |
| 39 | #include "vector.h" |
| 40 | #include "charset.h" |
| 41 | #include "mime.h" |
| 42 | #include "hex.h" |
| 43 | #include "heap.h" |
| 44 | #include "unicode.h" |
| 45 | #include "inputline.h" |
| 46 | #include "wstat.h" |
| 47 | #include "signame.h" |
| 48 | #include "cache.h" |
| 49 | #include "filepart.h" |
| 50 | #include "hash.h" |
| 51 | #include "selection.h" |
| 52 | |
| 53 | static int tests, errors; |
| 54 | static int fail_first; |
| 55 | |
| 56 | static void count_error() { |
| 57 | ++errors; |
| 58 | if(fail_first) |
| 59 | abort(); |
| 60 | } |
| 61 | |
| 62 | /** @brief Checks that @p expr is nonzero */ |
| 63 | #define insist(expr) do { \ |
| 64 | if(!(expr)) { \ |
| 65 | count_error(); \ |
| 66 | fprintf(stderr, "%s:%d: error checking %s\n", \ |
| 67 | __FILE__, __LINE__, #expr); \ |
| 68 | } \ |
| 69 | ++tests; \ |
| 70 | } while(0) |
| 71 | |
| 72 | static const char *format(const char *s) { |
| 73 | struct dynstr d; |
| 74 | int c; |
| 75 | char buf[10]; |
| 76 | |
| 77 | dynstr_init(&d); |
| 78 | while((c = (unsigned char)*s++)) { |
| 79 | if(c >= ' ' && c <= '~') |
| 80 | dynstr_append(&d, c); |
| 81 | else { |
| 82 | sprintf(buf, "\\x%02X", (unsigned)c); |
| 83 | dynstr_append_string(&d, buf); |
| 84 | } |
| 85 | } |
| 86 | dynstr_terminate(&d); |
| 87 | return d.vec; |
| 88 | } |
| 89 | |
| 90 | static const char *format_utf32(const uint32_t *s) { |
| 91 | struct dynstr d; |
| 92 | uint32_t c; |
| 93 | char buf[64]; |
| 94 | |
| 95 | dynstr_init(&d); |
| 96 | while((c = *s++)) { |
| 97 | sprintf(buf, " %04lX", (long)c); |
| 98 | dynstr_append_string(&d, buf); |
| 99 | } |
| 100 | dynstr_terminate(&d); |
| 101 | return d.vec; |
| 102 | } |
| 103 | |
| 104 | #define check_string(GOT, WANT) do { \ |
| 105 | const char *g = GOT; \ |
| 106 | const char *w = WANT; \ |
| 107 | \ |
| 108 | if(w == 0) { \ |
| 109 | fprintf(stderr, "%s:%d: %s returned 0\n", \ |
| 110 | __FILE__, __LINE__, #GOT); \ |
| 111 | count_error(); \ |
| 112 | } else if(strcmp(w, g)) { \ |
| 113 | fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \ |
| 114 | __FILE__, __LINE__, #GOT, format(g), format(w)); \ |
| 115 | count_error(); \ |
| 116 | } \ |
| 117 | ++tests; \ |
| 118 | } while(0) |
| 119 | |
| 120 | static uint32_t *ucs4parse(const char *s) { |
| 121 | struct dynstr_ucs4 d; |
| 122 | char *e; |
| 123 | |
| 124 | dynstr_ucs4_init(&d); |
| 125 | while(*s) { |
| 126 | errno = 0; |
| 127 | dynstr_ucs4_append(&d, strtoul(s, &e, 0)); |
| 128 | if(errno) fatal(errno, "strtoul (%s)", s); |
| 129 | s = e; |
| 130 | } |
| 131 | dynstr_ucs4_terminate(&d); |
| 132 | return d.vec; |
| 133 | } |
| 134 | |
| 135 | static void test_utf8(void) { |
| 136 | /* Test validutf8, convert to UCS-4, check the answer is right, |
| 137 | * convert back to UTF-8, check we got to where we started */ |
| 138 | #define U8(CHARS, WORDS) do { \ |
| 139 | uint32_t *w = ucs4parse(WORDS); \ |
| 140 | uint32_t *ucs; \ |
| 141 | char *u8; \ |
| 142 | \ |
| 143 | insist(validutf8(CHARS)); \ |
| 144 | ucs = utf8_to_utf32(CHARS, strlen(CHARS), 0); \ |
| 145 | insist(ucs != 0); \ |
| 146 | insist(!utf32_cmp(w, ucs)); \ |
| 147 | u8 = utf32_to_utf8(ucs, utf32_len(ucs), 0); \ |
| 148 | insist(u8 != 0); \ |
| 149 | insist(!strcmp(u8, CHARS)); \ |
| 150 | } while(0) |
| 151 | |
| 152 | fprintf(stderr, "test_utf8\n"); |
| 153 | #define validutf8(S) utf8_valid((S), strlen(S)) |
| 154 | |
| 155 | /* empty string */ |
| 156 | |
| 157 | U8("", ""); |
| 158 | |
| 159 | /* ASCII characters */ |
| 160 | |
| 161 | U8(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", |
| 162 | "0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d " |
| 163 | "0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a " |
| 164 | "0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 " |
| 165 | "0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 " |
| 166 | "0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 " |
| 167 | "0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e " |
| 168 | "0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b " |
| 169 | "0x7c 0x7d 0x7e"); |
| 170 | U8("\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\177", |
| 171 | "0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 " |
| 172 | "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d " |
| 173 | "0x1e 0x1f 0x7f"); |
| 174 | |
| 175 | /* from RFC3629 */ |
| 176 | |
| 177 | /* UTF8-2 = %xC2-DF UTF8-tail */ |
| 178 | insist(!validutf8("\xC0\x80")); |
| 179 | insist(!validutf8("\xC1\x80")); |
| 180 | insist(!validutf8("\xC2\x7F")); |
| 181 | U8("\xC2\x80", "0x80"); |
| 182 | U8("\xDF\xBF", "0x7FF"); |
| 183 | insist(!validutf8("\xDF\xC0")); |
| 184 | |
| 185 | /* UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) / |
| 186 | * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail ) |
| 187 | */ |
| 188 | insist(!validutf8("\xE0\x9F\x80")); |
| 189 | U8("\xE0\xA0\x80", "0x800"); |
| 190 | U8("\xE0\xBF\xBF", "0xFFF"); |
| 191 | insist(!validutf8("\xE0\xC0\xBF")); |
| 192 | |
| 193 | insist(!validutf8("\xE1\x80\x7F")); |
| 194 | U8("\xE1\x80\x80", "0x1000"); |
| 195 | U8("\xEC\xBF\xBF", "0xCFFF"); |
| 196 | insist(!validutf8("\xEC\xC0\xBF")); |
| 197 | |
| 198 | U8("\xED\x80\x80", "0xD000"); |
| 199 | U8("\xED\x9F\xBF", "0xD7FF"); |
| 200 | insist(!validutf8("\xED\xA0\xBF")); |
| 201 | |
| 202 | insist(!validutf8("\xEE\x7f\x80")); |
| 203 | U8("\xEE\x80\x80", "0xE000"); |
| 204 | U8("\xEF\xBF\xBF", "0xFFFF"); |
| 205 | insist(!validutf8("\xEF\xC0\xBF")); |
| 206 | |
| 207 | /* UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) / |
| 208 | * %xF4 %x80-8F 2( UTF8-tail ) |
| 209 | */ |
| 210 | insist(!validutf8("\xF0\x8F\x80\x80")); |
| 211 | U8("\xF0\x90\x80\x80", "0x10000"); |
| 212 | U8("\xF0\xBF\xBF\xBF", "0x3FFFF"); |
| 213 | insist(!validutf8("\xF0\xC0\x80\x80")); |
| 214 | |
| 215 | insist(!validutf8("\xF1\x80\x80\x7F")); |
| 216 | U8("\xF1\x80\x80\x80", "0x40000"); |
| 217 | U8("\xF3\xBF\xBF\xBF", "0xFFFFF"); |
| 218 | insist(!validutf8("\xF3\xC0\x80\x80")); |
| 219 | |
| 220 | insist(!validutf8("\xF4\x80\x80\x7F")); |
| 221 | U8("\xF4\x80\x80\x80", "0x100000"); |
| 222 | U8("\xF4\x8F\xBF\xBF", "0x10FFFF"); |
| 223 | insist(!validutf8("\xF4\x90\x80\x80")); |
| 224 | insist(!validutf8("\xF4\x80\xFF\x80")); |
| 225 | |
| 226 | /* miscellaneous non-UTF-8 rubbish */ |
| 227 | insist(!validutf8("\x80")); |
| 228 | insist(!validutf8("\xBF")); |
| 229 | insist(!validutf8("\xC0")); |
| 230 | insist(!validutf8("\xC0\x7F")); |
| 231 | insist(!validutf8("\xC0\xC0")); |
| 232 | insist(!validutf8("\xE0")); |
| 233 | insist(!validutf8("\xE0\x7F")); |
| 234 | insist(!validutf8("\xE0\xC0")); |
| 235 | insist(!validutf8("\xE0\x80")); |
| 236 | insist(!validutf8("\xE0\x80\x7f")); |
| 237 | insist(!validutf8("\xE0\x80\xC0")); |
| 238 | insist(!validutf8("\xF0")); |
| 239 | insist(!validutf8("\xF0\x7F")); |
| 240 | insist(!validutf8("\xF0\xC0")); |
| 241 | insist(!validutf8("\xF0\x80")); |
| 242 | insist(!validutf8("\xF0\x80\x7f")); |
| 243 | insist(!validutf8("\xF0\x80\xC0")); |
| 244 | insist(!validutf8("\xF0\x80\x80\x7f")); |
| 245 | insist(!validutf8("\xF0\x80\x80\xC0")); |
| 246 | insist(!validutf8("\xF5\x80\x80\x80")); |
| 247 | insist(!validutf8("\xF8")); |
| 248 | } |
| 249 | |
| 250 | static void test_mime(void) { |
| 251 | char *t, *n, *v; |
| 252 | |
| 253 | fprintf(stderr, "test_mime\n"); |
| 254 | |
| 255 | t = n = v = 0; |
| 256 | insist(!mime_content_type("text/plain", &t, &n, &v)); |
| 257 | insist(!strcmp(t, "text/plain")); |
| 258 | insist(n == 0); |
| 259 | insist(v == 0); |
| 260 | |
| 261 | t = n = v = 0; |
| 262 | insist(!mime_content_type("TEXT ((nested) comment) /plain", &t, &n, &v)); |
| 263 | insist(!strcmp(t, "text/plain")); |
| 264 | insist(n == 0); |
| 265 | insist(v == 0); |
| 266 | |
| 267 | t = n = v = 0; |
| 268 | insist(!mime_content_type(" text/plain ; Charset=utf-8", &t, &n, &v)); |
| 269 | insist(!strcmp(t, "text/plain")); |
| 270 | insist(!strcmp(n, "charset")); |
| 271 | insist(!strcmp(v, "utf-8")); |
| 272 | |
| 273 | t = n = v = 0; |
| 274 | insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t, &n, &v)); |
| 275 | insist(!strcmp(t, "text/plain")); |
| 276 | insist(!strcmp(n, "charset")); |
| 277 | insist(!strcmp(v, "ISO-8859-1")); |
| 278 | |
| 279 | /* XXX mime_parse */ |
| 280 | /* XXX mime_multipart */ |
| 281 | /* XXX mime_rfc2388_content_disposition */ |
| 282 | |
| 283 | check_string(mime_qp(""), ""); |
| 284 | check_string(mime_qp("foobar"), "foobar"); |
| 285 | check_string(mime_qp("foo=20bar"), "foo bar"); |
| 286 | check_string(mime_qp("x \r\ny"), "x\r\ny"); |
| 287 | check_string(mime_qp("x=\r\ny"), "xy"); |
| 288 | check_string(mime_qp("x= \r\ny"), "xy"); |
| 289 | check_string(mime_qp("x =\r\ny"), "x y"); |
| 290 | check_string(mime_qp("x = \r\ny"), "x y"); |
| 291 | |
| 292 | /* from RFC2045 */ |
| 293 | check_string(mime_qp("Now's the time =\r\n" |
| 294 | "for all folk to come=\r\n" |
| 295 | " to the aid of their country."), |
| 296 | "Now's the time for all folk to come to the aid of their country."); |
| 297 | |
| 298 | check_string(mime_base64(""), ""); |
| 299 | check_string(mime_base64("BBBB"), "\x04\x10\x41"); |
| 300 | check_string(mime_base64("////"), "\xFF\xFF\xFF"); |
| 301 | check_string(mime_base64("//BB"), "\xFF\xF0\x41"); |
| 302 | check_string(mime_base64("BBBB//BB////"), |
| 303 | "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF"); |
| 304 | check_string(mime_base64("B B B B / / B B / / / /"), |
| 305 | "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF"); |
| 306 | check_string(mime_base64("B\r\nBBB.// B-B//~//"), |
| 307 | "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF"); |
| 308 | check_string(mime_base64("BBBB="), |
| 309 | "\x04\x10\x41"); |
| 310 | check_string(mime_base64("BBBBx="), /* not actually valid base64 */ |
| 311 | "\x04\x10\x41"); |
| 312 | check_string(mime_base64("BBBB BB=="), |
| 313 | "\x04\x10\x41" "\x04"); |
| 314 | check_string(mime_base64("BBBB BBB="), |
| 315 | "\x04\x10\x41" "\x04\x10"); |
| 316 | } |
| 317 | |
| 318 | static void test_hex(void) { |
| 319 | unsigned n; |
| 320 | static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F }; |
| 321 | uint8_t *u; |
| 322 | size_t ul; |
| 323 | |
| 324 | fprintf(stderr, "test_hex\n"); |
| 325 | |
| 326 | for(n = 0; n <= UCHAR_MAX; ++n) { |
| 327 | if(!isxdigit(n)) |
| 328 | insist(unhexdigitq(n) == -1); |
| 329 | } |
| 330 | insist(unhexdigitq('0') == 0); |
| 331 | insist(unhexdigitq('1') == 1); |
| 332 | insist(unhexdigitq('2') == 2); |
| 333 | insist(unhexdigitq('3') == 3); |
| 334 | insist(unhexdigitq('4') == 4); |
| 335 | insist(unhexdigitq('5') == 5); |
| 336 | insist(unhexdigitq('6') == 6); |
| 337 | insist(unhexdigitq('7') == 7); |
| 338 | insist(unhexdigitq('8') == 8); |
| 339 | insist(unhexdigitq('9') == 9); |
| 340 | insist(unhexdigitq('a') == 10); |
| 341 | insist(unhexdigitq('b') == 11); |
| 342 | insist(unhexdigitq('c') == 12); |
| 343 | insist(unhexdigitq('d') == 13); |
| 344 | insist(unhexdigitq('e') == 14); |
| 345 | insist(unhexdigitq('f') == 15); |
| 346 | insist(unhexdigitq('A') == 10); |
| 347 | insist(unhexdigitq('B') == 11); |
| 348 | insist(unhexdigitq('C') == 12); |
| 349 | insist(unhexdigitq('D') == 13); |
| 350 | insist(unhexdigitq('E') == 14); |
| 351 | insist(unhexdigitq('F') == 15); |
| 352 | check_string(hex(h, sizeof h), "00ff807f"); |
| 353 | check_string(hex(0, 0), ""); |
| 354 | u = unhex("00ff807f", &ul); |
| 355 | insist(ul == 4); |
| 356 | insist(memcmp(u, h, 4) == 0); |
| 357 | u = unhex("00FF807F", &ul); |
| 358 | insist(ul == 4); |
| 359 | insist(memcmp(u, h, 4) == 0); |
| 360 | u = unhex("", &ul); |
| 361 | insist(ul == 0); |
| 362 | fprintf(stderr, "2 ERROR reports expected {\n"); |
| 363 | insist(unhex("F", 0) == 0); |
| 364 | insist(unhex("az", 0) == 0); |
| 365 | fprintf(stderr, "}\n"); |
| 366 | } |
| 367 | |
| 368 | static void test_casefold(void) { |
| 369 | uint32_t c, l; |
| 370 | const char *input, *canon_folded, *compat_folded, *canon_expected, *compat_expected; |
| 371 | |
| 372 | fprintf(stderr, "test_casefold\n"); |
| 373 | |
| 374 | /* This isn't a very exhaustive test. Unlike for normalization, there don't |
| 375 | * seem to be any public test vectors for these algorithms. */ |
| 376 | |
| 377 | for(c = 1; c < 256; ++c) { |
| 378 | input = utf32_to_utf8(&c, 1, 0); |
| 379 | canon_folded = utf8_casefold_canon(input, strlen(input), 0); |
| 380 | compat_folded = utf8_casefold_compat(input, strlen(input), 0); |
| 381 | switch(c) { |
| 382 | default: |
| 383 | if((c >= 'A' && c <= 'Z') |
| 384 | || (c >= 0xC0 && c <= 0xDE && c != 0xD7)) |
| 385 | l = c ^ 0x20; |
| 386 | else |
| 387 | l = c; |
| 388 | break; |
| 389 | case 0xB5: /* MICRO SIGN */ |
| 390 | l = 0x3BC; /* GREEK SMALL LETTER MU */ |
| 391 | break; |
| 392 | case 0xDF: /* LATIN SMALL LETTER SHARP S */ |
| 393 | insist(!strcmp(canon_folded, "ss")); |
| 394 | insist(!strcmp(compat_folded, "ss")); |
| 395 | l = 0; |
| 396 | break; |
| 397 | } |
| 398 | if(l) { |
| 399 | uint32_t *d; |
| 400 | /* Case-folded data is now normalized */ |
| 401 | d = utf32_decompose_canon(&l, 1, 0); |
| 402 | canon_expected = utf32_to_utf8(d, utf32_len(d), 0); |
| 403 | if(strcmp(canon_folded, canon_expected)) { |
| 404 | fprintf(stderr, "%s:%d: canon-casefolding %#lx got '%s', expected '%s'\n", |
| 405 | __FILE__, __LINE__, (unsigned long)c, |
| 406 | format(canon_folded), format(canon_expected)); |
| 407 | count_error(); |
| 408 | } |
| 409 | ++tests; |
| 410 | d = utf32_decompose_compat(&l, 1, 0); |
| 411 | compat_expected = utf32_to_utf8(d, utf32_len(d), 0); |
| 412 | if(strcmp(compat_folded, compat_expected)) { |
| 413 | fprintf(stderr, "%s:%d: compat-casefolding %#lx got '%s', expected '%s'\n", |
| 414 | __FILE__, __LINE__, (unsigned long)c, |
| 415 | format(compat_folded), format(compat_expected)); |
| 416 | count_error(); |
| 417 | } |
| 418 | ++tests; |
| 419 | } |
| 420 | } |
| 421 | check_string(utf8_casefold_canon("", 0, 0), ""); |
| 422 | } |
| 423 | |
| 424 | struct { |
| 425 | const char *in; |
| 426 | const char *expect[10]; |
| 427 | } wtest[] = { |
| 428 | /* Empty string */ |
| 429 | { "", { 0 } }, |
| 430 | /* Only whitespace and punctuation */ |
| 431 | { " ", { 0 } }, |
| 432 | { " ' ", { 0 } }, |
| 433 | { " ! ", { 0 } }, |
| 434 | { " \"\" ", { 0 } }, |
| 435 | { " @ ", { 0 } }, |
| 436 | /* Basics */ |
| 437 | { "wibble", { "wibble", 0 } }, |
| 438 | { " wibble", { "wibble", 0 } }, |
| 439 | { " wibble ", { "wibble", 0 } }, |
| 440 | { "wibble ", { "wibble", 0 } }, |
| 441 | { "wibble spong", { "wibble", "spong", 0 } }, |
| 442 | { " wibble spong", { "wibble", "spong", 0 } }, |
| 443 | { " wibble spong ", { "wibble", "spong", 0 } }, |
| 444 | { "wibble spong ", { "wibble", "spong", 0 } }, |
| 445 | { "wibble spong splat foo zot ", { "wibble", "spong", "splat", "foo", "zot", 0 } }, |
| 446 | /* Apostrophes */ |
| 447 | { "wibble 'spong", { "wibble", "spong", 0 } }, |
| 448 | { " wibble's", { "wibble's", 0 } }, |
| 449 | { " wibblespong' ", { "wibblespong", 0 } }, |
| 450 | { "wibble sp''ong ", { "wibble", "sp", "ong", 0 } }, |
| 451 | }; |
| 452 | #define NWTEST (sizeof wtest / sizeof *wtest) |
| 453 | |
| 454 | static void test_words(void) { |
| 455 | size_t t, nexpect, ngot, i; |
| 456 | int right; |
| 457 | |
| 458 | fprintf(stderr, "test_words\n"); |
| 459 | for(t = 0; t < NWTEST; ++t) { |
| 460 | char **got = utf8_word_split(wtest[t].in, strlen(wtest[t].in), &ngot, 0); |
| 461 | |
| 462 | for(nexpect = 0; wtest[t].expect[nexpect]; ++nexpect) |
| 463 | ; |
| 464 | if(nexpect == ngot) { |
| 465 | for(i = 0; i < ngot; ++i) |
| 466 | if(strcmp(wtest[t].expect[i], got[i])) |
| 467 | break; |
| 468 | right = i == ngot; |
| 469 | } else |
| 470 | right = 0; |
| 471 | if(!right) { |
| 472 | fprintf(stderr, "word split %zu failed\n", t); |
| 473 | fprintf(stderr, "input: %s\n", wtest[t].in); |
| 474 | fprintf(stderr, " | %-30s | %-30s\n", |
| 475 | "expected", "got"); |
| 476 | for(i = 0; i < nexpect || i < ngot; ++i) { |
| 477 | const char *e = i < nexpect ? wtest[t].expect[i] : "<none>"; |
| 478 | const char *g = i < ngot ? got[i] : "<none>"; |
| 479 | fprintf(stderr, " %2zu | %-30s | %-30s\n", i, e, g); |
| 480 | } |
| 481 | count_error(); |
| 482 | } |
| 483 | ++tests; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /** @brief Less-than comparison function for integer heap */ |
| 488 | static inline int int_lt(int a, int b) { return a < b; } |
| 489 | |
| 490 | /** @struct iheap |
| 491 | * @brief A heap with @c int elements */ |
| 492 | HEAP_TYPE(iheap, int, int_lt); |
| 493 | HEAP_DEFINE(iheap, int, int_lt); |
| 494 | |
| 495 | /** @brief Tests for @ref heap.h */ |
| 496 | static void test_heap(void) { |
| 497 | struct iheap h[1]; |
| 498 | int n; |
| 499 | int last = -1; |
| 500 | |
| 501 | fprintf(stderr, "test_heap\n"); |
| 502 | |
| 503 | iheap_init(h); |
| 504 | for(n = 0; n < 1000; ++n) |
| 505 | iheap_insert(h, random() % 100); |
| 506 | for(n = 0; n < 1000; ++n) { |
| 507 | const int latest = iheap_remove(h); |
| 508 | if(last > latest) |
| 509 | fprintf(stderr, "should have %d <= %d\n", last, latest); |
| 510 | insist(last <= latest); |
| 511 | last = latest; |
| 512 | } |
| 513 | putchar('\n'); |
| 514 | } |
| 515 | |
| 516 | /** @brief Open a Unicode test file */ |
| 517 | static FILE *open_unicode_test(const char *path) { |
| 518 | const char *base; |
| 519 | FILE *fp; |
| 520 | char buffer[1024]; |
| 521 | int w; |
| 522 | |
| 523 | if((base = strrchr(path, '/'))) |
| 524 | ++base; |
| 525 | else |
| 526 | base = path; |
| 527 | if(!(fp = fopen(base, "r"))) { |
| 528 | snprintf(buffer, sizeof buffer, |
| 529 | "wget http://www.unicode.org/Public/5.0.0/ucd/%s", path); |
| 530 | if((w = system(buffer))) |
| 531 | fatal(0, "%s: %s", buffer, wstat(w)); |
| 532 | if(chmod(base, 0444) < 0) |
| 533 | fatal(errno, "chmod %s", base); |
| 534 | if(!(fp = fopen(base, "r"))) |
| 535 | fatal(errno, "%s", base); |
| 536 | } |
| 537 | return fp; |
| 538 | } |
| 539 | |
| 540 | /** @brief Run breaking tests for utf32_grapheme_boundary() etc */ |
| 541 | static void breaktest(const char *path, |
| 542 | int (*breakfn)(const uint32_t *, size_t, size_t)) { |
| 543 | FILE *fp = open_unicode_test(path); |
| 544 | int lineno = 0; |
| 545 | char *l, *lp; |
| 546 | size_t bn, n; |
| 547 | char break_allowed[1024]; |
| 548 | uint32_t buffer[1024]; |
| 549 | |
| 550 | while(!inputline(path, fp, &l, '\n')) { |
| 551 | ++lineno; |
| 552 | if(l[0] == '#') continue; |
| 553 | bn = 0; |
| 554 | lp = l; |
| 555 | while(*lp) { |
| 556 | if(*lp == ' ' || *lp == '\t') { |
| 557 | ++lp; |
| 558 | continue; |
| 559 | } |
| 560 | if(*lp == '#') |
| 561 | break; |
| 562 | if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0xB7) { |
| 563 | /* 00F7 DIVISION SIGN */ |
| 564 | break_allowed[bn] = 1; |
| 565 | lp += 2; |
| 566 | continue; |
| 567 | } |
| 568 | if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0x97) { |
| 569 | /* 00D7 MULTIPLICATION SIGN */ |
| 570 | break_allowed[bn] = 0; |
| 571 | lp += 2; |
| 572 | continue; |
| 573 | } |
| 574 | if(isxdigit((unsigned char)*lp)) { |
| 575 | buffer[bn++] = strtoul(lp, &lp, 16); |
| 576 | continue; |
| 577 | } |
| 578 | fatal(0, "%s:%d: evil line: %s", path, lineno, l); |
| 579 | } |
| 580 | for(n = 0; n <= bn; ++n) { |
| 581 | if(breakfn(buffer, bn, n) != break_allowed[n]) { |
| 582 | fprintf(stderr, |
| 583 | "%s:%d: offset %zu: mismatch\n" |
| 584 | "%s\n" |
| 585 | "\n", |
| 586 | path, lineno, n, l); |
| 587 | count_error(); |
| 588 | } |
| 589 | ++tests; |
| 590 | } |
| 591 | xfree(l); |
| 592 | } |
| 593 | fclose(fp); |
| 594 | } |
| 595 | |
| 596 | /** @brief Tests for @ref lib/unicode.h */ |
| 597 | static void test_unicode(void) { |
| 598 | FILE *fp; |
| 599 | int lineno = 0; |
| 600 | char *l, *lp; |
| 601 | uint32_t buffer[1024]; |
| 602 | uint32_t *c[6], *NFD_c[6], *NFKD_c[6], *NFC_c[6], *NFKC_c[6]; /* 1-indexed */ |
| 603 | int cn, bn; |
| 604 | |
| 605 | fprintf(stderr, "test_unicode\n"); |
| 606 | fp = open_unicode_test("NormalizationTest.txt"); |
| 607 | while(!inputline("NormalizationTest.txt", fp, &l, '\n')) { |
| 608 | ++lineno; |
| 609 | if(*l == '#' || *l == '@') |
| 610 | continue; |
| 611 | bn = 0; |
| 612 | cn = 1; |
| 613 | lp = l; |
| 614 | c[cn++] = &buffer[bn]; |
| 615 | while(*lp && *lp != '#') { |
| 616 | if(*lp == ' ') { |
| 617 | ++lp; |
| 618 | continue; |
| 619 | } |
| 620 | if(*lp == ';') { |
| 621 | buffer[bn++] = 0; |
| 622 | if(cn == 6) |
| 623 | break; |
| 624 | c[cn++] = &buffer[bn]; |
| 625 | ++lp; |
| 626 | continue; |
| 627 | } |
| 628 | buffer[bn++] = strtoul(lp, &lp, 16); |
| 629 | } |
| 630 | buffer[bn] = 0; |
| 631 | assert(cn == 6); |
| 632 | for(cn = 1; cn <= 5; ++cn) { |
| 633 | NFD_c[cn] = utf32_decompose_canon(c[cn], utf32_len(c[cn]), 0); |
| 634 | NFKD_c[cn] = utf32_decompose_compat(c[cn], utf32_len(c[cn]), 0); |
| 635 | NFC_c[cn] = utf32_compose_canon(c[cn], utf32_len(c[cn]), 0); |
| 636 | NFKC_c[cn] = utf32_compose_compat(c[cn], utf32_len(c[cn]), 0); |
| 637 | } |
| 638 | #define unt_check(T, A, B) do { \ |
| 639 | ++tests; \ |
| 640 | if(utf32_cmp(c[A], T##_c[B])) { \ |
| 641 | fprintf(stderr, \ |
| 642 | "NormalizationTest.txt:%d: c%d != "#T"(c%d)\n", \ |
| 643 | lineno, A, B); \ |
| 644 | fprintf(stderr, " c%d:%s\n", \ |
| 645 | A, format_utf32(c[A])); \ |
| 646 | fprintf(stderr, " c%d:%s\n", \ |
| 647 | B, format_utf32(c[B])); \ |
| 648 | fprintf(stderr, "%4s(c%d):%s\n", \ |
| 649 | #T, B, format_utf32(T##_c[B])); \ |
| 650 | count_error(); \ |
| 651 | } \ |
| 652 | } while(0) |
| 653 | unt_check(NFD, 3, 1); |
| 654 | unt_check(NFD, 3, 2); |
| 655 | unt_check(NFD, 3, 3); |
| 656 | unt_check(NFD, 5, 4); |
| 657 | unt_check(NFD, 5, 5); |
| 658 | unt_check(NFKD, 5, 1); |
| 659 | unt_check(NFKD, 5, 2); |
| 660 | unt_check(NFKD, 5, 3); |
| 661 | unt_check(NFKD, 5, 4); |
| 662 | unt_check(NFKD, 5, 5); |
| 663 | unt_check(NFC, 2, 1); |
| 664 | unt_check(NFC, 2, 2); |
| 665 | unt_check(NFC, 2, 3); |
| 666 | unt_check(NFC, 4, 4); |
| 667 | unt_check(NFC, 4, 5); |
| 668 | unt_check(NFKC, 4, 1); |
| 669 | unt_check(NFKC, 4, 2); |
| 670 | unt_check(NFKC, 4, 3); |
| 671 | unt_check(NFKC, 4, 4); |
| 672 | unt_check(NFKC, 4, 5); |
| 673 | for(cn = 1; cn <= 5; ++cn) { |
| 674 | xfree(NFD_c[cn]); |
| 675 | xfree(NFKD_c[cn]); |
| 676 | } |
| 677 | xfree(l); |
| 678 | } |
| 679 | fclose(fp); |
| 680 | breaktest("auxiliary/GraphemeBreakTest.txt", utf32_is_grapheme_boundary); |
| 681 | breaktest("auxiliary/WordBreakTest.txt", utf32_is_word_boundary); |
| 682 | insist(utf32_combining_class(0x40000) == 0); |
| 683 | insist(utf32_combining_class(0xE0000) == 0); |
| 684 | } |
| 685 | |
| 686 | static void test_signame(void) { |
| 687 | fprintf(stderr, "test_signame\n"); |
| 688 | insist(find_signal("SIGTERM") == SIGTERM); |
| 689 | insist(find_signal("SIGHUP") == SIGHUP); |
| 690 | insist(find_signal("SIGINT") == SIGINT); |
| 691 | insist(find_signal("SIGQUIT") == SIGQUIT); |
| 692 | insist(find_signal("SIGKILL") == SIGKILL); |
| 693 | insist(find_signal("SIGYOURMUM") == -1); |
| 694 | } |
| 695 | |
| 696 | static void test_cache(void) { |
| 697 | const struct cache_type t1 = { 1 }, t2 = { 10 }; |
| 698 | const char v11[] = "spong", v12[] = "wibble", v2[] = "blat"; |
| 699 | fprintf(stderr, "test_cache\n"); |
| 700 | cache_put(&t1, "1_1", v11); |
| 701 | cache_put(&t1, "1_2", v12); |
| 702 | cache_put(&t2, "2", v2); |
| 703 | insist(cache_count() == 3); |
| 704 | insist(cache_get(&t2, "2") == v2); |
| 705 | insist(cache_get(&t1, "1_1") == v11); |
| 706 | insist(cache_get(&t1, "1_2") == v12); |
| 707 | insist(cache_get(&t1, "2") == 0); |
| 708 | insist(cache_get(&t2, "1_1") == 0); |
| 709 | insist(cache_get(&t2, "1_2") == 0); |
| 710 | insist(cache_get(&t1, "2") == 0); |
| 711 | insist(cache_get(&t2, "1_1") == 0); |
| 712 | insist(cache_get(&t2, "1_2") == 0); |
| 713 | sleep(2); |
| 714 | cache_expire(); |
| 715 | insist(cache_count() == 1); |
| 716 | insist(cache_get(&t1, "1_1") == 0); |
| 717 | insist(cache_get(&t1, "1_2") == 0); |
| 718 | insist(cache_get(&t2, "2") == v2); |
| 719 | cache_clean(0); |
| 720 | insist(cache_count() == 0); |
| 721 | insist(cache_get(&t2, "2") == 0); |
| 722 | } |
| 723 | |
| 724 | static void test_filepart(void) { |
| 725 | fprintf(stderr, "test_filepart\n"); |
| 726 | check_string(d_dirname("/"), "/"); |
| 727 | check_string(d_dirname("/spong"), "/"); |
| 728 | check_string(d_dirname("/foo/bar"), "/foo"); |
| 729 | check_string(d_dirname("./bar"), "."); |
| 730 | check_string(d_dirname("."), "."); |
| 731 | check_string(d_dirname(".."), "."); |
| 732 | check_string(d_dirname("../blat"), ".."); |
| 733 | check_string(d_dirname("wibble"), "."); |
| 734 | check_string(extension("foo.c"), ".c"); |
| 735 | check_string(extension(".c"), ".c"); |
| 736 | check_string(extension("."), "."); |
| 737 | check_string(extension("foo"), ""); |
| 738 | check_string(extension("./foo"), ""); |
| 739 | check_string(extension("./foo.c"), ".c"); |
| 740 | } |
| 741 | |
| 742 | static void test_selection(void) { |
| 743 | hash *h; |
| 744 | fprintf(stderr, "test_selection\n"); |
| 745 | insist((h = selection_new()) != 0); |
| 746 | selection_set(h, "one", 1); |
| 747 | selection_set(h, "two", 1); |
| 748 | selection_set(h, "three", 0); |
| 749 | selection_set(h, "four", 1); |
| 750 | insist(selection_selected(h, "one") == 1); |
| 751 | insist(selection_selected(h, "two") == 1); |
| 752 | insist(selection_selected(h, "three") == 0); |
| 753 | insist(selection_selected(h, "four") == 1); |
| 754 | insist(selection_selected(h, "five") == 0); |
| 755 | insist(hash_count(h) == 3); |
| 756 | selection_flip(h, "one"); |
| 757 | selection_flip(h, "three"); |
| 758 | insist(selection_selected(h, "one") == 0); |
| 759 | insist(selection_selected(h, "three") == 1); |
| 760 | insist(hash_count(h) == 3); |
| 761 | selection_live(h, "one"); |
| 762 | selection_live(h, "two"); |
| 763 | selection_live(h, "three"); |
| 764 | selection_cleanup(h); |
| 765 | insist(selection_selected(h, "one") == 0); |
| 766 | insist(selection_selected(h, "two") == 1); |
| 767 | insist(selection_selected(h, "three") == 1); |
| 768 | insist(selection_selected(h, "four") == 0); |
| 769 | insist(selection_selected(h, "five") == 0); |
| 770 | insist(hash_count(h) == 2); |
| 771 | selection_empty(h); |
| 772 | insist(selection_selected(h, "one") == 0); |
| 773 | insist(selection_selected(h, "two") == 0); |
| 774 | insist(selection_selected(h, "three") == 0); |
| 775 | insist(selection_selected(h, "four") == 0); |
| 776 | insist(selection_selected(h, "five") == 0); |
| 777 | insist(hash_count(h) == 0); |
| 778 | } |
| 779 | |
| 780 | int main(void) { |
| 781 | fail_first = !!getenv("FAIL_FIRST"); |
| 782 | insist('\n' == 0x0A); |
| 783 | insist('\r' == 0x0D); |
| 784 | insist(' ' == 0x20); |
| 785 | insist('0' == 0x30); |
| 786 | insist('9' == 0x39); |
| 787 | insist('A' == 0x41); |
| 788 | insist('Z' == 0x5A); |
| 789 | insist('a' == 0x61); |
| 790 | insist('z' == 0x7A); |
| 791 | /* addr.c */ |
| 792 | /* asprintf.c */ |
| 793 | /* authhash.c */ |
| 794 | /* basen.c */ |
| 795 | /* charset.c */ |
| 796 | /* client.c */ |
| 797 | /* configuration.c */ |
| 798 | /* event.c */ |
| 799 | /* filepart.c */ |
| 800 | test_filepart(); |
| 801 | /* fprintf.c */ |
| 802 | /* heap.c */ |
| 803 | test_heap(); |
| 804 | /* hex.c */ |
| 805 | test_hex(); |
| 806 | /* inputline.c */ |
| 807 | /* kvp.c */ |
| 808 | /* log.c */ |
| 809 | /* mem.c */ |
| 810 | /* mime.c */ |
| 811 | test_mime(); |
| 812 | /* mixer.c */ |
| 813 | /* plugin.c */ |
| 814 | /* printf.c */ |
| 815 | /* queue.c */ |
| 816 | /* sink.c */ |
| 817 | /* snprintf.c */ |
| 818 | /* split.c */ |
| 819 | /* syscalls.c */ |
| 820 | /* table.c */ |
| 821 | /* unicode.c */ |
| 822 | test_unicode(); |
| 823 | /* utf8.c */ |
| 824 | test_utf8(); |
| 825 | /* vector.c */ |
| 826 | /* words.c */ |
| 827 | test_casefold(); |
| 828 | test_words(); |
| 829 | /* wstat.c */ |
| 830 | /* signame.c */ |
| 831 | test_signame(); |
| 832 | /* cache.c */ |
| 833 | test_cache(); |
| 834 | /* selection.c */ |
| 835 | test_selection(); |
| 836 | fprintf(stderr, "%d errors out of %d tests\n", errors, tests); |
| 837 | return !!errors; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | Local Variables: |
| 842 | c-basic-offset:2 |
| 843 | comment-column:40 |
| 844 | fill-column:79 |
| 845 | indent-tabs-mode:nil |
| 846 | End: |
| 847 | */ |