Commit | Line | Data |
---|---|---|
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> |
460b9539 | 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" | |
033fd4e3 | 42 | #include "heap.h" |
e5a5a138 RK |
43 | #include "unicode.h" |
44 | #include "inputline.h" | |
e2452add | 45 | #include "wstat.h" |
ea387d53 | 46 | #include "signame.h" |
9f28e855 | 47 | #include "cache.h" |
460b9539 | 48 | |
49 | static int tests, errors; | |
bb48024f RK |
50 | static int fail_first; |
51 | ||
52 | static void count_error() { | |
53 | ++errors; | |
54 | if(fail_first) | |
55 | abort(); | |
56 | } | |
460b9539 | 57 | |
033fd4e3 | 58 | /** @brief Checks that @p expr is nonzero */ |
460b9539 | 59 | #define insist(expr) do { \ |
033fd4e3 | 60 | if(!(expr)) { \ |
bb48024f | 61 | count_error(); \ |
460b9539 | 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 | ||
e5a5a138 RK |
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++)) { | |
16506c9d RK |
93 | sprintf(buf, " %04lX", (long)c); |
94 | dynstr_append_string(&d, buf); | |
e5a5a138 RK |
95 | } |
96 | dynstr_terminate(&d); | |
97 | return d.vec; | |
98 | } | |
99 | ||
460b9539 | 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); \ | |
bb48024f | 107 | count_error(); \ |
460b9539 | 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)); \ | |
bb48024f | 111 | count_error(); \ |
460b9539 | 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)); \ | |
caecd4f4 | 140 | ucs = utf8_to_utf32(CHARS, strlen(CHARS), 0); \ |
460b9539 | 141 | insist(ucs != 0); \ |
caecd4f4 RK |
142 | insist(!utf32_cmp(w, ucs)); \ |
143 | u8 = utf32_to_utf8(ucs, utf32_len(ucs), 0); \ | |
460b9539 | 144 | insist(u8 != 0); \ |
145 | insist(!strcmp(u8, CHARS)); \ | |
146 | } while(0) | |
147 | ||
033fd4e3 | 148 | fprintf(stderr, "test_utf8\n"); |
18cda350 | 149 | #define validutf8(S) utf8_valid((S), strlen(S)) |
033fd4e3 | 150 | |
460b9539 | 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")); | |
c35e83d9 | 220 | insist(!validutf8("\xF4\x80\xFF\x80")); |
460b9539 | 221 | |
222 | /* miscellaneous non-UTF-8 rubbish */ | |
223 | insist(!validutf8("\x80")); | |
224 | insist(!validutf8("\xBF")); | |
225 | insist(!validutf8("\xC0")); | |
226 | insist(!validutf8("\xC0\x7F")); | |
227 | insist(!validutf8("\xC0\xC0")); | |
228 | insist(!validutf8("\xE0")); | |
229 | insist(!validutf8("\xE0\x7F")); | |
230 | insist(!validutf8("\xE0\xC0")); | |
231 | insist(!validutf8("\xE0\x80")); | |
232 | insist(!validutf8("\xE0\x80\x7f")); | |
233 | insist(!validutf8("\xE0\x80\xC0")); | |
234 | insist(!validutf8("\xF0")); | |
235 | insist(!validutf8("\xF0\x7F")); | |
236 | insist(!validutf8("\xF0\xC0")); | |
237 | insist(!validutf8("\xF0\x80")); | |
238 | insist(!validutf8("\xF0\x80\x7f")); | |
239 | insist(!validutf8("\xF0\x80\xC0")); | |
240 | insist(!validutf8("\xF0\x80\x80\x7f")); | |
241 | insist(!validutf8("\xF0\x80\x80\xC0")); | |
242 | insist(!validutf8("\xF5\x80\x80\x80")); | |
243 | insist(!validutf8("\xF8")); | |
244 | } | |
245 | ||
246 | static void test_mime(void) { | |
247 | char *t, *n, *v; | |
248 | ||
033fd4e3 RK |
249 | fprintf(stderr, "test_mime\n"); |
250 | ||
460b9539 | 251 | t = n = v = 0; |
252 | insist(!mime_content_type("text/plain", &t, &n, &v)); | |
253 | insist(!strcmp(t, "text/plain")); | |
254 | insist(n == 0); | |
255 | insist(v == 0); | |
256 | ||
257 | t = n = v = 0; | |
258 | insist(!mime_content_type("TEXT ((nested) comment) /plain", &t, &n, &v)); | |
259 | insist(!strcmp(t, "text/plain")); | |
260 | insist(n == 0); | |
261 | insist(v == 0); | |
262 | ||
263 | t = n = v = 0; | |
264 | insist(!mime_content_type(" text/plain ; Charset=utf-8", &t, &n, &v)); | |
265 | insist(!strcmp(t, "text/plain")); | |
266 | insist(!strcmp(n, "charset")); | |
267 | insist(!strcmp(v, "utf-8")); | |
268 | ||
269 | t = n = v = 0; | |
270 | insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t, &n, &v)); | |
271 | insist(!strcmp(t, "text/plain")); | |
272 | insist(!strcmp(n, "charset")); | |
273 | insist(!strcmp(v, "ISO-8859-1")); | |
274 | ||
275 | /* XXX mime_parse */ | |
276 | /* XXX mime_multipart */ | |
277 | /* XXX mime_rfc2388_content_disposition */ | |
278 | ||
279 | check_string(mime_qp(""), ""); | |
280 | check_string(mime_qp("foobar"), "foobar"); | |
281 | check_string(mime_qp("foo=20bar"), "foo bar"); | |
282 | check_string(mime_qp("x \r\ny"), "x\r\ny"); | |
283 | check_string(mime_qp("x=\r\ny"), "xy"); | |
284 | check_string(mime_qp("x= \r\ny"), "xy"); | |
285 | check_string(mime_qp("x =\r\ny"), "x y"); | |
286 | check_string(mime_qp("x = \r\ny"), "x y"); | |
287 | ||
288 | /* from RFC2045 */ | |
289 | check_string(mime_qp("Now's the time =\r\n" | |
290 | "for all folk to come=\r\n" | |
291 | " to the aid of their country."), | |
292 | "Now's the time for all folk to come to the aid of their country."); | |
293 | ||
294 | check_string(mime_base64(""), ""); | |
295 | check_string(mime_base64("BBBB"), "\x04\x10\x41"); | |
296 | check_string(mime_base64("////"), "\xFF\xFF\xFF"); | |
297 | check_string(mime_base64("//BB"), "\xFF\xF0\x41"); | |
298 | check_string(mime_base64("BBBB//BB////"), | |
299 | "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF"); | |
300 | check_string(mime_base64("B B B B / / B B / / / /"), | |
301 | "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF"); | |
302 | check_string(mime_base64("B\r\nBBB.// B-B//~//"), | |
303 | "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF"); | |
304 | check_string(mime_base64("BBBB="), | |
305 | "\x04\x10\x41"); | |
306 | check_string(mime_base64("BBBBx="), /* not actually valid base64 */ | |
307 | "\x04\x10\x41"); | |
308 | check_string(mime_base64("BBBB BB=="), | |
309 | "\x04\x10\x41" "\x04"); | |
310 | check_string(mime_base64("BBBB BBB="), | |
311 | "\x04\x10\x41" "\x04\x10"); | |
312 | } | |
313 | ||
314 | static void test_hex(void) { | |
315 | unsigned n; | |
316 | static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F }; | |
317 | uint8_t *u; | |
318 | size_t ul; | |
319 | ||
033fd4e3 RK |
320 | fprintf(stderr, "test_hex\n"); |
321 | ||
460b9539 | 322 | for(n = 0; n <= UCHAR_MAX; ++n) { |
323 | if(!isxdigit(n)) | |
324 | insist(unhexdigitq(n) == -1); | |
325 | } | |
326 | insist(unhexdigitq('0') == 0); | |
327 | insist(unhexdigitq('1') == 1); | |
328 | insist(unhexdigitq('2') == 2); | |
329 | insist(unhexdigitq('3') == 3); | |
330 | insist(unhexdigitq('4') == 4); | |
331 | insist(unhexdigitq('5') == 5); | |
332 | insist(unhexdigitq('6') == 6); | |
333 | insist(unhexdigitq('7') == 7); | |
334 | insist(unhexdigitq('8') == 8); | |
335 | insist(unhexdigitq('9') == 9); | |
336 | insist(unhexdigitq('a') == 10); | |
337 | insist(unhexdigitq('b') == 11); | |
338 | insist(unhexdigitq('c') == 12); | |
339 | insist(unhexdigitq('d') == 13); | |
340 | insist(unhexdigitq('e') == 14); | |
341 | insist(unhexdigitq('f') == 15); | |
342 | insist(unhexdigitq('A') == 10); | |
343 | insist(unhexdigitq('B') == 11); | |
344 | insist(unhexdigitq('C') == 12); | |
345 | insist(unhexdigitq('D') == 13); | |
346 | insist(unhexdigitq('E') == 14); | |
347 | insist(unhexdigitq('F') == 15); | |
348 | check_string(hex(h, sizeof h), "00ff807f"); | |
349 | check_string(hex(0, 0), ""); | |
350 | u = unhex("00ff807f", &ul); | |
351 | insist(ul == 4); | |
352 | insist(memcmp(u, h, 4) == 0); | |
353 | u = unhex("00FF807F", &ul); | |
354 | insist(ul == 4); | |
355 | insist(memcmp(u, h, 4) == 0); | |
356 | u = unhex("", &ul); | |
357 | insist(ul == 0); | |
033fd4e3 | 358 | fprintf(stderr, "2 ERROR reports expected {\n"); |
460b9539 | 359 | insist(unhex("F", 0) == 0); |
360 | insist(unhex("az", 0) == 0); | |
033fd4e3 | 361 | fprintf(stderr, "}\n"); |
460b9539 | 362 | } |
363 | ||
364 | static void test_casefold(void) { | |
e5a5a138 | 365 | uint32_t c, l; |
56fd389c | 366 | const char *input, *canon_folded, *compat_folded, *canon_expected, *compat_expected; |
460b9539 | 367 | |
033fd4e3 | 368 | fprintf(stderr, "test_casefold\n"); |
56fd389c RK |
369 | |
370 | /* This isn't a very exhaustive test. Unlike for normalization, there don't | |
371 | * seem to be any public test vectors for these algorithms. */ | |
e5a5a138 | 372 | |
460b9539 | 373 | for(c = 1; c < 256; ++c) { |
e5a5a138 | 374 | input = utf32_to_utf8(&c, 1, 0); |
56fd389c RK |
375 | canon_folded = utf8_casefold_canon(input, strlen(input), 0); |
376 | compat_folded = utf8_casefold_compat(input, strlen(input), 0); | |
460b9539 | 377 | switch(c) { |
378 | default: | |
379 | if((c >= 'A' && c <= 'Z') | |
380 | || (c >= 0xC0 && c <= 0xDE && c != 0xD7)) | |
381 | l = c ^ 0x20; | |
382 | else | |
383 | l = c; | |
384 | break; | |
385 | case 0xB5: /* MICRO SIGN */ | |
e5a5a138 | 386 | l = 0x3BC; /* GREEK SMALL LETTER MU */ |
460b9539 | 387 | break; |
388 | case 0xDF: /* LATIN SMALL LETTER SHARP S */ | |
56fd389c RK |
389 | insist(!strcmp(canon_folded, "ss")); |
390 | insist(!strcmp(compat_folded, "ss")); | |
460b9539 | 391 | l = 0; |
392 | break; | |
393 | } | |
394 | if(l) { | |
caecd4f4 | 395 | uint32_t *d; |
e5a5a138 | 396 | /* Case-folded data is now normalized */ |
caecd4f4 RK |
397 | d = utf32_decompose_canon(&l, 1, 0); |
398 | canon_expected = utf32_to_utf8(d, utf32_len(d), 0); | |
56fd389c RK |
399 | if(strcmp(canon_folded, canon_expected)) { |
400 | fprintf(stderr, "%s:%d: canon-casefolding %#lx got '%s', expected '%s'\n", | |
401 | __FILE__, __LINE__, (unsigned long)c, | |
402 | format(canon_folded), format(canon_expected)); | |
bb48024f | 403 | count_error(); |
56fd389c RK |
404 | } |
405 | ++tests; | |
caecd4f4 RK |
406 | d = utf32_decompose_compat(&l, 1, 0); |
407 | compat_expected = utf32_to_utf8(d, utf32_len(d), 0); | |
56fd389c RK |
408 | if(strcmp(compat_folded, compat_expected)) { |
409 | fprintf(stderr, "%s:%d: compat-casefolding %#lx got '%s', expected '%s'\n", | |
460b9539 | 410 | __FILE__, __LINE__, (unsigned long)c, |
56fd389c | 411 | format(compat_folded), format(compat_expected)); |
bb48024f | 412 | count_error(); |
460b9539 | 413 | } |
414 | ++tests; | |
415 | } | |
416 | } | |
c85b7022 | 417 | check_string(utf8_casefold_canon("", 0, 0), ""); |
460b9539 | 418 | } |
419 | ||
8818b7fc RK |
420 | struct { |
421 | const char *in; | |
422 | const char *expect[10]; | |
423 | } wtest[] = { | |
424 | /* Empty string */ | |
425 | { "", { 0 } }, | |
426 | /* Only whitespace and punctuation */ | |
427 | { " ", { 0 } }, | |
428 | { " ' ", { 0 } }, | |
429 | { " ! ", { 0 } }, | |
430 | { " \"\" ", { 0 } }, | |
431 | { " @ ", { 0 } }, | |
432 | /* Basics */ | |
433 | { "wibble", { "wibble", 0 } }, | |
434 | { " wibble", { "wibble", 0 } }, | |
435 | { " wibble ", { "wibble", 0 } }, | |
436 | { "wibble ", { "wibble", 0 } }, | |
437 | { "wibble spong", { "wibble", "spong", 0 } }, | |
438 | { " wibble spong", { "wibble", "spong", 0 } }, | |
439 | { " wibble spong ", { "wibble", "spong", 0 } }, | |
440 | { "wibble spong ", { "wibble", "spong", 0 } }, | |
441 | { "wibble spong splat foo zot ", { "wibble", "spong", "splat", "foo", "zot", 0 } }, | |
442 | /* Apostrophes */ | |
443 | { "wibble 'spong", { "wibble", "spong", 0 } }, | |
444 | { " wibble's", { "wibble's", 0 } }, | |
445 | { " wibblespong' ", { "wibblespong", 0 } }, | |
446 | { "wibble sp''ong ", { "wibble", "sp", "ong", 0 } }, | |
447 | }; | |
448 | #define NWTEST (sizeof wtest / sizeof *wtest) | |
449 | ||
450 | static void test_words(void) { | |
451 | size_t t, nexpect, ngot, i; | |
452 | int right; | |
453 | ||
454 | fprintf(stderr, "test_words\n"); | |
455 | for(t = 0; t < NWTEST; ++t) { | |
c85b7022 | 456 | char **got = utf8_word_split(wtest[t].in, strlen(wtest[t].in), &ngot, 0); |
8818b7fc RK |
457 | |
458 | for(nexpect = 0; wtest[t].expect[nexpect]; ++nexpect) | |
459 | ; | |
460 | if(nexpect == ngot) { | |
461 | for(i = 0; i < ngot; ++i) | |
462 | if(strcmp(wtest[t].expect[i], got[i])) | |
463 | break; | |
464 | right = i == ngot; | |
465 | } else | |
466 | right = 0; | |
467 | if(!right) { | |
468 | fprintf(stderr, "word split %zu failed\n", t); | |
469 | fprintf(stderr, "input: %s\n", wtest[t].in); | |
470 | fprintf(stderr, " | %-30s | %-30s\n", | |
471 | "expected", "got"); | |
472 | for(i = 0; i < nexpect || i < ngot; ++i) { | |
473 | const char *e = i < nexpect ? wtest[t].expect[i] : "<none>"; | |
474 | const char *g = i < ngot ? got[i] : "<none>"; | |
475 | fprintf(stderr, " %2zu | %-30s | %-30s\n", i, e, g); | |
476 | } | |
477 | count_error(); | |
478 | } | |
479 | ++tests; | |
480 | } | |
481 | } | |
482 | ||
033fd4e3 RK |
483 | /** @brief Less-than comparison function for integer heap */ |
484 | static inline int int_lt(int a, int b) { return a < b; } | |
485 | ||
dab22732 RK |
486 | /** @struct iheap |
487 | * @brief A heap with @c int elements */ | |
033fd4e3 | 488 | HEAP_TYPE(iheap, int, int_lt); |
8e3fe3d8 | 489 | HEAP_DEFINE(iheap, int, int_lt); |
033fd4e3 RK |
490 | |
491 | /** @brief Tests for @ref heap.h */ | |
492 | static void test_heap(void) { | |
493 | struct iheap h[1]; | |
494 | int n; | |
495 | int last = -1; | |
496 | ||
497 | fprintf(stderr, "test_heap\n"); | |
498 | ||
499 | iheap_init(h); | |
500 | for(n = 0; n < 1000; ++n) | |
501 | iheap_insert(h, random() % 100); | |
502 | for(n = 0; n < 1000; ++n) { | |
503 | const int latest = iheap_remove(h); | |
504 | if(last > latest) | |
505 | fprintf(stderr, "should have %d <= %d\n", last, latest); | |
506 | insist(last <= latest); | |
507 | last = latest; | |
508 | } | |
509 | putchar('\n'); | |
510 | } | |
511 | ||
e2452add RK |
512 | /** @brief Open a Unicode test file */ |
513 | static FILE *open_unicode_test(const char *path) { | |
514 | const char *base; | |
515 | FILE *fp; | |
516 | char buffer[1024]; | |
517 | int w; | |
518 | ||
519 | if((base = strrchr(path, '/'))) | |
520 | ++base; | |
521 | else | |
522 | base = path; | |
523 | if(!(fp = fopen(base, "r"))) { | |
524 | snprintf(buffer, sizeof buffer, | |
525 | "wget http://www.unicode.org/Public/5.0.0/ucd/%s", path); | |
526 | if((w = system(buffer))) | |
527 | fatal(0, "%s: %s", buffer, wstat(w)); | |
528 | if(chmod(base, 0444) < 0) | |
529 | fatal(errno, "chmod %s", base); | |
530 | if(!(fp = fopen(base, "r"))) | |
531 | fatal(errno, "%s", base); | |
532 | } | |
533 | return fp; | |
534 | } | |
535 | ||
1625e11a | 536 | /** @brief Run breaking tests for utf32_grapheme_boundary() etc */ |
bb48024f RK |
537 | static void breaktest(const char *path, |
538 | int (*breakfn)(const uint32_t *, size_t, size_t)) { | |
539 | FILE *fp = open_unicode_test(path); | |
540 | int lineno = 0; | |
541 | char *l, *lp; | |
542 | size_t bn, n; | |
543 | char break_allowed[1024]; | |
544 | uint32_t buffer[1024]; | |
545 | ||
546 | while(!inputline(path, fp, &l, '\n')) { | |
547 | ++lineno; | |
548 | if(l[0] == '#') continue; | |
549 | bn = 0; | |
550 | lp = l; | |
551 | while(*lp) { | |
552 | if(*lp == ' ' || *lp == '\t') { | |
553 | ++lp; | |
554 | continue; | |
555 | } | |
556 | if(*lp == '#') | |
557 | break; | |
558 | if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0xB7) { | |
559 | /* 00F7 DIVISION SIGN */ | |
560 | break_allowed[bn] = 1; | |
561 | lp += 2; | |
562 | continue; | |
563 | } | |
564 | if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0x97) { | |
565 | /* 00D7 MULTIPLICATION SIGN */ | |
566 | break_allowed[bn] = 0; | |
567 | lp += 2; | |
568 | continue; | |
569 | } | |
570 | if(isxdigit((unsigned char)*lp)) { | |
571 | buffer[bn++] = strtoul(lp, &lp, 16); | |
572 | continue; | |
573 | } | |
574 | fatal(0, "%s:%d: evil line: %s", path, lineno, l); | |
575 | } | |
576 | for(n = 0; n <= bn; ++n) { | |
577 | if(breakfn(buffer, bn, n) != break_allowed[n]) { | |
578 | fprintf(stderr, | |
b21a155c RK |
579 | "%s:%d: offset %zu: mismatch\n" |
580 | "%s\n" | |
581 | "\n", | |
582 | path, lineno, n, l); | |
bb48024f RK |
583 | count_error(); |
584 | } | |
585 | ++tests; | |
586 | } | |
587 | xfree(l); | |
588 | } | |
589 | fclose(fp); | |
590 | } | |
591 | ||
e5a5a138 RK |
592 | /** @brief Tests for @ref lib/unicode.h */ |
593 | static void test_unicode(void) { | |
594 | FILE *fp; | |
595 | int lineno = 0; | |
596 | char *l, *lp; | |
597 | uint32_t buffer[1024]; | |
16506c9d | 598 | uint32_t *c[6], *NFD_c[6], *NFKD_c[6], *NFC_c[6], *NFKC_c[6]; /* 1-indexed */ |
e5a5a138 RK |
599 | int cn, bn; |
600 | ||
601 | fprintf(stderr, "test_unicode\n"); | |
e2452add | 602 | fp = open_unicode_test("NormalizationTest.txt"); |
e5a5a138 RK |
603 | while(!inputline("NormalizationTest.txt", fp, &l, '\n')) { |
604 | ++lineno; | |
605 | if(*l == '#' || *l == '@') | |
606 | continue; | |
607 | bn = 0; | |
608 | cn = 1; | |
609 | lp = l; | |
610 | c[cn++] = &buffer[bn]; | |
611 | while(*lp && *lp != '#') { | |
612 | if(*lp == ' ') { | |
613 | ++lp; | |
614 | continue; | |
615 | } | |
616 | if(*lp == ';') { | |
617 | buffer[bn++] = 0; | |
618 | if(cn == 6) | |
619 | break; | |
620 | c[cn++] = &buffer[bn]; | |
621 | ++lp; | |
622 | continue; | |
623 | } | |
624 | buffer[bn++] = strtoul(lp, &lp, 16); | |
625 | } | |
626 | buffer[bn] = 0; | |
627 | assert(cn == 6); | |
628 | for(cn = 1; cn <= 5; ++cn) { | |
629 | NFD_c[cn] = utf32_decompose_canon(c[cn], utf32_len(c[cn]), 0); | |
630 | NFKD_c[cn] = utf32_decompose_compat(c[cn], utf32_len(c[cn]), 0); | |
16506c9d RK |
631 | NFC_c[cn] = utf32_compose_canon(c[cn], utf32_len(c[cn]), 0); |
632 | NFKC_c[cn] = utf32_compose_compat(c[cn], utf32_len(c[cn]), 0); | |
e5a5a138 RK |
633 | } |
634 | #define unt_check(T, A, B) do { \ | |
635 | ++tests; \ | |
636 | if(utf32_cmp(c[A], T##_c[B])) { \ | |
e2452add RK |
637 | fprintf(stderr, \ |
638 | "NormalizationTest.txt:%d: c%d != "#T"(c%d)\n", \ | |
639 | lineno, A, B); \ | |
16506c9d | 640 | fprintf(stderr, " c%d:%s\n", \ |
e5a5a138 | 641 | A, format_utf32(c[A])); \ |
16506c9d RK |
642 | fprintf(stderr, " c%d:%s\n", \ |
643 | B, format_utf32(c[B])); \ | |
644 | fprintf(stderr, "%4s(c%d):%s\n", \ | |
e5a5a138 | 645 | #T, B, format_utf32(T##_c[B])); \ |
bcf9ed7f | 646 | count_error(); \ |
e5a5a138 RK |
647 | } \ |
648 | } while(0) | |
649 | unt_check(NFD, 3, 1); | |
650 | unt_check(NFD, 3, 2); | |
651 | unt_check(NFD, 3, 3); | |
652 | unt_check(NFD, 5, 4); | |
653 | unt_check(NFD, 5, 5); | |
654 | unt_check(NFKD, 5, 1); | |
655 | unt_check(NFKD, 5, 2); | |
656 | unt_check(NFKD, 5, 3); | |
657 | unt_check(NFKD, 5, 4); | |
658 | unt_check(NFKD, 5, 5); | |
16506c9d RK |
659 | unt_check(NFC, 2, 1); |
660 | unt_check(NFC, 2, 2); | |
661 | unt_check(NFC, 2, 3); | |
662 | unt_check(NFC, 4, 4); | |
663 | unt_check(NFC, 4, 5); | |
664 | unt_check(NFKC, 4, 1); | |
665 | unt_check(NFKC, 4, 2); | |
666 | unt_check(NFKC, 4, 3); | |
667 | unt_check(NFKC, 4, 4); | |
668 | unt_check(NFKC, 4, 5); | |
e5a5a138 RK |
669 | for(cn = 1; cn <= 5; ++cn) { |
670 | xfree(NFD_c[cn]); | |
671 | xfree(NFKD_c[cn]); | |
672 | } | |
673 | xfree(l); | |
674 | } | |
e2452add | 675 | fclose(fp); |
1625e11a | 676 | breaktest("auxiliary/GraphemeBreakTest.txt", utf32_is_grapheme_boundary); |
bb48024f | 677 | breaktest("auxiliary/WordBreakTest.txt", utf32_is_word_boundary); |
c35e83d9 RK |
678 | insist(utf32_combining_class(0x40000) == 0); |
679 | insist(utf32_combining_class(0xE0000) == 0); | |
e5a5a138 RK |
680 | } |
681 | ||
ea387d53 RK |
682 | static void test_signame(void) { |
683 | fprintf(stderr, "test_signame\n"); | |
684 | insist(find_signal("SIGTERM") == SIGTERM); | |
685 | insist(find_signal("SIGHUP") == SIGHUP); | |
686 | insist(find_signal("SIGINT") == SIGINT); | |
687 | insist(find_signal("SIGQUIT") == SIGQUIT); | |
688 | insist(find_signal("SIGKILL") == SIGKILL); | |
689 | insist(find_signal("SIGYOURMUM") == -1); | |
690 | } | |
691 | ||
9f28e855 RK |
692 | static void test_cache(void) { |
693 | const struct cache_type t1 = { 1 }, t2 = { 10 }; | |
694 | const char v11[] = "spong", v12[] = "wibble", v2[] = "blat"; | |
695 | fprintf(stderr, "test_cache\n"); | |
696 | cache_put(&t1, "1_1", v11); | |
697 | cache_put(&t1, "1_2", v12); | |
698 | cache_put(&t2, "2", v2); | |
699 | insist(cache_count() == 3); | |
700 | insist(cache_get(&t2, "2") == v2); | |
701 | insist(cache_get(&t1, "1_1") == v11); | |
702 | insist(cache_get(&t1, "1_2") == v12); | |
703 | insist(cache_get(&t1, "2") == 0); | |
704 | insist(cache_get(&t2, "1_1") == 0); | |
705 | insist(cache_get(&t2, "1_2") == 0); | |
706 | insist(cache_get(&t1, "2") == 0); | |
707 | insist(cache_get(&t2, "1_1") == 0); | |
708 | insist(cache_get(&t2, "1_2") == 0); | |
709 | sleep(2); | |
710 | cache_expire(); | |
711 | insist(cache_count() == 1); | |
712 | insist(cache_get(&t1, "1_1") == 0); | |
713 | insist(cache_get(&t1, "1_2") == 0); | |
714 | insist(cache_get(&t2, "2") == v2); | |
715 | cache_clean(0); | |
716 | insist(cache_count() == 0); | |
717 | insist(cache_get(&t2, "2") == 0); | |
718 | } | |
719 | ||
460b9539 | 720 | int main(void) { |
bb48024f | 721 | fail_first = !!getenv("FAIL_FIRST"); |
460b9539 | 722 | insist('\n' == 0x0A); |
723 | insist('\r' == 0x0D); | |
724 | insist(' ' == 0x20); | |
725 | insist('0' == 0x30); | |
726 | insist('9' == 0x39); | |
727 | insist('A' == 0x41); | |
728 | insist('Z' == 0x5A); | |
729 | insist('a' == 0x61); | |
730 | insist('z' == 0x7A); | |
731 | /* addr.c */ | |
732 | /* asprintf.c */ | |
733 | /* authhash.c */ | |
734 | /* basen.c */ | |
735 | /* charset.c */ | |
736 | /* client.c */ | |
737 | /* configuration.c */ | |
738 | /* event.c */ | |
739 | /* fprintf.c */ | |
033fd4e3 RK |
740 | /* heap.c */ |
741 | test_heap(); | |
460b9539 | 742 | /* hex.c */ |
743 | test_hex(); | |
744 | /* inputline.c */ | |
745 | /* kvp.c */ | |
746 | /* log.c */ | |
747 | /* mem.c */ | |
748 | /* mime.c */ | |
749 | test_mime(); | |
750 | /* mixer.c */ | |
751 | /* plugin.c */ | |
752 | /* printf.c */ | |
753 | /* queue.c */ | |
754 | /* sink.c */ | |
755 | /* snprintf.c */ | |
756 | /* split.c */ | |
757 | /* syscalls.c */ | |
758 | /* table.c */ | |
e5a5a138 RK |
759 | /* unicode.c */ |
760 | test_unicode(); | |
460b9539 | 761 | /* utf8.c */ |
762 | test_utf8(); | |
763 | /* vector.c */ | |
764 | /* words.c */ | |
765 | test_casefold(); | |
8818b7fc | 766 | test_words(); |
460b9539 | 767 | /* wstat.c */ |
ea387d53 RK |
768 | /* signame.c */ |
769 | test_signame(); | |
9f28e855 RK |
770 | /* cache.c */ |
771 | test_cache(); | |
460b9539 | 772 | fprintf(stderr, "%d errors out of %d tests\n", errors, tests); |
773 | return !!errors; | |
774 | } | |
775 | ||
776 | /* | |
777 | Local Variables: | |
778 | c-basic-offset:2 | |
779 | comment-column:40 | |
56fd389c RK |
780 | fill-column:79 |
781 | indent-tabs-mode:nil | |
460b9539 | 782 | End: |
783 | */ |