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