2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007 Richard Kettlewell
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.
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.
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
20 /** @file lib/test.c @brief Library tests */
31 #include <sys/types.h>
47 #include "inputline.h"
53 #include "selection.h"
60 static int tests, errors;
61 static int fail_first;
63 static void count_error() {
69 /** @brief Checks that @p expr is nonzero */
70 #define insist(expr) do { \
73 fprintf(stderr, "%s:%d: error checking %s\n", \
74 __FILE__, __LINE__, #expr); \
79 static const char *format(const char *s) {
85 while((c = (unsigned char)*s++)) {
86 if(c >= ' ' && c <= '~')
89 sprintf(buf, "\\x%02X", (unsigned)c);
90 dynstr_append_string(&d, buf);
97 static const char *format_utf32(const uint32_t *s) {
104 sprintf(buf, " %04lX", (long)c);
105 dynstr_append_string(&d, buf);
107 dynstr_terminate(&d);
111 #define check_string(GOT, WANT) do { \
112 const char *got = GOT; \
113 const char *want = WANT; \
116 fprintf(stderr, "%s:%d: %s returned 0\n", \
117 __FILE__, __LINE__, #GOT); \
119 } else if(strcmp(want, got)) { \
120 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \
121 __FILE__, __LINE__, #GOT, format(got), format(want)); \
127 #define check_string_prefix(GOT, WANT) do { \
128 const char *got = GOT; \
129 const char *want = WANT; \
132 fprintf(stderr, "%s:%d: %s returned 0\n", \
133 __FILE__, __LINE__, #GOT); \
135 } else if(strncmp(want, got, strlen(want))) { \
136 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n", \
137 __FILE__, __LINE__, #GOT, format(got), format(want)); \
143 #define check_integer(GOT, WANT) do { \
144 const intmax_t got = GOT, want = WANT; \
146 fprintf(stderr, "%s:%d: %s returned: %jd expected: %jd\n", \
147 __FILE__, __LINE__, #GOT, got, want); \
153 static uint32_t *ucs4parse(const char *s) {
154 struct dynstr_ucs4 d;
157 dynstr_ucs4_init(&d);
160 dynstr_ucs4_append(&d, strtoul(s, &e, 0));
161 if(errno) fatal(errno, "strtoul (%s)", s);
164 dynstr_ucs4_terminate(&d);
168 static void test_utf8(void) {
169 /* Test validutf8, convert to UCS-4, check the answer is right,
170 * convert back to UTF-8, check we got to where we started */
171 #define U8(CHARS, WORDS) do { \
172 uint32_t *w = ucs4parse(WORDS); \
176 insist(validutf8(CHARS)); \
177 ucs = utf8_to_utf32(CHARS, strlen(CHARS), 0); \
179 insist(!utf32_cmp(w, ucs)); \
180 u8 = utf32_to_utf8(ucs, utf32_len(ucs), 0); \
182 check_string(u8, CHARS); \
185 fprintf(stderr, "test_utf8\n");
186 #define validutf8(S) utf8_valid((S), strlen(S))
192 /* ASCII characters */
194 U8(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
195 "0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d "
196 "0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a "
197 "0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 "
198 "0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 "
199 "0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 "
200 "0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e "
201 "0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b "
203 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",
204 "0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 "
205 "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d "
210 /* UTF8-2 = %xC2-DF UTF8-tail */
211 insist(!validutf8("\xC0\x80"));
212 insist(!validutf8("\xC1\x80"));
213 insist(!validutf8("\xC2\x7F"));
214 U8("\xC2\x80", "0x80");
215 U8("\xDF\xBF", "0x7FF");
216 insist(!validutf8("\xDF\xC0"));
218 /* UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
219 * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
221 insist(!validutf8("\xE0\x9F\x80"));
222 U8("\xE0\xA0\x80", "0x800");
223 U8("\xE0\xBF\xBF", "0xFFF");
224 insist(!validutf8("\xE0\xC0\xBF"));
226 insist(!validutf8("\xE1\x80\x7F"));
227 U8("\xE1\x80\x80", "0x1000");
228 U8("\xEC\xBF\xBF", "0xCFFF");
229 insist(!validutf8("\xEC\xC0\xBF"));
231 U8("\xED\x80\x80", "0xD000");
232 U8("\xED\x9F\xBF", "0xD7FF");
233 insist(!validutf8("\xED\xA0\xBF"));
235 insist(!validutf8("\xEE\x7f\x80"));
236 U8("\xEE\x80\x80", "0xE000");
237 U8("\xEF\xBF\xBF", "0xFFFF");
238 insist(!validutf8("\xEF\xC0\xBF"));
240 /* UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
241 * %xF4 %x80-8F 2( UTF8-tail )
243 insist(!validutf8("\xF0\x8F\x80\x80"));
244 U8("\xF0\x90\x80\x80", "0x10000");
245 U8("\xF0\xBF\xBF\xBF", "0x3FFFF");
246 insist(!validutf8("\xF0\xC0\x80\x80"));
248 insist(!validutf8("\xF1\x80\x80\x7F"));
249 U8("\xF1\x80\x80\x80", "0x40000");
250 U8("\xF3\xBF\xBF\xBF", "0xFFFFF");
251 insist(!validutf8("\xF3\xC0\x80\x80"));
253 insist(!validutf8("\xF4\x80\x80\x7F"));
254 U8("\xF4\x80\x80\x80", "0x100000");
255 U8("\xF4\x8F\xBF\xBF", "0x10FFFF");
256 insist(!validutf8("\xF4\x90\x80\x80"));
257 insist(!validutf8("\xF4\x80\xFF\x80"));
259 /* miscellaneous non-UTF-8 rubbish */
260 insist(!validutf8("\x80"));
261 insist(!validutf8("\xBF"));
262 insist(!validutf8("\xC0"));
263 insist(!validutf8("\xC0\x7F"));
264 insist(!validutf8("\xC0\xC0"));
265 insist(!validutf8("\xE0"));
266 insist(!validutf8("\xE0\x7F"));
267 insist(!validutf8("\xE0\xC0"));
268 insist(!validutf8("\xE0\x80"));
269 insist(!validutf8("\xE0\x80\x7f"));
270 insist(!validutf8("\xE0\x80\xC0"));
271 insist(!validutf8("\xF0"));
272 insist(!validutf8("\xF0\x7F"));
273 insist(!validutf8("\xF0\xC0"));
274 insist(!validutf8("\xF0\x80"));
275 insist(!validutf8("\xF0\x80\x7f"));
276 insist(!validutf8("\xF0\x80\xC0"));
277 insist(!validutf8("\xF0\x80\x80\x7f"));
278 insist(!validutf8("\xF0\x80\x80\xC0"));
279 insist(!validutf8("\xF5\x80\x80\x80"));
280 insist(!validutf8("\xF8"));
283 static int test_multipart_callback(const char *s, void *u) {
284 struct vector *parts = u;
286 vector_append(parts, (char *)s);
290 static void test_mime(void) {
292 struct vector parts[1];
294 fprintf(stderr, "test_mime\n");
297 insist(!mime_content_type("text/plain", &t, &n, &v));
298 check_string(t, "text/plain");
302 insist(mime_content_type("TEXT ((broken) comment", &t, &n, &v) < 0);
303 insist(mime_content_type("TEXT ((broken) comment\\", &t, &n, &v) < 0);
306 insist(!mime_content_type("TEXT ((nested)\\ comment) /plain", &t, &n, &v));
307 check_string(t, "text/plain");
312 insist(!mime_content_type(" text/plain ; Charset=\"utf-\\8\"", &t, &n, &v));
313 check_string(t, "text/plain");
314 check_string(n, "charset");
315 check_string(v, "utf-8");
318 insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t, &n, &v));
319 check_string(t, "text/plain");
320 check_string(n, "charset");
321 check_string(v, "ISO-8859-1");
324 insist(!mime_rfc2388_content_disposition("form-data; name=\"field1\"", &t, &n, &v));
325 check_string(t, "form-data");
326 check_string(n, "name");
327 check_string(v, "field1");
329 insist(!mime_rfc2388_content_disposition("inline", &t, &n, &v));
330 check_string(t, "inline");
334 /* Current versions of the code only understand a single arg to these
335 * headers. This is a bug at the level they work at but suffices for
336 * DisOrder's current purposes. */
338 insist(!mime_rfc2388_content_disposition(
339 "attachment; filename=genome.jpeg;\n"
340 "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\"",
342 check_string(t, "attachment");
343 check_string(n, "filename");
344 check_string(v, "genome.jpeg");
347 insist(mime_multipart("--outer\r\n"
348 "Content-Type: text/plain\r\n"
349 "Content-Disposition: inline\r\n"
350 "Content-Description: text-part-1\r\n"
352 "Some text goes here\r\n"
355 "Content-Type: multipart/mixed; boundary=inner\r\n"
356 "Content-Disposition: attachment\r\n"
357 "Content-Description: multipart-2\r\n"
360 "Content-Type: text/plain\r\n"
361 "Content-Disposition: inline\r\n"
362 "Content-Description: text-part-2\r\n"
364 "Some more text here.\r\n"
367 "Content-Type: image/jpeg\r\n"
368 "Content-Disposition: attachment\r\n"
369 "Content-Description: jpeg-1\r\n"
374 test_multipart_callback,
377 check_integer(parts->nvec, 2);
378 check_string(parts->vec[0],
379 "Content-Type: text/plain\r\n"
380 "Content-Disposition: inline\r\n"
381 "Content-Description: text-part-1\r\n"
383 "Some text goes here\r\n");
384 check_string(parts->vec[1],
385 "Content-Type: multipart/mixed; boundary=inner\r\n"
386 "Content-Disposition: attachment\r\n"
387 "Content-Description: multipart-2\r\n"
390 "Content-Type: text/plain\r\n"
391 "Content-Disposition: inline\r\n"
392 "Content-Description: text-part-2\r\n"
394 "Some more text here.\r\n"
397 "Content-Type: image/jpeg\r\n"
398 "Content-Disposition: attachment\r\n"
399 "Content-Description: jpeg-1\r\n"
403 /* No trailing CRLF is _correct_ - see RFC2046 5.1.1 note regarding CRLF
404 * preceding the boundary delimiter line. An implication of this is that we
405 * must cope with partial lines at the end of the input when recursively
406 * decomposing a multipart message. */
408 insist(mime_multipart("--inner\r\n"
409 "Content-Type: text/plain\r\n"
410 "Content-Disposition: inline\r\n"
411 "Content-Description: text-part-2\r\n"
413 "Some more text here.\r\n"
416 "Content-Type: image/jpeg\r\n"
417 "Content-Disposition: attachment\r\n"
418 "Content-Description: jpeg-1\r\n"
422 test_multipart_callback,
425 check_integer(parts->nvec, 2);
426 check_string(parts->vec[0],
427 "Content-Type: text/plain\r\n"
428 "Content-Disposition: inline\r\n"
429 "Content-Description: text-part-2\r\n"
431 "Some more text here.\r\n");
432 check_string(parts->vec[1],
433 "Content-Type: image/jpeg\r\n"
434 "Content-Disposition: attachment\r\n"
435 "Content-Description: jpeg-1\r\n"
441 check_string(mime_qp(""), "");
442 check_string(mime_qp("foobar"), "foobar");
443 check_string(mime_qp("foo=20bar"), "foo bar");
444 check_string(mime_qp("x \r\ny"), "x\r\ny");
445 check_string(mime_qp("x=\r\ny"), "xy");
446 check_string(mime_qp("x= \r\ny"), "xy");
447 check_string(mime_qp("x =\r\ny"), "x y");
448 check_string(mime_qp("x = \r\ny"), "x y");
451 check_string(mime_qp("Now's the time =\r\n"
452 "for all folk to come=\r\n"
453 " to the aid of their country."),
454 "Now's the time for all folk to come to the aid of their country.");
456 check_string(mime_base64(""), "");
457 check_string(mime_base64("BBBB"), "\x04\x10\x41");
458 check_string(mime_base64("////"), "\xFF\xFF\xFF");
459 check_string(mime_base64("//BB"), "\xFF\xF0\x41");
460 check_string(mime_base64("BBBB//BB////"),
461 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
462 check_string(mime_base64("B B B B / / B B / / / /"),
463 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
464 check_string(mime_base64("B\r\nBBB.// B-B//~//"),
465 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
466 check_string(mime_base64("BBBB="),
468 check_string(mime_base64("BBBBx="), /* not actually valid base64 */
470 check_string(mime_base64("BBBB BB=="),
471 "\x04\x10\x41" "\x04");
472 check_string(mime_base64("BBBB BBB="),
473 "\x04\x10\x41" "\x04\x10");
476 static void test_hex(void) {
478 static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F };
482 fprintf(stderr, "test_hex\n");
484 for(n = 0; n <= UCHAR_MAX; ++n) {
486 insist(unhexdigitq(n) == -1);
488 insist(unhexdigitq('0') == 0);
489 insist(unhexdigitq('1') == 1);
490 insist(unhexdigitq('2') == 2);
491 insist(unhexdigitq('3') == 3);
492 insist(unhexdigitq('4') == 4);
493 insist(unhexdigitq('5') == 5);
494 insist(unhexdigitq('6') == 6);
495 insist(unhexdigitq('7') == 7);
496 insist(unhexdigitq('8') == 8);
497 insist(unhexdigitq('9') == 9);
498 insist(unhexdigitq('a') == 10);
499 insist(unhexdigitq('b') == 11);
500 insist(unhexdigitq('c') == 12);
501 insist(unhexdigitq('d') == 13);
502 insist(unhexdigitq('e') == 14);
503 insist(unhexdigitq('f') == 15);
504 insist(unhexdigitq('A') == 10);
505 insist(unhexdigitq('B') == 11);
506 insist(unhexdigitq('C') == 12);
507 insist(unhexdigitq('D') == 13);
508 insist(unhexdigitq('E') == 14);
509 insist(unhexdigitq('F') == 15);
510 check_string(hex(h, sizeof h), "00ff807f");
511 check_string(hex(0, 0), "");
512 u = unhex("00ff807f", &ul);
514 insist(memcmp(u, h, 4) == 0);
515 u = unhex("00FF807F", &ul);
517 insist(memcmp(u, h, 4) == 0);
520 fprintf(stderr, "2 ERROR reports expected {\n");
521 insist(unhex("F", 0) == 0);
522 insist(unhex("az", 0) == 0);
523 fprintf(stderr, "}\n");
526 static void test_casefold(void) {
528 const char *input, *canon_folded, *compat_folded, *canon_expected, *compat_expected;
530 fprintf(stderr, "test_casefold\n");
532 /* This isn't a very exhaustive test. Unlike for normalization, there don't
533 * seem to be any public test vectors for these algorithms. */
535 for(c = 1; c < 256; ++c) {
536 input = utf32_to_utf8(&c, 1, 0);
537 canon_folded = utf8_casefold_canon(input, strlen(input), 0);
538 compat_folded = utf8_casefold_compat(input, strlen(input), 0);
541 if((c >= 'A' && c <= 'Z')
542 || (c >= 0xC0 && c <= 0xDE && c != 0xD7))
547 case 0xB5: /* MICRO SIGN */
548 l = 0x3BC; /* GREEK SMALL LETTER MU */
550 case 0xDF: /* LATIN SMALL LETTER SHARP S */
551 check_string(canon_folded, "ss");
552 check_string(compat_folded, "ss");
558 /* Case-folded data is now normalized */
559 d = utf32_decompose_canon(&l, 1, 0);
560 canon_expected = utf32_to_utf8(d, utf32_len(d), 0);
561 if(strcmp(canon_folded, canon_expected)) {
562 fprintf(stderr, "%s:%d: canon-casefolding %#lx got '%s', expected '%s'\n",
563 __FILE__, __LINE__, (unsigned long)c,
564 format(canon_folded), format(canon_expected));
568 d = utf32_decompose_compat(&l, 1, 0);
569 compat_expected = utf32_to_utf8(d, utf32_len(d), 0);
570 if(strcmp(compat_folded, compat_expected)) {
571 fprintf(stderr, "%s:%d: compat-casefolding %#lx got '%s', expected '%s'\n",
572 __FILE__, __LINE__, (unsigned long)c,
573 format(compat_folded), format(compat_expected));
579 check_string(utf8_casefold_canon("", 0, 0), "");
584 const char *expect[10];
588 /* Only whitespace and punctuation */
595 { "wibble", { "wibble", 0 } },
596 { " wibble", { "wibble", 0 } },
597 { " wibble ", { "wibble", 0 } },
598 { "wibble ", { "wibble", 0 } },
599 { "wibble spong", { "wibble", "spong", 0 } },
600 { " wibble spong", { "wibble", "spong", 0 } },
601 { " wibble spong ", { "wibble", "spong", 0 } },
602 { "wibble spong ", { "wibble", "spong", 0 } },
603 { "wibble spong splat foo zot ", { "wibble", "spong", "splat", "foo", "zot", 0 } },
605 { "wibble 'spong", { "wibble", "spong", 0 } },
606 { " wibble's", { "wibble's", 0 } },
607 { " wibblespong' ", { "wibblespong", 0 } },
608 { "wibble sp''ong ", { "wibble", "sp", "ong", 0 } },
610 #define NWTEST (sizeof wtest / sizeof *wtest)
612 static void test_words(void) {
613 size_t t, nexpect, ngot, i;
616 fprintf(stderr, "test_words\n");
617 for(t = 0; t < NWTEST; ++t) {
618 char **got = utf8_word_split(wtest[t].in, strlen(wtest[t].in), &ngot, 0);
620 for(nexpect = 0; wtest[t].expect[nexpect]; ++nexpect)
622 if(nexpect == ngot) {
623 for(i = 0; i < ngot; ++i)
624 if(strcmp(wtest[t].expect[i], got[i]))
630 fprintf(stderr, "word split %zu failed\n", t);
631 fprintf(stderr, "input: %s\n", wtest[t].in);
632 fprintf(stderr, " | %-30s | %-30s\n",
634 for(i = 0; i < nexpect || i < ngot; ++i) {
635 const char *e = i < nexpect ? wtest[t].expect[i] : "<none>";
636 const char *g = i < ngot ? got[i] : "<none>";
637 fprintf(stderr, " %2zu | %-30s | %-30s\n", i, e, g);
645 /** @brief Less-than comparison function for integer heap */
646 static inline int int_lt(int a, int b) { return a < b; }
649 * @brief A heap with @c int elements */
650 HEAP_TYPE(iheap, int, int_lt);
651 HEAP_DEFINE(iheap, int, int_lt);
653 /** @brief Tests for @ref heap.h */
654 static void test_heap(void) {
659 fprintf(stderr, "test_heap\n");
662 for(n = 0; n < 1000; ++n)
663 iheap_insert(h, random() % 100);
664 for(n = 0; n < 1000; ++n) {
665 const int latest = iheap_remove(h);
667 fprintf(stderr, "should have %d <= %d\n", last, latest);
668 insist(last <= latest);
674 /** @brief Open a Unicode test file */
675 static FILE *open_unicode_test(const char *path) {
681 if((base = strrchr(path, '/')))
685 if(!(fp = fopen(base, "r"))) {
686 snprintf(buffer, sizeof buffer,
687 "wget http://www.unicode.org/Public/5.0.0/ucd/%s", path);
688 if((w = system(buffer)))
689 fatal(0, "%s: %s", buffer, wstat(w));
690 if(chmod(base, 0444) < 0)
691 fatal(errno, "chmod %s", base);
692 if(!(fp = fopen(base, "r")))
693 fatal(errno, "%s", base);
698 /** @brief Run breaking tests for utf32_grapheme_boundary() etc */
699 static void breaktest(const char *path,
700 int (*breakfn)(const uint32_t *, size_t, size_t)) {
701 FILE *fp = open_unicode_test(path);
705 char break_allowed[1024];
706 uint32_t buffer[1024];
708 while(!inputline(path, fp, &l, '\n')) {
710 if(l[0] == '#') continue;
714 if(*lp == ' ' || *lp == '\t') {
720 if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0xB7) {
721 /* 00F7 DIVISION SIGN */
722 break_allowed[bn] = 1;
726 if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0x97) {
727 /* 00D7 MULTIPLICATION SIGN */
728 break_allowed[bn] = 0;
732 if(isxdigit((unsigned char)*lp)) {
733 buffer[bn++] = strtoul(lp, &lp, 16);
736 fatal(0, "%s:%d: evil line: %s", path, lineno, l);
738 for(n = 0; n <= bn; ++n) {
739 if(breakfn(buffer, bn, n) != break_allowed[n]) {
741 "%s:%d: offset %zu: mismatch\n"
754 /** @brief Tests for @ref lib/unicode.h */
755 static void test_unicode(void) {
759 uint32_t buffer[1024];
760 uint32_t *c[6], *NFD_c[6], *NFKD_c[6], *NFC_c[6], *NFKC_c[6]; /* 1-indexed */
763 fprintf(stderr, "test_unicode\n");
764 fp = open_unicode_test("NormalizationTest.txt");
765 while(!inputline("NormalizationTest.txt", fp, &l, '\n')) {
767 if(*l == '#' || *l == '@')
772 c[cn++] = &buffer[bn];
773 while(*lp && *lp != '#') {
782 c[cn++] = &buffer[bn];
786 buffer[bn++] = strtoul(lp, &lp, 16);
790 for(cn = 1; cn <= 5; ++cn) {
791 NFD_c[cn] = utf32_decompose_canon(c[cn], utf32_len(c[cn]), 0);
792 NFKD_c[cn] = utf32_decompose_compat(c[cn], utf32_len(c[cn]), 0);
793 NFC_c[cn] = utf32_compose_canon(c[cn], utf32_len(c[cn]), 0);
794 NFKC_c[cn] = utf32_compose_compat(c[cn], utf32_len(c[cn]), 0);
796 #define unt_check(T, A, B) do { \
798 if(utf32_cmp(c[A], T##_c[B])) { \
800 "NormalizationTest.txt:%d: c%d != "#T"(c%d)\n", \
802 fprintf(stderr, " c%d:%s\n", \
803 A, format_utf32(c[A])); \
804 fprintf(stderr, " c%d:%s\n", \
805 B, format_utf32(c[B])); \
806 fprintf(stderr, "%4s(c%d):%s\n", \
807 #T, B, format_utf32(T##_c[B])); \
811 unt_check(NFD, 3, 1);
812 unt_check(NFD, 3, 2);
813 unt_check(NFD, 3, 3);
814 unt_check(NFD, 5, 4);
815 unt_check(NFD, 5, 5);
816 unt_check(NFKD, 5, 1);
817 unt_check(NFKD, 5, 2);
818 unt_check(NFKD, 5, 3);
819 unt_check(NFKD, 5, 4);
820 unt_check(NFKD, 5, 5);
821 unt_check(NFC, 2, 1);
822 unt_check(NFC, 2, 2);
823 unt_check(NFC, 2, 3);
824 unt_check(NFC, 4, 4);
825 unt_check(NFC, 4, 5);
826 unt_check(NFKC, 4, 1);
827 unt_check(NFKC, 4, 2);
828 unt_check(NFKC, 4, 3);
829 unt_check(NFKC, 4, 4);
830 unt_check(NFKC, 4, 5);
831 for(cn = 1; cn <= 5; ++cn) {
838 breaktest("auxiliary/GraphemeBreakTest.txt", utf32_is_grapheme_boundary);
839 breaktest("auxiliary/WordBreakTest.txt", utf32_is_word_boundary);
840 insist(utf32_combining_class(0x40000) == 0);
841 insist(utf32_combining_class(0xE0000) == 0);
844 static void test_signame(void) {
845 fprintf(stderr, "test_signame\n");
846 insist(find_signal("SIGTERM") == SIGTERM);
847 insist(find_signal("SIGHUP") == SIGHUP);
848 insist(find_signal("SIGINT") == SIGINT);
849 insist(find_signal("SIGQUIT") == SIGQUIT);
850 insist(find_signal("SIGKILL") == SIGKILL);
851 insist(find_signal("SIGYOURMUM") == -1);
854 static void test_cache(void) {
855 const struct cache_type t1 = { 1 }, t2 = { 10 };
856 const char v11[] = "spong", v12[] = "wibble", v2[] = "blat";
857 fprintf(stderr, "test_cache\n");
858 cache_put(&t1, "1_1", v11);
859 cache_put(&t1, "1_2", v12);
860 cache_put(&t2, "2", v2);
861 insist(cache_count() == 3);
862 insist(cache_get(&t2, "2") == v2);
863 insist(cache_get(&t1, "1_1") == v11);
864 insist(cache_get(&t1, "1_2") == v12);
865 insist(cache_get(&t1, "2") == 0);
866 insist(cache_get(&t2, "1_1") == 0);
867 insist(cache_get(&t2, "1_2") == 0);
868 insist(cache_get(&t1, "2") == 0);
869 insist(cache_get(&t2, "1_1") == 0);
870 insist(cache_get(&t2, "1_2") == 0);
873 insist(cache_count() == 1);
874 insist(cache_get(&t1, "1_1") == 0);
875 insist(cache_get(&t1, "1_2") == 0);
876 insist(cache_get(&t2, "2") == v2);
878 insist(cache_count() == 0);
879 insist(cache_get(&t2, "2") == 0);
882 static void test_filepart(void) {
883 fprintf(stderr, "test_filepart\n");
884 check_string(d_dirname("/"), "/");
885 check_string(d_dirname("/spong"), "/");
886 check_string(d_dirname("/foo/bar"), "/foo");
887 check_string(d_dirname("./bar"), ".");
888 check_string(d_dirname("."), ".");
889 check_string(d_dirname(".."), ".");
890 check_string(d_dirname("../blat"), "..");
891 check_string(d_dirname("wibble"), ".");
892 check_string(extension("foo.c"), ".c");
893 check_string(extension(".c"), ".c");
894 check_string(extension("."), ".");
895 check_string(extension("foo"), "");
896 check_string(extension("./foo"), "");
897 check_string(extension("./foo.c"), ".c");
900 static void test_selection(void) {
902 fprintf(stderr, "test_selection\n");
903 insist((h = selection_new()) != 0);
904 selection_set(h, "one", 1);
905 selection_set(h, "two", 1);
906 selection_set(h, "three", 0);
907 selection_set(h, "four", 1);
908 insist(selection_selected(h, "one") == 1);
909 insist(selection_selected(h, "two") == 1);
910 insist(selection_selected(h, "three") == 0);
911 insist(selection_selected(h, "four") == 1);
912 insist(selection_selected(h, "five") == 0);
913 insist(hash_count(h) == 3);
914 selection_flip(h, "one");
915 selection_flip(h, "three");
916 insist(selection_selected(h, "one") == 0);
917 insist(selection_selected(h, "three") == 1);
918 insist(hash_count(h) == 3);
919 selection_live(h, "one");
920 selection_live(h, "two");
921 selection_live(h, "three");
922 selection_cleanup(h);
923 insist(selection_selected(h, "one") == 0);
924 insist(selection_selected(h, "two") == 1);
925 insist(selection_selected(h, "three") == 1);
926 insist(selection_selected(h, "four") == 0);
927 insist(selection_selected(h, "five") == 0);
928 insist(hash_count(h) == 2);
930 insist(selection_selected(h, "one") == 0);
931 insist(selection_selected(h, "two") == 0);
932 insist(selection_selected(h, "three") == 0);
933 insist(selection_selected(h, "four") == 0);
934 insist(selection_selected(h, "five") == 0);
935 insist(hash_count(h) == 0);
938 static void test_wstat(void) {
942 fprintf(stderr, "test_wstat\n");
943 if(!(pid = xfork())) {
946 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
948 check_string(wstat(w), "exited with status 1");
949 if(!(pid = xfork())) {
950 kill(getpid(), SIGTERM);
953 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
955 check_string_prefix(wstat(w), "terminated by signal 15");
958 static void test_kvp(void) {
962 fprintf(stderr, "test_kvp\n");
964 #define KVP_URLDECODE(S) kvp_urldecode((S), strlen(S))
965 insist(KVP_URLDECODE("=%zz") == 0);
966 insist(KVP_URLDECODE("=%0") == 0);
967 insist(KVP_URLDECODE("=%0z") == 0);
968 insist(KVP_URLDECODE("=%%") == 0);
969 insist(KVP_URLDECODE("==%") == 0);
970 insist(KVP_URLDECODE("wibble") == 0);
971 insist(KVP_URLDECODE("") == 0);
972 insist(KVP_URLDECODE("wibble&") == 0);
973 insist((k = KVP_URLDECODE("one=bl%61t+foo")) != 0);
974 check_string(kvp_get(k, "one"), "blat foo");
975 insist(kvp_get(k, "ONE") == 0);
976 insist(k->next == 0);
977 insist((k = KVP_URLDECODE("wibble=splat&bar=spong")) != 0);
978 check_string(kvp_get(k, "wibble"), "splat");
979 check_string(kvp_get(k, "bar"), "spong");
980 insist(kvp_get(k, "ONE") == 0);
981 insist(k->next->next == 0);
983 insist(kvp_set(&k, "bar", "spong") == 0);
984 insist(kvp_set(&k, "bar", "foo") == 1);
985 insist(kvp_set(&k, "zog", "%") == 1);
986 insist(kvp_set(&k, "wibble", 0) == 1);
987 insist(kvp_set(&k, "wibble", 0) == 0);
988 check_string(kvp_urlencode(k, 0),
990 check_string(kvp_urlencode(k, &n),
992 insist(n == strlen("bar=foo&zog=%25"));
993 check_string(urlencodestring("abc% +\n"),
997 static void test_sink(void) {
1003 fprintf(stderr, "test_sink\n");
1007 s = sink_stdio("tmpfile", fp);
1008 insist(sink_printf(s, "test: %d\n", 999) == 10);
1009 insist(sink_printf(s, "wibble: %s\n", "foobar") == 15);
1011 insist(inputline("tmpfile", fp, &l, '\n') == 0);
1012 check_string(l, "test: 999");
1013 insist(inputline("tmpfile", fp, &l, '\n') == 0);
1014 check_string(l, "wibble: foobar");
1015 insist(inputline("tmpfile", fp, &l, '\n') == -1);
1019 insist(sink_printf(s, "test: %d\n", 999) == 10);
1020 insist(sink_printf(s, "wibble: %s\n", "foobar") == 15);
1021 dynstr_terminate(d);
1022 check_string(d->vec, "test: 999\nwibble: foobar\n");
1025 static const char *do_printf(const char *fmt, ...) {
1031 rc = byte_vasprintf(&s, fmt, ap);
1038 static void test_printf(void) {
1048 fprintf(stderr, "test_printf\n");
1049 check_string(do_printf("%d", 999), "999");
1050 check_string(do_printf("%d", -999), "-999");
1051 check_string(do_printf("%i", 999), "999");
1052 check_string(do_printf("%i", -999), "-999");
1053 check_string(do_printf("%u", 999), "999");
1054 check_string(do_printf("%2u", 999), "999");
1055 check_string(do_printf("%10u", 999), " 999");
1056 check_string(do_printf("%-10u", 999), "999 ");
1057 check_string(do_printf("%010u", 999), "0000000999");
1058 check_string(do_printf("%-10d", -999), "-999 ");
1059 check_string(do_printf("%-010d", -999), "-999 "); /* "-" beats "0" */
1060 check_string(do_printf("%66u", 999), " 999");
1061 check_string(do_printf("%o", 999), "1747");
1062 check_string(do_printf("%#o", 999), "01747");
1063 check_string(do_printf("%#o", 0), "0");
1064 check_string(do_printf("%x", 999), "3e7");
1065 check_string(do_printf("%#x", 999), "0x3e7");
1066 check_string(do_printf("%#X", 999), "0X3E7");
1067 check_string(do_printf("%#x", 0), "0");
1068 check_string(do_printf("%hd", (short)999), "999");
1069 check_string(do_printf("%hhd", (short)99), "99");
1070 check_string(do_printf("%ld", 100000L), "100000");
1071 check_string(do_printf("%lld", 10000000000LL), "10000000000");
1072 check_string(do_printf("%qd", 10000000000LL), "10000000000");
1073 check_string(do_printf("%jd", (intmax_t)10000000000LL), "10000000000");
1074 check_string(do_printf("%zd", (ssize_t)2000000000), "2000000000");
1075 check_string(do_printf("%td", (ptrdiff_t)2000000000), "2000000000");
1076 check_string(do_printf("%hu", (short)999), "999");
1077 check_string(do_printf("%hhu", (short)99), "99");
1078 check_string(do_printf("%lu", 100000L), "100000");
1079 check_string(do_printf("%llu", 10000000000LL), "10000000000");
1080 check_string(do_printf("%ju", (uintmax_t)10000000000LL), "10000000000");
1081 check_string(do_printf("%zu", (size_t)2000000000), "2000000000");
1082 check_string(do_printf("%tu", (ptrdiff_t)2000000000), "2000000000");
1083 check_string(do_printf("%p", (void *)0x100), "0x100");
1084 check_string(do_printf("%s", "wibble"), "wibble");
1085 check_string(do_printf("%s-%s", "wibble", "wobble"), "wibble-wobble");
1086 check_string(do_printf("%10s", "wibble"), " wibble");
1087 check_string(do_printf("%010s", "wibble"), " wibble"); /* 0 ignored for %s */
1088 check_string(do_printf("%-10s", "wibble"), "wibble ");
1089 check_string(do_printf("%2s", "wibble"), "wibble");
1090 check_string(do_printf("%.2s", "wibble"), "wi");
1091 check_string(do_printf("%.2s", "w"), "w");
1092 check_string(do_printf("%4.2s", "wibble"), " wi");
1093 check_string(do_printf("%c", 'a'), "a");
1094 check_string(do_printf("%4c", 'a'), " a");
1095 check_string(do_printf("%-4c", 'a'), "a ");
1096 check_string(do_printf("%*c", 0, 'a'), "a");
1097 check_string(do_printf("x%hhny", &c), "xy");
1099 check_string(do_printf("xx%hnyy", &s), "xxyy");
1101 check_string(do_printf("xxx%nyyy", &i), "xxxyyy");
1103 check_string(do_printf("xxxx%lnyyyy", &l), "xxxxyyyy");
1105 check_string(do_printf("xxxxx%llnyyyyy", &ll), "xxxxxyyyyy");
1107 check_string(do_printf("xxxxxx%jnyyyyyy", &m), "xxxxxxyyyyyy");
1109 check_string(do_printf("xxxxxxx%znyyyyyyy", &ssz), "xxxxxxxyyyyyyy");
1111 check_string(do_printf("xxxxxxxx%tnyyyyyyyy", &p), "xxxxxxxxyyyyyyyy");
1113 check_string(do_printf("%*d", 5, 99), " 99");
1114 check_string(do_printf("%*d", -5, 99), "99 ");
1115 check_string(do_printf("%.*d", 5, 99), "00099");
1116 check_string(do_printf("%.*d", -5, 99), "99");
1117 check_string(do_printf("%.0d", 0), "");
1118 check_string(do_printf("%.d", 0), "");
1119 check_string(do_printf("%.d", 0), "");
1120 check_string(do_printf("%%"), "%");
1121 check_string(do_printf("wibble"), "wibble");
1122 insist(do_printf("%") == 0);
1123 insist(do_printf("%=") == 0);
1126 static void test_basen(void) {
1127 unsigned long v[64];
1130 fprintf(stderr, "test_basen\n");
1132 insist(basen(v, 1, buffer, sizeof buffer, 10) == 0);
1133 check_string(buffer, "999");
1135 v[0] = 1+2*7+3*7*7+4*7*7*7;
1136 insist(basen(v, 1, buffer, sizeof buffer, 7) == 0);
1137 check_string(buffer, "4321");
1143 insist(basen(v, 4, buffer, sizeof buffer, 256) == 0);
1144 check_string(buffer, "123456789abcdef");
1150 insist(basen(v, 4, buffer, sizeof buffer, 16) == 0);
1151 check_string(buffer, "102030405060708090a0b0c0d0e0f");
1157 insist(basen(v, 4, buffer, 10, 16) == -1);
1161 fail_first = !!getenv("FAIL_FIRST");
1162 insist('\n' == 0x0A);
1163 insist('\r' == 0x0D);
1164 insist(' ' == 0x20);
1165 insist('0' == 0x30);
1166 insist('9' == 0x39);
1167 insist('A' == 0x41);
1168 insist('Z' == 0x5A);
1169 insist('a' == 0x61);
1170 insist('z' == 0x7A);
1178 /* configuration.c */
1221 fprintf(stderr, "%d errors out of %d tests\n", errors, tests);
1230 indent-tabs-mode:nil