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