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