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>
46 #include "inputline.h"
52 #include "selection.h"
56 static int tests, errors;
57 static int fail_first;
59 static void count_error() {
65 /** @brief Checks that @p expr is nonzero */
66 #define insist(expr) do { \
69 fprintf(stderr, "%s:%d: error checking %s\n", \
70 __FILE__, __LINE__, #expr); \
75 static const char *format(const char *s) {
81 while((c = (unsigned char)*s++)) {
82 if(c >= ' ' && c <= '~')
85 sprintf(buf, "\\x%02X", (unsigned)c);
86 dynstr_append_string(&d, buf);
93 static const char *format_utf32(const uint32_t *s) {
100 sprintf(buf, " %04lX", (long)c);
101 dynstr_append_string(&d, buf);
103 dynstr_terminate(&d);
107 #define check_string(GOT, WANT) do { \
108 const char *got = GOT; \
109 const char *want = WANT; \
112 fprintf(stderr, "%s:%d: %s returned 0\n", \
113 __FILE__, __LINE__, #GOT); \
115 } else if(strcmp(want, got)) { \
116 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \
117 __FILE__, __LINE__, #GOT, format(got), format(want)); \
123 #define check_string_prefix(GOT, WANT) do { \
124 const char *got = GOT; \
125 const char *want = WANT; \
128 fprintf(stderr, "%s:%d: %s returned 0\n", \
129 __FILE__, __LINE__, #GOT); \
131 } else if(strncmp(want, got, strlen(want))) { \
132 fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s...\n", \
133 __FILE__, __LINE__, #GOT, format(got), format(want)); \
139 static uint32_t *ucs4parse(const char *s) {
140 struct dynstr_ucs4 d;
143 dynstr_ucs4_init(&d);
146 dynstr_ucs4_append(&d, strtoul(s, &e, 0));
147 if(errno) fatal(errno, "strtoul (%s)", s);
150 dynstr_ucs4_terminate(&d);
154 static void test_utf8(void) {
155 /* Test validutf8, convert to UCS-4, check the answer is right,
156 * convert back to UTF-8, check we got to where we started */
157 #define U8(CHARS, WORDS) do { \
158 uint32_t *w = ucs4parse(WORDS); \
162 insist(validutf8(CHARS)); \
163 ucs = utf8_to_utf32(CHARS, strlen(CHARS), 0); \
165 insist(!utf32_cmp(w, ucs)); \
166 u8 = utf32_to_utf8(ucs, utf32_len(ucs), 0); \
168 insist(!strcmp(u8, CHARS)); \
171 fprintf(stderr, "test_utf8\n");
172 #define validutf8(S) utf8_valid((S), strlen(S))
178 /* ASCII characters */
180 U8(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
181 "0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d "
182 "0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a "
183 "0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 "
184 "0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 "
185 "0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 "
186 "0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e "
187 "0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b "
189 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",
190 "0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 "
191 "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d "
196 /* UTF8-2 = %xC2-DF UTF8-tail */
197 insist(!validutf8("\xC0\x80"));
198 insist(!validutf8("\xC1\x80"));
199 insist(!validutf8("\xC2\x7F"));
200 U8("\xC2\x80", "0x80");
201 U8("\xDF\xBF", "0x7FF");
202 insist(!validutf8("\xDF\xC0"));
204 /* UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
205 * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
207 insist(!validutf8("\xE0\x9F\x80"));
208 U8("\xE0\xA0\x80", "0x800");
209 U8("\xE0\xBF\xBF", "0xFFF");
210 insist(!validutf8("\xE0\xC0\xBF"));
212 insist(!validutf8("\xE1\x80\x7F"));
213 U8("\xE1\x80\x80", "0x1000");
214 U8("\xEC\xBF\xBF", "0xCFFF");
215 insist(!validutf8("\xEC\xC0\xBF"));
217 U8("\xED\x80\x80", "0xD000");
218 U8("\xED\x9F\xBF", "0xD7FF");
219 insist(!validutf8("\xED\xA0\xBF"));
221 insist(!validutf8("\xEE\x7f\x80"));
222 U8("\xEE\x80\x80", "0xE000");
223 U8("\xEF\xBF\xBF", "0xFFFF");
224 insist(!validutf8("\xEF\xC0\xBF"));
226 /* UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
227 * %xF4 %x80-8F 2( UTF8-tail )
229 insist(!validutf8("\xF0\x8F\x80\x80"));
230 U8("\xF0\x90\x80\x80", "0x10000");
231 U8("\xF0\xBF\xBF\xBF", "0x3FFFF");
232 insist(!validutf8("\xF0\xC0\x80\x80"));
234 insist(!validutf8("\xF1\x80\x80\x7F"));
235 U8("\xF1\x80\x80\x80", "0x40000");
236 U8("\xF3\xBF\xBF\xBF", "0xFFFFF");
237 insist(!validutf8("\xF3\xC0\x80\x80"));
239 insist(!validutf8("\xF4\x80\x80\x7F"));
240 U8("\xF4\x80\x80\x80", "0x100000");
241 U8("\xF4\x8F\xBF\xBF", "0x10FFFF");
242 insist(!validutf8("\xF4\x90\x80\x80"));
243 insist(!validutf8("\xF4\x80\xFF\x80"));
245 /* miscellaneous non-UTF-8 rubbish */
246 insist(!validutf8("\x80"));
247 insist(!validutf8("\xBF"));
248 insist(!validutf8("\xC0"));
249 insist(!validutf8("\xC0\x7F"));
250 insist(!validutf8("\xC0\xC0"));
251 insist(!validutf8("\xE0"));
252 insist(!validutf8("\xE0\x7F"));
253 insist(!validutf8("\xE0\xC0"));
254 insist(!validutf8("\xE0\x80"));
255 insist(!validutf8("\xE0\x80\x7f"));
256 insist(!validutf8("\xE0\x80\xC0"));
257 insist(!validutf8("\xF0"));
258 insist(!validutf8("\xF0\x7F"));
259 insist(!validutf8("\xF0\xC0"));
260 insist(!validutf8("\xF0\x80"));
261 insist(!validutf8("\xF0\x80\x7f"));
262 insist(!validutf8("\xF0\x80\xC0"));
263 insist(!validutf8("\xF0\x80\x80\x7f"));
264 insist(!validutf8("\xF0\x80\x80\xC0"));
265 insist(!validutf8("\xF5\x80\x80\x80"));
266 insist(!validutf8("\xF8"));
269 static void test_mime(void) {
272 fprintf(stderr, "test_mime\n");
275 insist(!mime_content_type("text/plain", &t, &n, &v));
276 insist(!strcmp(t, "text/plain"));
281 insist(!mime_content_type("TEXT ((nested) comment) /plain", &t, &n, &v));
282 insist(!strcmp(t, "text/plain"));
287 insist(!mime_content_type(" text/plain ; Charset=utf-8", &t, &n, &v));
288 insist(!strcmp(t, "text/plain"));
289 insist(!strcmp(n, "charset"));
290 insist(!strcmp(v, "utf-8"));
293 insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t, &n, &v));
294 insist(!strcmp(t, "text/plain"));
295 insist(!strcmp(n, "charset"));
296 insist(!strcmp(v, "ISO-8859-1"));
299 /* XXX mime_multipart */
300 /* XXX mime_rfc2388_content_disposition */
302 check_string(mime_qp(""), "");
303 check_string(mime_qp("foobar"), "foobar");
304 check_string(mime_qp("foo=20bar"), "foo bar");
305 check_string(mime_qp("x \r\ny"), "x\r\ny");
306 check_string(mime_qp("x=\r\ny"), "xy");
307 check_string(mime_qp("x= \r\ny"), "xy");
308 check_string(mime_qp("x =\r\ny"), "x y");
309 check_string(mime_qp("x = \r\ny"), "x y");
312 check_string(mime_qp("Now's the time =\r\n"
313 "for all folk to come=\r\n"
314 " to the aid of their country."),
315 "Now's the time for all folk to come to the aid of their country.");
317 check_string(mime_base64(""), "");
318 check_string(mime_base64("BBBB"), "\x04\x10\x41");
319 check_string(mime_base64("////"), "\xFF\xFF\xFF");
320 check_string(mime_base64("//BB"), "\xFF\xF0\x41");
321 check_string(mime_base64("BBBB//BB////"),
322 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
323 check_string(mime_base64("B B B B / / B B / / / /"),
324 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
325 check_string(mime_base64("B\r\nBBB.// B-B//~//"),
326 "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
327 check_string(mime_base64("BBBB="),
329 check_string(mime_base64("BBBBx="), /* not actually valid base64 */
331 check_string(mime_base64("BBBB BB=="),
332 "\x04\x10\x41" "\x04");
333 check_string(mime_base64("BBBB BBB="),
334 "\x04\x10\x41" "\x04\x10");
337 static void test_hex(void) {
339 static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F };
343 fprintf(stderr, "test_hex\n");
345 for(n = 0; n <= UCHAR_MAX; ++n) {
347 insist(unhexdigitq(n) == -1);
349 insist(unhexdigitq('0') == 0);
350 insist(unhexdigitq('1') == 1);
351 insist(unhexdigitq('2') == 2);
352 insist(unhexdigitq('3') == 3);
353 insist(unhexdigitq('4') == 4);
354 insist(unhexdigitq('5') == 5);
355 insist(unhexdigitq('6') == 6);
356 insist(unhexdigitq('7') == 7);
357 insist(unhexdigitq('8') == 8);
358 insist(unhexdigitq('9') == 9);
359 insist(unhexdigitq('a') == 10);
360 insist(unhexdigitq('b') == 11);
361 insist(unhexdigitq('c') == 12);
362 insist(unhexdigitq('d') == 13);
363 insist(unhexdigitq('e') == 14);
364 insist(unhexdigitq('f') == 15);
365 insist(unhexdigitq('A') == 10);
366 insist(unhexdigitq('B') == 11);
367 insist(unhexdigitq('C') == 12);
368 insist(unhexdigitq('D') == 13);
369 insist(unhexdigitq('E') == 14);
370 insist(unhexdigitq('F') == 15);
371 check_string(hex(h, sizeof h), "00ff807f");
372 check_string(hex(0, 0), "");
373 u = unhex("00ff807f", &ul);
375 insist(memcmp(u, h, 4) == 0);
376 u = unhex("00FF807F", &ul);
378 insist(memcmp(u, h, 4) == 0);
381 fprintf(stderr, "2 ERROR reports expected {\n");
382 insist(unhex("F", 0) == 0);
383 insist(unhex("az", 0) == 0);
384 fprintf(stderr, "}\n");
387 static void test_casefold(void) {
389 const char *input, *canon_folded, *compat_folded, *canon_expected, *compat_expected;
391 fprintf(stderr, "test_casefold\n");
393 /* This isn't a very exhaustive test. Unlike for normalization, there don't
394 * seem to be any public test vectors for these algorithms. */
396 for(c = 1; c < 256; ++c) {
397 input = utf32_to_utf8(&c, 1, 0);
398 canon_folded = utf8_casefold_canon(input, strlen(input), 0);
399 compat_folded = utf8_casefold_compat(input, strlen(input), 0);
402 if((c >= 'A' && c <= 'Z')
403 || (c >= 0xC0 && c <= 0xDE && c != 0xD7))
408 case 0xB5: /* MICRO SIGN */
409 l = 0x3BC; /* GREEK SMALL LETTER MU */
411 case 0xDF: /* LATIN SMALL LETTER SHARP S */
412 insist(!strcmp(canon_folded, "ss"));
413 insist(!strcmp(compat_folded, "ss"));
419 /* Case-folded data is now normalized */
420 d = utf32_decompose_canon(&l, 1, 0);
421 canon_expected = utf32_to_utf8(d, utf32_len(d), 0);
422 if(strcmp(canon_folded, canon_expected)) {
423 fprintf(stderr, "%s:%d: canon-casefolding %#lx got '%s', expected '%s'\n",
424 __FILE__, __LINE__, (unsigned long)c,
425 format(canon_folded), format(canon_expected));
429 d = utf32_decompose_compat(&l, 1, 0);
430 compat_expected = utf32_to_utf8(d, utf32_len(d), 0);
431 if(strcmp(compat_folded, compat_expected)) {
432 fprintf(stderr, "%s:%d: compat-casefolding %#lx got '%s', expected '%s'\n",
433 __FILE__, __LINE__, (unsigned long)c,
434 format(compat_folded), format(compat_expected));
440 check_string(utf8_casefold_canon("", 0, 0), "");
445 const char *expect[10];
449 /* Only whitespace and punctuation */
456 { "wibble", { "wibble", 0 } },
457 { " wibble", { "wibble", 0 } },
458 { " wibble ", { "wibble", 0 } },
459 { "wibble ", { "wibble", 0 } },
460 { "wibble spong", { "wibble", "spong", 0 } },
461 { " wibble spong", { "wibble", "spong", 0 } },
462 { " wibble spong ", { "wibble", "spong", 0 } },
463 { "wibble spong ", { "wibble", "spong", 0 } },
464 { "wibble spong splat foo zot ", { "wibble", "spong", "splat", "foo", "zot", 0 } },
466 { "wibble 'spong", { "wibble", "spong", 0 } },
467 { " wibble's", { "wibble's", 0 } },
468 { " wibblespong' ", { "wibblespong", 0 } },
469 { "wibble sp''ong ", { "wibble", "sp", "ong", 0 } },
471 #define NWTEST (sizeof wtest / sizeof *wtest)
473 static void test_words(void) {
474 size_t t, nexpect, ngot, i;
477 fprintf(stderr, "test_words\n");
478 for(t = 0; t < NWTEST; ++t) {
479 char **got = utf8_word_split(wtest[t].in, strlen(wtest[t].in), &ngot, 0);
481 for(nexpect = 0; wtest[t].expect[nexpect]; ++nexpect)
483 if(nexpect == ngot) {
484 for(i = 0; i < ngot; ++i)
485 if(strcmp(wtest[t].expect[i], got[i]))
491 fprintf(stderr, "word split %zu failed\n", t);
492 fprintf(stderr, "input: %s\n", wtest[t].in);
493 fprintf(stderr, " | %-30s | %-30s\n",
495 for(i = 0; i < nexpect || i < ngot; ++i) {
496 const char *e = i < nexpect ? wtest[t].expect[i] : "<none>";
497 const char *g = i < ngot ? got[i] : "<none>";
498 fprintf(stderr, " %2zu | %-30s | %-30s\n", i, e, g);
506 /** @brief Less-than comparison function for integer heap */
507 static inline int int_lt(int a, int b) { return a < b; }
510 * @brief A heap with @c int elements */
511 HEAP_TYPE(iheap, int, int_lt);
512 HEAP_DEFINE(iheap, int, int_lt);
514 /** @brief Tests for @ref heap.h */
515 static void test_heap(void) {
520 fprintf(stderr, "test_heap\n");
523 for(n = 0; n < 1000; ++n)
524 iheap_insert(h, random() % 100);
525 for(n = 0; n < 1000; ++n) {
526 const int latest = iheap_remove(h);
528 fprintf(stderr, "should have %d <= %d\n", last, latest);
529 insist(last <= latest);
535 /** @brief Open a Unicode test file */
536 static FILE *open_unicode_test(const char *path) {
542 if((base = strrchr(path, '/')))
546 if(!(fp = fopen(base, "r"))) {
547 snprintf(buffer, sizeof buffer,
548 "wget http://www.unicode.org/Public/5.0.0/ucd/%s", path);
549 if((w = system(buffer)))
550 fatal(0, "%s: %s", buffer, wstat(w));
551 if(chmod(base, 0444) < 0)
552 fatal(errno, "chmod %s", base);
553 if(!(fp = fopen(base, "r")))
554 fatal(errno, "%s", base);
559 /** @brief Run breaking tests for utf32_grapheme_boundary() etc */
560 static void breaktest(const char *path,
561 int (*breakfn)(const uint32_t *, size_t, size_t)) {
562 FILE *fp = open_unicode_test(path);
566 char break_allowed[1024];
567 uint32_t buffer[1024];
569 while(!inputline(path, fp, &l, '\n')) {
571 if(l[0] == '#') continue;
575 if(*lp == ' ' || *lp == '\t') {
581 if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0xB7) {
582 /* 00F7 DIVISION SIGN */
583 break_allowed[bn] = 1;
587 if((unsigned char)*lp == 0xC3 && (unsigned char)lp[1] == 0x97) {
588 /* 00D7 MULTIPLICATION SIGN */
589 break_allowed[bn] = 0;
593 if(isxdigit((unsigned char)*lp)) {
594 buffer[bn++] = strtoul(lp, &lp, 16);
597 fatal(0, "%s:%d: evil line: %s", path, lineno, l);
599 for(n = 0; n <= bn; ++n) {
600 if(breakfn(buffer, bn, n) != break_allowed[n]) {
602 "%s:%d: offset %zu: mismatch\n"
615 /** @brief Tests for @ref lib/unicode.h */
616 static void test_unicode(void) {
620 uint32_t buffer[1024];
621 uint32_t *c[6], *NFD_c[6], *NFKD_c[6], *NFC_c[6], *NFKC_c[6]; /* 1-indexed */
624 fprintf(stderr, "test_unicode\n");
625 fp = open_unicode_test("NormalizationTest.txt");
626 while(!inputline("NormalizationTest.txt", fp, &l, '\n')) {
628 if(*l == '#' || *l == '@')
633 c[cn++] = &buffer[bn];
634 while(*lp && *lp != '#') {
643 c[cn++] = &buffer[bn];
647 buffer[bn++] = strtoul(lp, &lp, 16);
651 for(cn = 1; cn <= 5; ++cn) {
652 NFD_c[cn] = utf32_decompose_canon(c[cn], utf32_len(c[cn]), 0);
653 NFKD_c[cn] = utf32_decompose_compat(c[cn], utf32_len(c[cn]), 0);
654 NFC_c[cn] = utf32_compose_canon(c[cn], utf32_len(c[cn]), 0);
655 NFKC_c[cn] = utf32_compose_compat(c[cn], utf32_len(c[cn]), 0);
657 #define unt_check(T, A, B) do { \
659 if(utf32_cmp(c[A], T##_c[B])) { \
661 "NormalizationTest.txt:%d: c%d != "#T"(c%d)\n", \
663 fprintf(stderr, " c%d:%s\n", \
664 A, format_utf32(c[A])); \
665 fprintf(stderr, " c%d:%s\n", \
666 B, format_utf32(c[B])); \
667 fprintf(stderr, "%4s(c%d):%s\n", \
668 #T, B, format_utf32(T##_c[B])); \
672 unt_check(NFD, 3, 1);
673 unt_check(NFD, 3, 2);
674 unt_check(NFD, 3, 3);
675 unt_check(NFD, 5, 4);
676 unt_check(NFD, 5, 5);
677 unt_check(NFKD, 5, 1);
678 unt_check(NFKD, 5, 2);
679 unt_check(NFKD, 5, 3);
680 unt_check(NFKD, 5, 4);
681 unt_check(NFKD, 5, 5);
682 unt_check(NFC, 2, 1);
683 unt_check(NFC, 2, 2);
684 unt_check(NFC, 2, 3);
685 unt_check(NFC, 4, 4);
686 unt_check(NFC, 4, 5);
687 unt_check(NFKC, 4, 1);
688 unt_check(NFKC, 4, 2);
689 unt_check(NFKC, 4, 3);
690 unt_check(NFKC, 4, 4);
691 unt_check(NFKC, 4, 5);
692 for(cn = 1; cn <= 5; ++cn) {
699 breaktest("auxiliary/GraphemeBreakTest.txt", utf32_is_grapheme_boundary);
700 breaktest("auxiliary/WordBreakTest.txt", utf32_is_word_boundary);
701 insist(utf32_combining_class(0x40000) == 0);
702 insist(utf32_combining_class(0xE0000) == 0);
705 static void test_signame(void) {
706 fprintf(stderr, "test_signame\n");
707 insist(find_signal("SIGTERM") == SIGTERM);
708 insist(find_signal("SIGHUP") == SIGHUP);
709 insist(find_signal("SIGINT") == SIGINT);
710 insist(find_signal("SIGQUIT") == SIGQUIT);
711 insist(find_signal("SIGKILL") == SIGKILL);
712 insist(find_signal("SIGYOURMUM") == -1);
715 static void test_cache(void) {
716 const struct cache_type t1 = { 1 }, t2 = { 10 };
717 const char v11[] = "spong", v12[] = "wibble", v2[] = "blat";
718 fprintf(stderr, "test_cache\n");
719 cache_put(&t1, "1_1", v11);
720 cache_put(&t1, "1_2", v12);
721 cache_put(&t2, "2", v2);
722 insist(cache_count() == 3);
723 insist(cache_get(&t2, "2") == v2);
724 insist(cache_get(&t1, "1_1") == v11);
725 insist(cache_get(&t1, "1_2") == v12);
726 insist(cache_get(&t1, "2") == 0);
727 insist(cache_get(&t2, "1_1") == 0);
728 insist(cache_get(&t2, "1_2") == 0);
729 insist(cache_get(&t1, "2") == 0);
730 insist(cache_get(&t2, "1_1") == 0);
731 insist(cache_get(&t2, "1_2") == 0);
734 insist(cache_count() == 1);
735 insist(cache_get(&t1, "1_1") == 0);
736 insist(cache_get(&t1, "1_2") == 0);
737 insist(cache_get(&t2, "2") == v2);
739 insist(cache_count() == 0);
740 insist(cache_get(&t2, "2") == 0);
743 static void test_filepart(void) {
744 fprintf(stderr, "test_filepart\n");
745 check_string(d_dirname("/"), "/");
746 check_string(d_dirname("/spong"), "/");
747 check_string(d_dirname("/foo/bar"), "/foo");
748 check_string(d_dirname("./bar"), ".");
749 check_string(d_dirname("."), ".");
750 check_string(d_dirname(".."), ".");
751 check_string(d_dirname("../blat"), "..");
752 check_string(d_dirname("wibble"), ".");
753 check_string(extension("foo.c"), ".c");
754 check_string(extension(".c"), ".c");
755 check_string(extension("."), ".");
756 check_string(extension("foo"), "");
757 check_string(extension("./foo"), "");
758 check_string(extension("./foo.c"), ".c");
761 static void test_selection(void) {
763 fprintf(stderr, "test_selection\n");
764 insist((h = selection_new()) != 0);
765 selection_set(h, "one", 1);
766 selection_set(h, "two", 1);
767 selection_set(h, "three", 0);
768 selection_set(h, "four", 1);
769 insist(selection_selected(h, "one") == 1);
770 insist(selection_selected(h, "two") == 1);
771 insist(selection_selected(h, "three") == 0);
772 insist(selection_selected(h, "four") == 1);
773 insist(selection_selected(h, "five") == 0);
774 insist(hash_count(h) == 3);
775 selection_flip(h, "one");
776 selection_flip(h, "three");
777 insist(selection_selected(h, "one") == 0);
778 insist(selection_selected(h, "three") == 1);
779 insist(hash_count(h) == 3);
780 selection_live(h, "one");
781 selection_live(h, "two");
782 selection_live(h, "three");
783 selection_cleanup(h);
784 insist(selection_selected(h, "one") == 0);
785 insist(selection_selected(h, "two") == 1);
786 insist(selection_selected(h, "three") == 1);
787 insist(selection_selected(h, "four") == 0);
788 insist(selection_selected(h, "five") == 0);
789 insist(hash_count(h) == 2);
791 insist(selection_selected(h, "one") == 0);
792 insist(selection_selected(h, "two") == 0);
793 insist(selection_selected(h, "three") == 0);
794 insist(selection_selected(h, "four") == 0);
795 insist(selection_selected(h, "five") == 0);
796 insist(hash_count(h) == 0);
799 static void test_wstat(void) {
803 fprintf(stderr, "test_wstat\n");
804 if(!(pid = xfork())) {
807 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
809 check_string(wstat(w), "exited with status 1");
810 if(!(pid = xfork())) {
811 kill(getpid(), SIGTERM);
814 while(waitpid(pid, &w, 0) < 0 && errno == EINTR)
816 check_string_prefix(wstat(w), "terminated by signal 15");
819 static void test_kvp(void) {
823 fprintf(stderr, "test_kvp\n");
825 #define KVP_URLDECODE(S) kvp_urldecode((S), strlen(S))
826 insist(KVP_URLDECODE("=%zz") == 0);
827 insist(KVP_URLDECODE("=%0") == 0);
828 insist(KVP_URLDECODE("=%0z") == 0);
829 insist(KVP_URLDECODE("=%%") == 0);
830 insist(KVP_URLDECODE("==%") == 0);
831 insist(KVP_URLDECODE("wibble") == 0);
832 insist(KVP_URLDECODE("") == 0);
833 insist(KVP_URLDECODE("wibble&") == 0);
834 insist((k = KVP_URLDECODE("one=bl%61t+foo")) != 0);
835 check_string(kvp_get(k, "one"), "blat foo");
836 insist(kvp_get(k, "ONE") == 0);
837 insist(k->next == 0);
838 insist((k = KVP_URLDECODE("wibble=splat&bar=spong")) != 0);
839 check_string(kvp_get(k, "wibble"), "splat");
840 check_string(kvp_get(k, "bar"), "spong");
841 insist(kvp_get(k, "ONE") == 0);
842 insist(k->next->next == 0);
844 insist(kvp_set(&k, "bar", "spong") == 0);
845 insist(kvp_set(&k, "bar", "foo") == 1);
846 insist(kvp_set(&k, "zog", "%") == 1);
847 insist(kvp_set(&k, "wibble", 0) == 1);
848 insist(kvp_set(&k, "wibble", 0) == 0);
849 check_string(kvp_urlencode(k, 0),
851 check_string(kvp_urlencode(k, &n),
853 insist(n == strlen("bar=foo&zog=%25"));
854 check_string(urlencodestring("abc% +\n"),
859 fail_first = !!getenv("FAIL_FIRST");
860 insist('\n' == 0x0A);
861 insist('\r' == 0x0D);
875 /* configuration.c */
916 fprintf(stderr, "%d errors out of %d tests\n", errors, tests);