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