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