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