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