chiark / gitweb /
hash.c tests
[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>
0c740e59 36#include <stddef.h>
460b9539 37
38#include "utf8.h"
39#include "mem.h"
40#include "log.h"
41#include "vector.h"
42#include "charset.h"
43#include "mime.h"
44#include "hex.h"
033fd4e3 45#include "heap.h"
e5a5a138
RK
46#include "unicode.h"
47#include "inputline.h"
e2452add 48#include "wstat.h"
ea387d53 49#include "signame.h"
9f28e855 50#include "cache.h"
00e36cd0 51#include "filepart.h"
65bb0fff
RK
52#include "hash.h"
53#include "selection.h"
71b90230 54#include "syscalls.h"
22f61603 55#include "kvp.h"
6049fe0e 56#include "sink.h"
0c740e59 57#include "printf.h"
abe28bfe 58#include "basen.h"
5e49fa7f 59#include "split.h"
460b9539 60
61static int tests, errors;
bb48024f
RK
62static int fail_first;
63
64static void count_error() {
65 ++errors;
66 if(fail_first)
67 abort();
68}
460b9539 69
033fd4e3 70/** @brief Checks that @p expr is nonzero */
460b9539 71#define insist(expr) do { \
033fd4e3 72 if(!(expr)) { \
bb48024f 73 count_error(); \
460b9539 74 fprintf(stderr, "%s:%d: error checking %s\n", \
75 __FILE__, __LINE__, #expr); \
76 } \
77 ++tests; \
78} while(0)
79
80static const char *format(const char *s) {
81 struct dynstr d;
82 int c;
83 char buf[10];
84
85 dynstr_init(&d);
86 while((c = (unsigned char)*s++)) {
87 if(c >= ' ' && c <= '~')
88 dynstr_append(&d, c);
89 else {
90 sprintf(buf, "\\x%02X", (unsigned)c);
91 dynstr_append_string(&d, buf);
92 }
93 }
94 dynstr_terminate(&d);
95 return d.vec;
96}
97
e5a5a138
RK
98static const char *format_utf32(const uint32_t *s) {
99 struct dynstr d;
100 uint32_t c;
101 char buf[64];
102
103 dynstr_init(&d);
104 while((c = *s++)) {
16506c9d
RK
105 sprintf(buf, " %04lX", (long)c);
106 dynstr_append_string(&d, buf);
e5a5a138
RK
107 }
108 dynstr_terminate(&d);
109 return d.vec;
110}
111
6f4d1e2d
RK
112#define check_string(GOT, WANT) do { \
113 const char *got = GOT; \
114 const char *want = WANT; \
115 \
116 if(want == 0) { \
117 fprintf(stderr, "%s:%d: %s returned 0\n", \
118 __FILE__, __LINE__, #GOT); \
119 count_error(); \
120 } else if(strcmp(want, got)) { \
121 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \
122 __FILE__, __LINE__, #GOT, format(got), format(want)); \
123 count_error(); \
124 } \
125 ++tests; \
460b9539 126 } while(0)
127
71b90230 128#define check_string_prefix(GOT, WANT) do { \
6f4d1e2d
RK
129 const char *got = GOT; \
130 const char *want = WANT; \
71b90230 131 \
6f4d1e2d 132 if(want == 0) { \
71b90230
RK
133 fprintf(stderr, "%s:%d: %s returned 0\n", \
134 __FILE__, __LINE__, #GOT); \
6f4d1e2d
RK
135 count_error(); \
136 } else if(strncmp(want, got, strlen(want))) { \
137 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n", \
138 __FILE__, __LINE__, #GOT, format(got), format(want)); \
139 count_error(); \
71b90230
RK
140 } \
141 ++tests; \
142 } while(0)
143
22896b25
RK
144#define check_integer(GOT, WANT) do { \
145 const intmax_t got = GOT, want = WANT; \
146 if(got != want) { \
147 fprintf(stderr, "%s:%d: %s returned: %jd expected: %jd\n", \
148 __FILE__, __LINE__, #GOT, got, want); \
149 count_error(); \
150 } \
151 ++tests; \
152} while(0)
153
460b9539 154static uint32_t *ucs4parse(const char *s) {
155 struct dynstr_ucs4 d;
156 char *e;
157
158 dynstr_ucs4_init(&d);
159 while(*s) {
160 errno = 0;
161 dynstr_ucs4_append(&d, strtoul(s, &e, 0));
162 if(errno) fatal(errno, "strtoul (%s)", s);
163 s = e;
164 }
165 dynstr_ucs4_terminate(&d);
166 return d.vec;
167}
168
169static void test_utf8(void) {
170 /* Test validutf8, convert to UCS-4, check the answer is right,
171 * convert back to UTF-8, check we got to where we started */
172#define U8(CHARS, WORDS) do { \
173 uint32_t *w = ucs4parse(WORDS); \
174 uint32_t *ucs; \
175 char *u8; \
176 \
177 insist(validutf8(CHARS)); \
caecd4f4 178 ucs = utf8_to_utf32(CHARS, strlen(CHARS), 0); \
460b9539 179 insist(ucs != 0); \
caecd4f4
RK
180 insist(!utf32_cmp(w, ucs)); \
181 u8 = utf32_to_utf8(ucs, utf32_len(ucs), 0); \
460b9539 182 insist(u8 != 0); \
22896b25 183 check_string(u8, CHARS); \
460b9539 184} while(0)
185
033fd4e3 186 fprintf(stderr, "test_utf8\n");
18cda350 187#define validutf8(S) utf8_valid((S), strlen(S))
033fd4e3 188
460b9539 189 /* empty string */
190
191 U8("", "");
192
193 /* ASCII characters */
194
195 U8(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
196 "0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d "
197 "0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a "
198 "0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 "
199 "0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 "
200 "0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 "
201 "0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e "
202 "0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b "
203 "0x7c 0x7d 0x7e");
204 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",
205 "0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 "
206 "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d "
207 "0x1e 0x1f 0x7f");
208
209 /* from RFC3629 */
210
211 /* UTF8-2 = %xC2-DF UTF8-tail */
212 insist(!validutf8("\xC0\x80"));
213 insist(!validutf8("\xC1\x80"));
214 insist(!validutf8("\xC2\x7F"));
215 U8("\xC2\x80", "0x80");
216 U8("\xDF\xBF", "0x7FF");
217 insist(!validutf8("\xDF\xC0"));
218
219 /* UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
220 * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
221 */
222 insist(!validutf8("\xE0\x9F\x80"));
223 U8("\xE0\xA0\x80", "0x800");
224 U8("\xE0\xBF\xBF", "0xFFF");
225 insist(!validutf8("\xE0\xC0\xBF"));
226
227 insist(!validutf8("\xE1\x80\x7F"));
228 U8("\xE1\x80\x80", "0x1000");
229 U8("\xEC\xBF\xBF", "0xCFFF");
230 insist(!validutf8("\xEC\xC0\xBF"));
231
232 U8("\xED\x80\x80", "0xD000");
233 U8("\xED\x9F\xBF", "0xD7FF");
234 insist(!validutf8("\xED\xA0\xBF"));
235
236 insist(!validutf8("\xEE\x7f\x80"));
237 U8("\xEE\x80\x80", "0xE000");
238 U8("\xEF\xBF\xBF", "0xFFFF");
239 insist(!validutf8("\xEF\xC0\xBF"));
240
241 /* UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
242 * %xF4 %x80-8F 2( UTF8-tail )
243 */
244 insist(!validutf8("\xF0\x8F\x80\x80"));
245 U8("\xF0\x90\x80\x80", "0x10000");
246 U8("\xF0\xBF\xBF\xBF", "0x3FFFF");
247 insist(!validutf8("\xF0\xC0\x80\x80"));
248
249 insist(!validutf8("\xF1\x80\x80\x7F"));
250 U8("\xF1\x80\x80\x80", "0x40000");
251 U8("\xF3\xBF\xBF\xBF", "0xFFFFF");
252 insist(!validutf8("\xF3\xC0\x80\x80"));
253
254 insist(!validutf8("\xF4\x80\x80\x7F"));
255 U8("\xF4\x80\x80\x80", "0x100000");
256 U8("\xF4\x8F\xBF\xBF", "0x10FFFF");
257 insist(!validutf8("\xF4\x90\x80\x80"));
c35e83d9 258 insist(!validutf8("\xF4\x80\xFF\x80"));
460b9539 259
260 /* miscellaneous non-UTF-8 rubbish */
261 insist(!validutf8("\x80"));
262 insist(!validutf8("\xBF"));
263 insist(!validutf8("\xC0"));
264 insist(!validutf8("\xC0\x7F"));
265 insist(!validutf8("\xC0\xC0"));
266 insist(!validutf8("\xE0"));
267 insist(!validutf8("\xE0\x7F"));
268 insist(!validutf8("\xE0\xC0"));
269 insist(!validutf8("\xE0\x80"));
270 insist(!validutf8("\xE0\x80\x7f"));
271 insist(!validutf8("\xE0\x80\xC0"));
272 insist(!validutf8("\xF0"));
273 insist(!validutf8("\xF0\x7F"));
274 insist(!validutf8("\xF0\xC0"));
275 insist(!validutf8("\xF0\x80"));
276 insist(!validutf8("\xF0\x80\x7f"));
277 insist(!validutf8("\xF0\x80\xC0"));
278 insist(!validutf8("\xF0\x80\x80\x7f"));
279 insist(!validutf8("\xF0\x80\x80\xC0"));
280 insist(!validutf8("\xF5\x80\x80\x80"));
281 insist(!validutf8("\xF8"));
282}
283
22896b25
RK
284static int test_multipart_callback(const char *s, void *u) {
285 struct vector *parts = u;
286
287 vector_append(parts, (char *)s);
288 return 0;
289}
290
460b9539 291static void test_mime(void) {
292 char *t, *n, *v;
22896b25 293 struct vector parts[1];
460b9539 294
033fd4e3
RK
295 fprintf(stderr, "test_mime\n");
296
460b9539 297 t = n = v = 0;
298 insist(!mime_content_type("text/plain", &t, &n, &v));
22896b25 299 check_string(t, "text/plain");
460b9539 300 insist(n == 0);
301 insist(v == 0);
302
22896b25
RK
303 insist(mime_content_type("TEXT ((broken) comment", &t, &n, &v) < 0);
304 insist(mime_content_type("TEXT ((broken) comment\\", &t, &n, &v) < 0);
305
460b9539 306 t = n = v = 0;
22896b25
RK
307 insist(!mime_content_type("TEXT ((nested)\\ comment) /plain", &t, &n, &v));
308 check_string(t, "text/plain");
460b9539 309 insist(n == 0);
310 insist(v == 0);
311
312 t = n = v = 0;
22896b25
RK
313 insist(!mime_content_type(" text/plain ; Charset=\"utf-\\8\"", &t, &n, &v));
314 check_string(t, "text/plain");
315 check_string(n, "charset");
316 check_string(v, "utf-8");
460b9539 317
318 t = n = v = 0;
319 insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t, &n, &v));
22896b25
RK
320 check_string(t, "text/plain");
321 check_string(n, "charset");
322 check_string(v, "ISO-8859-1");
323
324 t = n = v = 0;
325 insist(!mime_rfc2388_content_disposition("form-data; name=\"field1\"", &t, &n, &v));
326 check_string(t, "form-data");
327 check_string(n, "name");
328 check_string(v, "field1");
329
330 insist(!mime_rfc2388_content_disposition("inline", &t, &n, &v));
331 check_string(t, "inline");
332 insist(n == 0);
333 insist(v == 0);
460b9539 334
22896b25
RK
335 /* Current versions of the code only understand a single arg to these
336 * headers. This is a bug at the level they work at but suffices for
337 * DisOrder's current purposes. */
338
339 insist(!mime_rfc2388_content_disposition(
340 "attachment; filename=genome.jpeg;\n"
341 "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"",
342 &t, &n, &v));
343 check_string(t, "attachment");
344 check_string(n, "filename");
345 check_string(v, "genome.jpeg");
346
347 vector_init(parts);
348 insist(mime_multipart("--outer\r\n"
349 "Content-Type: text/plain\r\n"
350 "Content-Disposition: inline\r\n"
351 "Content-Description: text-part-1\r\n"
352 "\r\n"
353 "Some text goes here\r\n"
354 "\r\n"
355 "--outer\r\n"
356 "Content-Type: multipart/mixed; boundary=inner\r\n"
357 "Content-Disposition: attachment\r\n"
358 "Content-Description: multipart-2\r\n"
359 "\r\n"
360 "--inner\r\n"
361 "Content-Type: text/plain\r\n"
362 "Content-Disposition: inline\r\n"
363 "Content-Description: text-part-2\r\n"
364 "\r\n"
365 "Some more text here.\r\n"
366 "\r\n"
367 "--inner\r\n"
368 "Content-Type: image/jpeg\r\n"
369 "Content-Disposition: attachment\r\n"
370 "Content-Description: jpeg-1\r\n"
371 "\r\n"
372 "<jpeg data>\r\n"
373 "--inner--\r\n"
374 "--outer--\r\n",
375 test_multipart_callback,
376 "outer",
377 parts) == 0);
378 check_integer(parts->nvec, 2);
379 check_string(parts->vec[0],
380 "Content-Type: text/plain\r\n"
381 "Content-Disposition: inline\r\n"
382 "Content-Description: text-part-1\r\n"
383 "\r\n"
384 "Some text goes here\r\n");
385 check_string(parts->vec[1],
386 "Content-Type: multipart/mixed; boundary=inner\r\n"
387 "Content-Disposition: attachment\r\n"
388 "Content-Description: multipart-2\r\n"
389 "\r\n"
390 "--inner\r\n"
391 "Content-Type: text/plain\r\n"
392 "Content-Disposition: inline\r\n"
393 "Content-Description: text-part-2\r\n"
394 "\r\n"
395 "Some more text here.\r\n"
396 "\r\n"
397 "--inner\r\n"
398 "Content-Type: image/jpeg\r\n"
399 "Content-Disposition: attachment\r\n"
400 "Content-Description: jpeg-1\r\n"
401 "\r\n"
402 "<jpeg data>\r\n"
403 "--inner--");
404 /* No trailing CRLF is _correct_ - see RFC2046 5.1.1 note regarding CRLF
405 * preceding the boundary delimiter line. An implication of this is that we
406 * must cope with partial lines at the end of the input when recursively
407 * decomposing a multipart message. */
408 vector_init(parts);
409 insist(mime_multipart("--inner\r\n"
410 "Content-Type: text/plain\r\n"
411 "Content-Disposition: inline\r\n"
412 "Content-Description: text-part-2\r\n"
413 "\r\n"
414 "Some more text here.\r\n"
415 "\r\n"
416 "--inner\r\n"
417 "Content-Type: image/jpeg\r\n"
418 "Content-Disposition: attachment\r\n"
419 "Content-Description: jpeg-1\r\n"
420 "\r\n"
421 "<jpeg data>\r\n"
422 "--inner--",
423 test_multipart_callback,
424 "inner",
425 parts) == 0);
426 check_integer(parts->nvec, 2);
427 check_string(parts->vec[0],
428 "Content-Type: text/plain\r\n"
429 "Content-Disposition: inline\r\n"
430 "Content-Description: text-part-2\r\n"
431 "\r\n"
432 "Some more text here.\r\n");
433 check_string(parts->vec[1],
434 "Content-Type: image/jpeg\r\n"
435 "Content-Disposition: attachment\r\n"
436 "Content-Description: jpeg-1\r\n"
437 "\r\n"
438 "<jpeg data>");
439
460b9539 440 /* XXX mime_parse */
460b9539 441
442 check_string(mime_qp(""), "");
443 check_string(mime_qp("foobar"), "foobar");
444 check_string(mime_qp("foo=20bar"), "foo bar");
445 check_string(mime_qp("x \r\ny"), "x\r\ny");
446 check_string(mime_qp("x=\r\ny"), "xy");
447 check_string(mime_qp("x= \r\ny"), "xy");
448 check_string(mime_qp("x =\r\ny"), "x y");
449 check_string(mime_qp("x = \r\ny"), "x y");
450
451 /* from RFC2045 */
452 check_string(mime_qp("Now's the time =\r\n"
453"for all folk to come=\r\n"
454" to the aid of their country."),
455 "Now's the time for all folk to come to the aid of their country.");
456
457 check_string(mime_base64(""), "");
458 check_string(mime_base64("BBBB"), "\x04\x10\x41");
459 check_string(mime_base64("////"), "\xFF\xFF\xFF");
460 check_string(mime_base64("//BB"), "\xFF\xF0\x41");
461 check_string(mime_base64("BBBB//BB////"),
462 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
463 check_string(mime_base64("B B B B / / B B / / / /"),
464 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
465 check_string(mime_base64("B\r\nBBB.// B-B//~//"),
466 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
467 check_string(mime_base64("BBBB="),
468 "\x04\x10\x41");
469 check_string(mime_base64("BBBBx="), /* not actually valid base64 */
470 "\x04\x10\x41");
471 check_string(mime_base64("BBBB BB=="),
472 "\x04\x10\x41" "\x04");
473 check_string(mime_base64("BBBB BBB="),
474 "\x04\x10\x41" "\x04\x10");
475}
476
477static void test_hex(void) {
478 unsigned n;
479 static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F };
480 uint8_t *u;
481 size_t ul;
482
033fd4e3
RK
483 fprintf(stderr, "test_hex\n");
484
460b9539 485 for(n = 0; n <= UCHAR_MAX; ++n) {
486 if(!isxdigit(n))
487 insist(unhexdigitq(n) == -1);
488 }
489 insist(unhexdigitq('0') == 0);
490 insist(unhexdigitq('1') == 1);
491 insist(unhexdigitq('2') == 2);
492 insist(unhexdigitq('3') == 3);
493 insist(unhexdigitq('4') == 4);
494 insist(unhexdigitq('5') == 5);
495 insist(unhexdigitq('6') == 6);
496 insist(unhexdigitq('7') == 7);
497 insist(unhexdigitq('8') == 8);
498 insist(unhexdigitq('9') == 9);
499 insist(unhexdigitq('a') == 10);
500 insist(unhexdigitq('b') == 11);
501 insist(unhexdigitq('c') == 12);
502 insist(unhexdigitq('d') == 13);
503 insist(unhexdigitq('e') == 14);
504 insist(unhexdigitq('f') == 15);
505 insist(unhexdigitq('A') == 10);
506 insist(unhexdigitq('B') == 11);
507 insist(unhexdigitq('C') == 12);
508 insist(unhexdigitq('D') == 13);
509 insist(unhexdigitq('E') == 14);
510 insist(unhexdigitq('F') == 15);
511 check_string(hex(h, sizeof h), "00ff807f");
512 check_string(hex(0, 0), "");
513 u = unhex("00ff807f", &ul);
514 insist(ul == 4);
515 insist(memcmp(u, h, 4) == 0);
516 u = unhex("00FF807F", &ul);
517 insist(ul == 4);
518 insist(memcmp(u, h, 4) == 0);
519 u = unhex("", &ul);
520 insist(ul == 0);
033fd4e3 521 fprintf(stderr, "2 ERROR reports expected {\n");
460b9539 522 insist(unhex("F", 0) == 0);
523 insist(unhex("az", 0) == 0);
033fd4e3 524 fprintf(stderr, "}\n");
460b9539 525}
526
527static void test_casefold(void) {
e5a5a138 528 uint32_t c, l;
56fd389c 529 const char *input, *canon_folded, *compat_folded, *canon_expected, *compat_expected;
460b9539 530
033fd4e3 531 fprintf(stderr, "test_casefold\n");
56fd389c
RK
532
533 /* This isn't a very exhaustive test. Unlike for normalization, there don't
534 * seem to be any public test vectors for these algorithms. */
e5a5a138 535
460b9539 536 for(c = 1; c < 256; ++c) {
e5a5a138 537 input = utf32_to_utf8(&c, 1, 0);
56fd389c
RK
538 canon_folded = utf8_casefold_canon(input, strlen(input), 0);
539 compat_folded = utf8_casefold_compat(input, strlen(input), 0);
460b9539 540 switch(c) {
541 default:
542 if((c >= 'A' && c <= 'Z')
543 || (c >= 0xC0 && c <= 0xDE && c != 0xD7))
544 l = c ^ 0x20;
545 else
546 l = c;
547 break;
548 case 0xB5: /* MICRO SIGN */
e5a5a138 549 l = 0x3BC; /* GREEK SMALL LETTER MU */
460b9539 550 break;
551 case 0xDF: /* LATIN SMALL LETTER SHARP S */
22896b25
RK
552 check_string(canon_folded, "ss");
553 check_string(compat_folded, "ss");
460b9539 554 l = 0;
555 break;
556 }
557 if(l) {
caecd4f4 558 uint32_t *d;
e5a5a138 559 /* Case-folded data is now normalized */
caecd4f4
RK
560 d = utf32_decompose_canon(&l, 1, 0);
561 canon_expected = utf32_to_utf8(d, utf32_len(d), 0);
56fd389c
RK
562 if(strcmp(canon_folded, canon_expected)) {
563 fprintf(stderr, "%s:%d: canon-casefolding %#lx got '%s', expected '%s'\n",
564 __FILE__, __LINE__, (unsigned long)c,
565 format(canon_folded), format(canon_expected));
bb48024f 566 count_error();
56fd389c
RK
567 }
568 ++tests;
caecd4f4
RK
569 d = utf32_decompose_compat(&l, 1, 0);
570 compat_expected = utf32_to_utf8(d, utf32_len(d), 0);
56fd389c
RK
571 if(strcmp(compat_folded, compat_expected)) {
572 fprintf(stderr, "%s:%d: compat-casefolding %#lx got '%s', expected '%s'\n",
460b9539 573 __FILE__, __LINE__, (unsigned long)c,
56fd389c 574 format(compat_folded), format(compat_expected));
bb48024f 575 count_error();
460b9539 576 }
577 ++tests;
578 }
579 }
c85b7022 580 check_string(utf8_casefold_canon("", 0, 0), "");
460b9539 581}
582
8818b7fc
RK
583struct {
584 const char *in;
585 const char *expect[10];
586} wtest[] = {
587 /* Empty string */
588 { "", { 0 } },
589 /* Only whitespace and punctuation */
590 { " ", { 0 } },
591 { " ' ", { 0 } },
592 { " ! ", { 0 } },
593 { " \"\" ", { 0 } },
594 { " @ ", { 0 } },
595 /* Basics */
596 { "wibble", { "wibble", 0 } },
597 { " wibble", { "wibble", 0 } },
598 { " wibble ", { "wibble", 0 } },
599 { "wibble ", { "wibble", 0 } },
600 { "wibble spong", { "wibble", "spong", 0 } },
601 { " wibble spong", { "wibble", "spong", 0 } },
602 { " wibble spong ", { "wibble", "spong", 0 } },
603 { "wibble spong ", { "wibble", "spong", 0 } },
604 { "wibble spong splat foo zot ", { "wibble", "spong", "splat", "foo", "zot", 0 } },
605 /* Apostrophes */
606 { "wibble 'spong", { "wibble", "spong", 0 } },
607 { " wibble's", { "wibble's", 0 } },
608 { " wibblespong' ", { "wibblespong", 0 } },
609 { "wibble sp''ong ", { "wibble", "sp", "ong", 0 } },
610};
611#define NWTEST (sizeof wtest / sizeof *wtest)
612
613static void test_words(void) {
614 size_t t, nexpect, ngot, i;
615 int right;
616
617 fprintf(stderr, "test_words\n");
618 for(t = 0; t < NWTEST; ++t) {
c85b7022 619 char **got = utf8_word_split(wtest[t].in, strlen(wtest[t].in), &ngot, 0);
8818b7fc
RK
620
621 for(nexpect = 0; wtest[t].expect[nexpect]; ++nexpect)
622 ;
623 if(nexpect == ngot) {
624 for(i = 0; i < ngot; ++i)
625 if(strcmp(wtest[t].expect[i], got[i]))
626 break;
627 right = i == ngot;
628 } else
629 right = 0;
630 if(!right) {
631 fprintf(stderr, "word split %zu failed\n", t);
632 fprintf(stderr, "input: %s\n", wtest[t].in);
633 fprintf(stderr, " | %-30s | %-30s\n",
634 "expected", "got");
635 for(i = 0; i < nexpect || i < ngot; ++i) {
636 const char *e = i < nexpect ? wtest[t].expect[i] : "<none>";
637 const char *g = i < ngot ? got[i] : "<none>";
638 fprintf(stderr, " %2zu | %-30s | %-30s\n", i, e, g);
639 }
640 count_error();
641 }
642 ++tests;
643 }
644}
645
033fd4e3
RK
646/** @brief Less-than comparison function for integer heap */
647static inline int int_lt(int a, int b) { return a < b; }
648
dab22732
RK
649/** @struct iheap
650 * @brief A heap with @c int elements */
033fd4e3 651HEAP_TYPE(iheap, int, int_lt);
8e3fe3d8 652HEAP_DEFINE(iheap, int, int_lt);
033fd4e3
RK
653
654/** @brief Tests for @ref heap.h */
655static void test_heap(void) {
656 struct iheap h[1];
657 int n;
658 int last = -1;
659
660 fprintf(stderr, "test_heap\n");
661
662 iheap_init(h);
663 for(n = 0; n < 1000; ++n)
664 iheap_insert(h, random() % 100);
665 for(n = 0; n < 1000; ++n) {
666 const int latest = iheap_remove(h);
667 if(last > latest)
668 fprintf(stderr, "should have %d <= %d\n", last, latest);
669 insist(last <= latest);
670 last = latest;
671 }
672 putchar('\n');
673}
674
e2452add
RK
675/** @brief Open a Unicode test file */
676static FILE *open_unicode_test(const char *path) {
677 const char *base;
678 FILE *fp;
679 char buffer[1024];
680 int w;
681
682 if((base = strrchr(path, '/')))
683 ++base;
684 else
685 base = path;
686 if(!(fp = fopen(base, "r"))) {
687 snprintf(buffer, sizeof buffer,
688 "wget http://www.unicode.org/Public/5.0.0/ucd/%s", path);
689 if((w = system(buffer)))
690 fatal(0, "%s: %s", buffer, wstat(w));
691 if(chmod(base, 0444) < 0)
692 fatal(errno, "chmod %s", base);
693 if(!(fp = fopen(base, "r")))
694 fatal(errno, "%s", base);
695 }
696 return fp;
697}
698
1625e11a 699/** @brief Run breaking tests for utf32_grapheme_boundary() etc */
bb48024f
RK
700static void breaktest(const char *path,
701 int (*breakfn)(const uint32_t *, size_t, size_t)) {
702 FILE *fp = open_unicode_test(path);
703 int lineno = 0;
704 char *l, *lp;
705 size_t bn, n;
706 char break_allowed[1024];
707 uint32_t buffer[1024];
708
709 while(!inputline(path, fp, &l, '\n')) {
710 ++lineno;
711 if(l[0] == '#') continue;
712 bn = 0;
713 lp = l;
714 while(*lp) {
715 if(*lp == ' ' || *lp == '\t') {
716 ++lp;
717 continue;
718 }
719 if(*lp == '#')
720 break;
721 if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0xB7) {
722 /* 00F7 DIVISION SIGN */
723 break_allowed[bn] = 1;
724 lp += 2;
725 continue;
726 }
727 if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0x97) {
728 /* 00D7 MULTIPLICATION SIGN */
729 break_allowed[bn] = 0;
730 lp += 2;
731 continue;
732 }
733 if(isxdigit((unsigned char)*lp)) {
734 buffer[bn++] = strtoul(lp, &lp, 16);
735 continue;
736 }
737 fatal(0, "%s:%d: evil line: %s", path, lineno, l);
738 }
739 for(n = 0; n <= bn; ++n) {
740 if(breakfn(buffer, bn, n) != break_allowed[n]) {
741 fprintf(stderr,
b21a155c
RK
742 "%s:%d: offset %zu: mismatch\n"
743 "%s\n"
744 "\n",
745 path, lineno, n, l);
bb48024f
RK
746 count_error();
747 }
748 ++tests;
749 }
750 xfree(l);
751 }
752 fclose(fp);
753}
754
e5a5a138
RK
755/** @brief Tests for @ref lib/unicode.h */
756static void test_unicode(void) {
757 FILE *fp;
758 int lineno = 0;
759 char *l, *lp;
760 uint32_t buffer[1024];
16506c9d 761 uint32_t *c[6], *NFD_c[6], *NFKD_c[6], *NFC_c[6], *NFKC_c[6]; /* 1-indexed */
e5a5a138
RK
762 int cn, bn;
763
764 fprintf(stderr, "test_unicode\n");
e2452add 765 fp = open_unicode_test("NormalizationTest.txt");
e5a5a138
RK
766 while(!inputline("NormalizationTest.txt", fp, &l, '\n')) {
767 ++lineno;
768 if(*l == '#' || *l == '@')
769 continue;
770 bn = 0;
771 cn = 1;
772 lp = l;
773 c[cn++] = &buffer[bn];
774 while(*lp && *lp != '#') {
775 if(*lp == ' ') {
776 ++lp;
777 continue;
778 }
779 if(*lp == ';') {
780 buffer[bn++] = 0;
781 if(cn == 6)
782 break;
783 c[cn++] = &buffer[bn];
784 ++lp;
785 continue;
786 }
787 buffer[bn++] = strtoul(lp, &lp, 16);
788 }
789 buffer[bn] = 0;
790 assert(cn == 6);
791 for(cn = 1; cn <= 5; ++cn) {
792 NFD_c[cn] = utf32_decompose_canon(c[cn], utf32_len(c[cn]), 0);
793 NFKD_c[cn] = utf32_decompose_compat(c[cn], utf32_len(c[cn]), 0);
16506c9d
RK
794 NFC_c[cn] = utf32_compose_canon(c[cn], utf32_len(c[cn]), 0);
795 NFKC_c[cn] = utf32_compose_compat(c[cn], utf32_len(c[cn]), 0);
e5a5a138
RK
796 }
797#define unt_check(T, A, B) do { \
798 ++tests; \
799 if(utf32_cmp(c[A], T##_c[B])) { \
e2452add
RK
800 fprintf(stderr, \
801 "NormalizationTest.txt:%d: c%d != "#T"(c%d)\n", \
802 lineno, A, B); \
16506c9d 803 fprintf(stderr, " c%d:%s\n", \
e5a5a138 804 A, format_utf32(c[A])); \
16506c9d
RK
805 fprintf(stderr, " c%d:%s\n", \
806 B, format_utf32(c[B])); \
807 fprintf(stderr, "%4s(c%d):%s\n", \
e5a5a138 808 #T, B, format_utf32(T##_c[B])); \
bcf9ed7f 809 count_error(); \
e5a5a138
RK
810 } \
811 } while(0)
812 unt_check(NFD, 3, 1);
813 unt_check(NFD, 3, 2);
814 unt_check(NFD, 3, 3);
815 unt_check(NFD, 5, 4);
816 unt_check(NFD, 5, 5);
817 unt_check(NFKD, 5, 1);
818 unt_check(NFKD, 5, 2);
819 unt_check(NFKD, 5, 3);
820 unt_check(NFKD, 5, 4);
821 unt_check(NFKD, 5, 5);
16506c9d
RK
822 unt_check(NFC, 2, 1);
823 unt_check(NFC, 2, 2);
824 unt_check(NFC, 2, 3);
825 unt_check(NFC, 4, 4);
826 unt_check(NFC, 4, 5);
827 unt_check(NFKC, 4, 1);
828 unt_check(NFKC, 4, 2);
829 unt_check(NFKC, 4, 3);
830 unt_check(NFKC, 4, 4);
831 unt_check(NFKC, 4, 5);
e5a5a138
RK
832 for(cn = 1; cn <= 5; ++cn) {
833 xfree(NFD_c[cn]);
834 xfree(NFKD_c[cn]);
835 }
836 xfree(l);
837 }
e2452add 838 fclose(fp);
1625e11a 839 breaktest("auxiliary/GraphemeBreakTest.txt", utf32_is_grapheme_boundary);
bb48024f 840 breaktest("auxiliary/WordBreakTest.txt", utf32_is_word_boundary);
c35e83d9
RK
841 insist(utf32_combining_class(0x40000) == 0);
842 insist(utf32_combining_class(0xE0000) == 0);
e5a5a138
RK
843}
844
ea387d53
RK
845static void test_signame(void) {
846 fprintf(stderr, "test_signame\n");
847 insist(find_signal("SIGTERM") == SIGTERM);
848 insist(find_signal("SIGHUP") == SIGHUP);
849 insist(find_signal("SIGINT") == SIGINT);
850 insist(find_signal("SIGQUIT") == SIGQUIT);
851 insist(find_signal("SIGKILL") == SIGKILL);
852 insist(find_signal("SIGYOURMUM") == -1);
853}
854
9f28e855
RK
855static void test_cache(void) {
856 const struct cache_type t1 = { 1 }, t2 = { 10 };
857 const char v11[] = "spong", v12[] = "wibble", v2[] = "blat";
858 fprintf(stderr, "test_cache\n");
859 cache_put(&t1, "1_1", v11);
860 cache_put(&t1, "1_2", v12);
861 cache_put(&t2, "2", v2);
862 insist(cache_count() == 3);
863 insist(cache_get(&t2, "2") == v2);
864 insist(cache_get(&t1, "1_1") == v11);
865 insist(cache_get(&t1, "1_2") == v12);
866 insist(cache_get(&t1, "2") == 0);
867 insist(cache_get(&t2, "1_1") == 0);
868 insist(cache_get(&t2, "1_2") == 0);
869 insist(cache_get(&t1, "2") == 0);
870 insist(cache_get(&t2, "1_1") == 0);
871 insist(cache_get(&t2, "1_2") == 0);
872 sleep(2);
873 cache_expire();
874 insist(cache_count() == 1);
875 insist(cache_get(&t1, "1_1") == 0);
876 insist(cache_get(&t1, "1_2") == 0);
877 insist(cache_get(&t2, "2") == v2);
878 cache_clean(0);
879 insist(cache_count() == 0);
880 insist(cache_get(&t2, "2") == 0);
881}
882
00e36cd0
RK
883static void test_filepart(void) {
884 fprintf(stderr, "test_filepart\n");
885 check_string(d_dirname("/"), "/");
886 check_string(d_dirname("/spong"), "/");
887 check_string(d_dirname("/foo/bar"), "/foo");
888 check_string(d_dirname("./bar"), ".");
889 check_string(d_dirname("."), ".");
890 check_string(d_dirname(".."), ".");
891 check_string(d_dirname("../blat"), "..");
892 check_string(d_dirname("wibble"), ".");
893 check_string(extension("foo.c"), ".c");
894 check_string(extension(".c"), ".c");
895 check_string(extension("."), ".");
896 check_string(extension("foo"), "");
897 check_string(extension("./foo"), "");
898 check_string(extension("./foo.c"), ".c");
899}
900
65bb0fff
RK
901static void test_selection(void) {
902 hash *h;
903 fprintf(stderr, "test_selection\n");
904 insist((h = selection_new()) != 0);
905 selection_set(h, "one", 1);
906 selection_set(h, "two", 1);
907 selection_set(h, "three", 0);
908 selection_set(h, "four", 1);
909 insist(selection_selected(h, "one") == 1);
910 insist(selection_selected(h, "two") == 1);
911 insist(selection_selected(h, "three") == 0);
912 insist(selection_selected(h, "four") == 1);
913 insist(selection_selected(h, "five") == 0);
914 insist(hash_count(h) == 3);
915 selection_flip(h, "one");
916 selection_flip(h, "three");
917 insist(selection_selected(h, "one") == 0);
918 insist(selection_selected(h, "three") == 1);
919 insist(hash_count(h) == 3);
920 selection_live(h, "one");
921 selection_live(h, "two");
922 selection_live(h, "three");
923 selection_cleanup(h);
924 insist(selection_selected(h, "one") == 0);
925 insist(selection_selected(h, "two") == 1);
926 insist(selection_selected(h, "three") == 1);
927 insist(selection_selected(h, "four") == 0);
928 insist(selection_selected(h, "five") == 0);
929 insist(hash_count(h) == 2);
930 selection_empty(h);
931 insist(selection_selected(h, "one") == 0);
932 insist(selection_selected(h, "two") == 0);
933 insist(selection_selected(h, "three") == 0);
934 insist(selection_selected(h, "four") == 0);
935 insist(selection_selected(h, "five") == 0);
936 insist(hash_count(h) == 0);
937}
938
71b90230
RK
939static void test_wstat(void) {
940 pid_t pid;
941 int w;
942
943 fprintf(stderr, "test_wstat\n");
944 if(!(pid = xfork())) {
945 _exit(1);
946 }
947 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
948 ;
949 check_string(wstat(w), "exited with status 1");
950 if(!(pid = xfork())) {
951 kill(getpid(), SIGTERM);
952 _exit(-1);
953 }
954 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
955 ;
956 check_string_prefix(wstat(w), "terminated by signal 15");
957}
958
22f61603
RK
959static void test_kvp(void) {
960 struct kvp *k;
632637c7 961 size_t n;
22f61603
RK
962
963 fprintf(stderr, "test_kvp\n");
632637c7 964 /* decoding */
22f61603
RK
965#define KVP_URLDECODE(S) kvp_urldecode((S), strlen(S))
966 insist(KVP_URLDECODE("=%zz") == 0);
967 insist(KVP_URLDECODE("=%0") == 0);
968 insist(KVP_URLDECODE("=%0z") == 0);
969 insist(KVP_URLDECODE("=%%") == 0);
970 insist(KVP_URLDECODE("==%") == 0);
971 insist(KVP_URLDECODE("wibble") == 0);
972 insist(KVP_URLDECODE("") == 0);
973 insist(KVP_URLDECODE("wibble&") == 0);
974 insist((k = KVP_URLDECODE("one=bl%61t+foo")) != 0);
975 check_string(kvp_get(k, "one"), "blat foo");
976 insist(kvp_get(k, "ONE") == 0);
977 insist(k->next == 0);
978 insist((k = KVP_URLDECODE("wibble=splat&bar=spong")) != 0);
979 check_string(kvp_get(k, "wibble"), "splat");
980 check_string(kvp_get(k, "bar"), "spong");
981 insist(kvp_get(k, "ONE") == 0);
982 insist(k->next->next == 0);
632637c7
RK
983 /* encoding */
984 insist(kvp_set(&k, "bar", "spong") == 0);
985 insist(kvp_set(&k, "bar", "foo") == 1);
986 insist(kvp_set(&k, "zog", "%") == 1);
987 insist(kvp_set(&k, "wibble", 0) == 1);
988 insist(kvp_set(&k, "wibble", 0) == 0);
989 check_string(kvp_urlencode(k, 0),
990 "bar=foo&zog=%25");
991 check_string(kvp_urlencode(k, &n),
992 "bar=foo&zog=%25");
993 insist(n == strlen("bar=foo&zog=%25"));
994 check_string(urlencodestring("abc% +\n"),
995 "abc%25%20%2b%0a");
22f61603
RK
996}
997
6049fe0e
RK
998static void test_sink(void) {
999 struct sink *s;
1000 struct dynstr d[1];
1001 FILE *fp;
1002 char *l;
1003
1004 fprintf(stderr, "test_sink\n");
1005
1006 fp = tmpfile();
1007 assert(fp != 0);
1008 s = sink_stdio("tmpfile", fp);
1009 insist(sink_printf(s, "test: %d\n", 999) == 10);
1010 insist(sink_printf(s, "wibble: %s\n", "foobar") == 15);
1011 rewind(fp);
1012 insist(inputline("tmpfile", fp, &l, '\n') == 0);
1013 check_string(l, "test: 999");
1014 insist(inputline("tmpfile", fp, &l, '\n') == 0);
1015 check_string(l, "wibble: foobar");
1016 insist(inputline("tmpfile", fp, &l, '\n') == -1);
1017
1018 dynstr_init(d);
1019 s = sink_dynstr(d);
1020 insist(sink_printf(s, "test: %d\n", 999) == 10);
1021 insist(sink_printf(s, "wibble: %s\n", "foobar") == 15);
1022 dynstr_terminate(d);
1023 check_string(d->vec, "test: 999\nwibble: foobar\n");
1024}
1025
0c740e59
RK
1026static const char *do_printf(const char *fmt, ...) {
1027 va_list ap;
1028 char *s;
1029 int rc;
1030
1031 va_start(ap, fmt);
1032 rc = byte_vasprintf(&s, fmt, ap);
1033 va_end(ap);
1034 if(rc < 0)
1035 return 0;
1036 return s;
1037}
1038
1039static void test_printf(void) {
1040 char c;
1041 short s;
1042 int i;
1043 long l;
1044 long long ll;
1045 intmax_t m;
1046 ssize_t ssz;
1047 ptrdiff_t p;
1048
1049 fprintf(stderr, "test_printf\n");
1050 check_string(do_printf("%d", 999), "999");
1051 check_string(do_printf("%d", -999), "-999");
1052 check_string(do_printf("%i", 999), "999");
1053 check_string(do_printf("%i", -999), "-999");
1054 check_string(do_printf("%u", 999), "999");
1055 check_string(do_printf("%2u", 999), "999");
1056 check_string(do_printf("%10u", 999), " 999");
1057 check_string(do_printf("%-10u", 999), "999 ");
1058 check_string(do_printf("%010u", 999), "0000000999");
1059 check_string(do_printf("%-10d", -999), "-999 ");
1060 check_string(do_printf("%-010d", -999), "-999 "); /* "-" beats "0" */
1061 check_string(do_printf("%66u", 999), " 999");
1062 check_string(do_printf("%o", 999), "1747");
1063 check_string(do_printf("%#o", 999), "01747");
1064 check_string(do_printf("%#o", 0), "0");
1065 check_string(do_printf("%x", 999), "3e7");
1066 check_string(do_printf("%#x", 999), "0x3e7");
1067 check_string(do_printf("%#X", 999), "0X3E7");
1068 check_string(do_printf("%#x", 0), "0");
1069 check_string(do_printf("%hd", (short)999), "999");
1070 check_string(do_printf("%hhd", (short)99), "99");
1071 check_string(do_printf("%ld", 100000L), "100000");
1072 check_string(do_printf("%lld", 10000000000LL), "10000000000");
1073 check_string(do_printf("%qd", 10000000000LL), "10000000000");
1074 check_string(do_printf("%jd", (intmax_t)10000000000LL), "10000000000");
1075 check_string(do_printf("%zd", (ssize_t)2000000000), "2000000000");
1076 check_string(do_printf("%td", (ptrdiff_t)2000000000), "2000000000");
1077 check_string(do_printf("%hu", (short)999), "999");
1078 check_string(do_printf("%hhu", (short)99), "99");
1079 check_string(do_printf("%lu", 100000L), "100000");
1080 check_string(do_printf("%llu", 10000000000LL), "10000000000");
1081 check_string(do_printf("%ju", (uintmax_t)10000000000LL), "10000000000");
1082 check_string(do_printf("%zu", (size_t)2000000000), "2000000000");
1083 check_string(do_printf("%tu", (ptrdiff_t)2000000000), "2000000000");
1084 check_string(do_printf("%p", (void *)0x100), "0x100");
1085 check_string(do_printf("%s", "wibble"), "wibble");
1086 check_string(do_printf("%s-%s", "wibble", "wobble"), "wibble-wobble");
1087 check_string(do_printf("%10s", "wibble"), " wibble");
1088 check_string(do_printf("%010s", "wibble"), " wibble"); /* 0 ignored for %s */
1089 check_string(do_printf("%-10s", "wibble"), "wibble ");
1090 check_string(do_printf("%2s", "wibble"), "wibble");
1091 check_string(do_printf("%.2s", "wibble"), "wi");
1092 check_string(do_printf("%.2s", "w"), "w");
1093 check_string(do_printf("%4.2s", "wibble"), " wi");
1094 check_string(do_printf("%c", 'a'), "a");
1095 check_string(do_printf("%4c", 'a'), " a");
1096 check_string(do_printf("%-4c", 'a'), "a ");
1097 check_string(do_printf("%*c", 0, 'a'), "a");
1098 check_string(do_printf("x%hhny", &c), "xy");
1099 insist(c == 1);
1100 check_string(do_printf("xx%hnyy", &s), "xxyy");
1101 insist(s == 2);
1102 check_string(do_printf("xxx%nyyy", &i), "xxxyyy");
1103 insist(i == 3);
1104 check_string(do_printf("xxxx%lnyyyy", &l), "xxxxyyyy");
1105 insist(l == 4);
1106 check_string(do_printf("xxxxx%llnyyyyy", &ll), "xxxxxyyyyy");
1107 insist(ll == 5);
1108 check_string(do_printf("xxxxxx%jnyyyyyy", &m), "xxxxxxyyyyyy");
1109 insist(m == 6);
1110 check_string(do_printf("xxxxxxx%znyyyyyyy", &ssz), "xxxxxxxyyyyyyy");
1111 insist(ssz == 7);
1112 check_string(do_printf("xxxxxxxx%tnyyyyyyyy", &p), "xxxxxxxxyyyyyyyy");
1113 insist(p == 8);
1114 check_string(do_printf("%*d", 5, 99), " 99");
1115 check_string(do_printf("%*d", -5, 99), "99 ");
1116 check_string(do_printf("%.*d", 5, 99), "00099");
1117 check_string(do_printf("%.*d", -5, 99), "99");
1118 check_string(do_printf("%.0d", 0), "");
1119 check_string(do_printf("%.d", 0), "");
1120 check_string(do_printf("%.d", 0), "");
1121 check_string(do_printf("%%"), "%");
1122 check_string(do_printf("wibble"), "wibble");
1123 insist(do_printf("%") == 0);
1124 insist(do_printf("%=") == 0);
1125}
1126
abe28bfe
RK
1127static void test_basen(void) {
1128 unsigned long v[64];
1129 char buffer[1024];
1130
1131 fprintf(stderr, "test_basen\n");
1132 v[0] = 999;
1133 insist(basen(v, 1, buffer, sizeof buffer, 10) == 0);
1134 check_string(buffer, "999");
1135
1136 v[0] = 1+2*7+3*7*7+4*7*7*7;
1137 insist(basen(v, 1, buffer, sizeof buffer, 7) == 0);
1138 check_string(buffer, "4321");
1139
1140 v[0] = 0x00010203;
1141 v[1] = 0x04050607;
1142 v[2] = 0x08090A0B;
1143 v[3] = 0x0C0D0E0F;
1144 insist(basen(v, 4, buffer, sizeof buffer, 256) == 0);
1145 check_string(buffer, "123456789abcdef");
1146
1147 v[0] = 0x00010203;
1148 v[1] = 0x04050607;
1149 v[2] = 0x08090A0B;
1150 v[3] = 0x0C0D0E0F;
1151 insist(basen(v, 4, buffer, sizeof buffer, 16) == 0);
1152 check_string(buffer, "102030405060708090a0b0c0d0e0f");
1153
1154 v[0] = 0x00010203;
1155 v[1] = 0x04050607;
1156 v[2] = 0x08090A0B;
1157 v[3] = 0x0C0D0E0F;
1158 insist(basen(v, 4, buffer, 10, 16) == -1);
1159}
1160
5e49fa7f
RK
1161static void test_split(void) {
1162 char **v;
1163 int nv;
1164
1165 fprintf(stderr, "test_split\n");
1166 insist(split("\"misquoted", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1167 insist(split("\'misquoted", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1168 insist(split("\'misquoted\\", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1169 insist(split("\'misquoted\\\"", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1170 insist(split("\'mis\\escaped\'", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0) == 0);
1171
1172 insist((v = split("", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1173 check_integer(nv, 0);
1174 insist(*v == 0);
1175
1176 insist((v = split("wibble", &nv, SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1177 check_integer(nv, 1);
1178 check_string(v[0], "wibble");
1179 insist(v[1] == 0);
1180
1181 insist((v = split(" wibble \t\r\n wobble ", &nv,
1182 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1183 check_integer(nv, 2);
1184 check_string(v[0], "wibble");
1185 check_string(v[1], "wobble");
1186 insist(v[2] == 0);
1187
1188 insist((v = split("wibble wobble #splat", &nv,
1189 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1190 check_integer(nv, 2);
1191 check_string(v[0], "wibble");
1192 check_string(v[1], "wobble");
1193 insist(v[2] == 0);
1194
1195 insist((v = split("\"wibble wobble\" #splat", &nv,
1196 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1197 check_integer(nv, 1);
1198 check_string(v[0], "wibble wobble");
1199 insist(v[1] == 0);
1200
1201 insist((v = split("\"wibble \\\"\\nwobble\"", &nv,
1202 SPLIT_COMMENTS|SPLIT_QUOTES, 0, 0)));
1203 check_integer(nv, 1);
1204 check_string(v[0], "wibble \"\nwobble");
1205 insist(v[1] == 0);
1206
1207 insist((v = split("\"wibble wobble\" #splat", &nv,
1208 SPLIT_QUOTES, 0, 0)));
1209 check_integer(nv, 2);
1210 check_string(v[0], "wibble wobble");
1211 check_string(v[1], "#splat");
1212 insist(v[2] == 0);
1213
1214 insist((v = split("\"wibble wobble\" #splat", &nv,
1215 SPLIT_COMMENTS, 0, 0)));
1216 check_integer(nv, 2);
1217 check_string(v[0], "\"wibble");
1218 check_string(v[1], "wobble\"");
1219 insist(v[2] == 0);
1220
1221 check_string(quoteutf8("wibble"), "wibble");
1222 check_string(quoteutf8(" wibble "), "\" wibble \"");
1223 check_string(quoteutf8("wibble wobble"), "\"wibble wobble\"");
1224 check_string(quoteutf8("wibble\"wobble"), "\"wibble\\\"wobble\"");
1225 check_string(quoteutf8("wibble\nwobble"), "\"wibble\\nwobble\"");
1226 check_string(quoteutf8("wibble\\wobble"), "\"wibble\\\\wobble\"");
1227 check_string(quoteutf8("wibble'wobble"), "\"wibble'wobble\"");
1228}
1229
fe575537
RK
1230static void test_hash(void) {
1231 hash *h;
1232 int i, *ip;
1233 char **keys;
1234
1235 fprintf(stderr, "test_hash\n");
1236 h = hash_new(sizeof(int));
1237 for(i = 0; i < 10000; ++i)
1238 insist(hash_add(h, do_printf("%d", i), &i, HASH_INSERT) == 0);
1239 check_integer(hash_count(h), 10000);
1240 for(i = 0; i < 10000; ++i) {
1241 insist((ip = hash_find(h, do_printf("%d", i))) != 0);
1242 check_integer(*ip, i);
1243 insist(hash_add(h, do_printf("%d", i), &i, HASH_REPLACE) == 0);
1244 }
1245 check_integer(hash_count(h), 10000);
1246 keys = hash_keys(h);
1247 for(i = 0; i < 10000; ++i)
1248 insist(keys[i] != 0);
1249 insist(keys[10000] == 0);
1250 for(i = 0; i < 10000; ++i)
1251 insist(hash_remove(h, do_printf("%d", i)) == 0);
1252 check_integer(hash_count(h), 0);
1253}
1254
460b9539 1255int main(void) {
fe575537 1256 mem_init();
bb48024f 1257 fail_first = !!getenv("FAIL_FIRST");
460b9539 1258 insist('\n' == 0x0A);
1259 insist('\r' == 0x0D);
1260 insist(' ' == 0x20);
1261 insist('0' == 0x30);
1262 insist('9' == 0x39);
1263 insist('A' == 0x41);
1264 insist('Z' == 0x5A);
1265 insist('a' == 0x61);
1266 insist('z' == 0x7A);
1267 /* addr.c */
1268 /* asprintf.c */
1269 /* authhash.c */
1270 /* basen.c */
abe28bfe 1271 test_basen();
460b9539 1272 /* charset.c */
1273 /* client.c */
1274 /* configuration.c */
1275 /* event.c */
00e36cd0
RK
1276 /* filepart.c */
1277 test_filepart();
460b9539 1278 /* fprintf.c */
033fd4e3
RK
1279 /* heap.c */
1280 test_heap();
460b9539 1281 /* hex.c */
1282 test_hex();
1283 /* inputline.c */
1284 /* kvp.c */
22f61603 1285 test_kvp();
460b9539 1286 /* log.c */
1287 /* mem.c */
1288 /* mime.c */
1289 test_mime();
1290 /* mixer.c */
1291 /* plugin.c */
1292 /* printf.c */
0c740e59 1293 test_printf();
460b9539 1294 /* queue.c */
1295 /* sink.c */
6049fe0e 1296 test_sink();
460b9539 1297 /* snprintf.c */
1298 /* split.c */
5e49fa7f 1299 test_split();
460b9539 1300 /* syscalls.c */
1301 /* table.c */
e5a5a138
RK
1302 /* unicode.c */
1303 test_unicode();
460b9539 1304 /* utf8.c */
1305 test_utf8();
1306 /* vector.c */
1307 /* words.c */
1308 test_casefold();
8818b7fc 1309 test_words();
460b9539 1310 /* wstat.c */
71b90230 1311 test_wstat();
ea387d53
RK
1312 /* signame.c */
1313 test_signame();
9f28e855
RK
1314 /* cache.c */
1315 test_cache();
65bb0fff
RK
1316 /* selection.c */
1317 test_selection();
fe575537 1318 test_hash();
460b9539 1319 fprintf(stderr, "%d errors out of %d tests\n", errors, tests);
1320 return !!errors;
1321}
1322
1323/*
1324Local Variables:
1325c-basic-offset:2
1326comment-column:40
56fd389c
RK
1327fill-column:79
1328indent-tabs-mode:nil
460b9539 1329End:
1330*/