chiark / gitweb /
speaker protocol redesign to cope with libao re-opening
[disorder] / lib / test.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2005, 2007 Richard Kettlewell
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  */
20 /** @file lib/test.c @brief Library tests */
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>
30 #include <assert.h>
31
32 #include "utf8.h"
33 #include "mem.h"
34 #include "log.h"
35 #include "vector.h"
36 #include "charset.h"
37 #include "mime.h"
38 #include "hex.h"
39 #include "words.h"
40 #include "heap.h"
41
42 static int tests, errors;
43
44 /** @brief Checks that @p expr is nonzero */
45 #define insist(expr) do {                               \
46   if(!(expr)) {                                         \
47     ++errors;                                           \
48     fprintf(stderr, "%s:%d: error checking %s\n",       \
49             __FILE__, __LINE__, #expr);                 \
50   }                                                     \
51   ++tests;                                              \
52 } while(0)
53
54 static const char *format(const char *s) {
55   struct dynstr d;
56   int c;
57   char buf[10];
58   
59   dynstr_init(&d);
60   while((c = (unsigned char)*s++)) {
61     if(c >= ' ' && c <= '~')
62       dynstr_append(&d, c);
63     else {
64       sprintf(buf, "\\x%02X", (unsigned)c);
65       dynstr_append_string(&d, buf);
66     }
67   }
68   dynstr_terminate(&d);
69   return d.vec;
70 }
71
72 #define check_string(GOT, WANT) do {                            \
73   const char *g = GOT;                                          \
74   const char *w = WANT;                                         \
75                                                                 \
76   if(w == 0) {                                                  \
77     fprintf(stderr, "%s:%d: %s returned 0\n",                   \
78             __FILE__, __LINE__, #GOT);                          \
79     ++errors;                                                   \
80   } else if(strcmp(w, g)) {                                     \
81     fprintf(stderr, "%s:%d: %s returned:\n%s\nexpected:\n%s\n", \
82             __FILE__, __LINE__, #GOT, format(g), format(w));    \
83     ++errors;                                                   \
84   }                                                             \
85   ++tests;                                                      \
86  } while(0)
87
88 static uint32_t *ucs4parse(const char *s) {
89   struct dynstr_ucs4 d;
90   char *e;
91
92   dynstr_ucs4_init(&d);
93   while(*s) {
94     errno = 0;
95     dynstr_ucs4_append(&d, strtoul(s, &e, 0));
96     if(errno) fatal(errno, "strtoul (%s)", s);
97     s = e;
98   }
99   dynstr_ucs4_terminate(&d);
100   return d.vec;
101 }
102
103 static void test_utf8(void) {
104   /* Test validutf8, convert to UCS-4, check the answer is right,
105    * convert back to UTF-8, check we got to where we started */
106 #define U8(CHARS, WORDS) do {                   \
107   uint32_t *w = ucs4parse(WORDS);               \
108   uint32_t *ucs;                                \
109   char *u8;                                     \
110                                                 \
111   insist(validutf8(CHARS));                     \
112   ucs = utf82ucs4(CHARS);                       \
113   insist(ucs != 0);                             \
114   insist(!ucs4cmp(w, ucs));                     \
115   u8 = ucs42utf8(ucs);                          \
116   insist(u8 != 0);                              \
117   insist(!strcmp(u8, CHARS));                   \
118 } while(0)
119
120   fprintf(stderr, "test_utf8\n");
121
122   /* empty string */
123
124   U8("", "");
125   
126   /* ASCII characters */
127
128   U8(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
129      "0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d "
130      "0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a "
131      "0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 "
132      "0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 "
133      "0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 "
134      "0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e "
135      "0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b "
136      "0x7c 0x7d 0x7e");
137   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",
138      "0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 "
139      "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d "
140      "0x1e 0x1f 0x7f");
141
142   /* from RFC3629 */
143
144   /* UTF8-2      = %xC2-DF UTF8-tail */
145   insist(!validutf8("\xC0\x80"));
146   insist(!validutf8("\xC1\x80"));
147   insist(!validutf8("\xC2\x7F"));
148   U8("\xC2\x80", "0x80");
149   U8("\xDF\xBF", "0x7FF");
150   insist(!validutf8("\xDF\xC0"));
151
152   /*  UTF8-3      = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
153    *                %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
154    */
155   insist(!validutf8("\xE0\x9F\x80"));
156   U8("\xE0\xA0\x80", "0x800");
157   U8("\xE0\xBF\xBF", "0xFFF");
158   insist(!validutf8("\xE0\xC0\xBF"));
159
160   insist(!validutf8("\xE1\x80\x7F"));
161   U8("\xE1\x80\x80", "0x1000");
162   U8("\xEC\xBF\xBF", "0xCFFF");
163   insist(!validutf8("\xEC\xC0\xBF"));
164   
165   U8("\xED\x80\x80", "0xD000");
166   U8("\xED\x9F\xBF", "0xD7FF");
167   insist(!validutf8("\xED\xA0\xBF"));
168
169   insist(!validutf8("\xEE\x7f\x80"));
170   U8("\xEE\x80\x80", "0xE000");
171   U8("\xEF\xBF\xBF", "0xFFFF");
172   insist(!validutf8("\xEF\xC0\xBF"));
173
174   /*  UTF8-4      = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
175    *                %xF4 %x80-8F 2( UTF8-tail )
176    */
177   insist(!validutf8("\xF0\x8F\x80\x80"));
178   U8("\xF0\x90\x80\x80", "0x10000");
179   U8("\xF0\xBF\xBF\xBF", "0x3FFFF");
180   insist(!validutf8("\xF0\xC0\x80\x80"));
181
182   insist(!validutf8("\xF1\x80\x80\x7F"));
183   U8("\xF1\x80\x80\x80", "0x40000");
184   U8("\xF3\xBF\xBF\xBF", "0xFFFFF");
185   insist(!validutf8("\xF3\xC0\x80\x80"));
186
187   insist(!validutf8("\xF4\x80\x80\x7F"));
188   U8("\xF4\x80\x80\x80", "0x100000");
189   U8("\xF4\x8F\xBF\xBF", "0x10FFFF");
190   insist(!validutf8("\xF4\x90\x80\x80"));
191
192   /* miscellaneous non-UTF-8 rubbish */
193   insist(!validutf8("\x80"));
194   insist(!validutf8("\xBF"));
195   insist(!validutf8("\xC0"));
196   insist(!validutf8("\xC0\x7F"));
197   insist(!validutf8("\xC0\xC0"));
198   insist(!validutf8("\xE0"));
199   insist(!validutf8("\xE0\x7F"));
200   insist(!validutf8("\xE0\xC0"));
201   insist(!validutf8("\xE0\x80"));
202   insist(!validutf8("\xE0\x80\x7f"));
203   insist(!validutf8("\xE0\x80\xC0"));
204   insist(!validutf8("\xF0"));
205   insist(!validutf8("\xF0\x7F"));
206   insist(!validutf8("\xF0\xC0"));
207   insist(!validutf8("\xF0\x80"));
208   insist(!validutf8("\xF0\x80\x7f"));
209   insist(!validutf8("\xF0\x80\xC0"));
210   insist(!validutf8("\xF0\x80\x80\x7f"));
211   insist(!validutf8("\xF0\x80\x80\xC0"));
212   insist(!validutf8("\xF5\x80\x80\x80"));
213   insist(!validutf8("\xF8"));
214 }
215
216 static void test_mime(void) {
217   char *t, *n, *v;
218
219   fprintf(stderr, "test_mime\n");
220
221   t = n = v = 0;
222   insist(!mime_content_type("text/plain", &t, &n, &v));
223   insist(!strcmp(t, "text/plain"));
224   insist(n == 0);
225   insist(v == 0);
226
227   t = n = v = 0;
228   insist(!mime_content_type("TEXT ((nested) comment) /plain", &t, &n, &v));
229   insist(!strcmp(t, "text/plain"));
230   insist(n == 0);
231   insist(v == 0);
232
233   t = n = v = 0;
234   insist(!mime_content_type(" text/plain ; Charset=utf-8", &t, &n, &v));
235   insist(!strcmp(t, "text/plain"));
236   insist(!strcmp(n, "charset"));
237   insist(!strcmp(v, "utf-8"));
238
239   t = n = v = 0;
240   insist(!mime_content_type("text/plain;charset = ISO-8859-1 ", &t, &n, &v));
241   insist(!strcmp(t, "text/plain"));
242   insist(!strcmp(n, "charset"));
243   insist(!strcmp(v, "ISO-8859-1"));
244
245   /* XXX mime_parse */
246   /* XXX mime_multipart */
247   /* XXX mime_rfc2388_content_disposition */
248
249   check_string(mime_qp(""), "");
250   check_string(mime_qp("foobar"), "foobar");
251   check_string(mime_qp("foo=20bar"), "foo bar");
252   check_string(mime_qp("x \r\ny"), "x\r\ny");
253   check_string(mime_qp("x=\r\ny"), "xy");
254   check_string(mime_qp("x= \r\ny"), "xy");
255   check_string(mime_qp("x =\r\ny"), "x y");
256   check_string(mime_qp("x = \r\ny"), "x y");
257
258   /* from RFC2045 */
259   check_string(mime_qp("Now's the time =\r\n"
260 "for all folk to come=\r\n"
261 " to the aid of their country."),
262                "Now's the time for all folk to come to the aid of their country.");
263
264   check_string(mime_base64(""),  "");
265   check_string(mime_base64("BBBB"), "\x04\x10\x41");
266   check_string(mime_base64("////"), "\xFF\xFF\xFF");
267   check_string(mime_base64("//BB"), "\xFF\xF0\x41");
268   check_string(mime_base64("BBBB//BB////"),
269                "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
270   check_string(mime_base64("B B B B  / / B B / / / /"),
271                "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
272   check_string(mime_base64("B\r\nBBB.// B-B//~//"),
273                "\x04\x10\x41" "\xFF\xF0\x41" "\xFF\xFF\xFF");
274   check_string(mime_base64("BBBB="),
275                "\x04\x10\x41");
276   check_string(mime_base64("BBBBx="),   /* not actually valid base64 */
277                "\x04\x10\x41");
278   check_string(mime_base64("BBBB BB=="),
279                "\x04\x10\x41" "\x04");
280   check_string(mime_base64("BBBB BBB="),
281                "\x04\x10\x41" "\x04\x10");
282 }
283
284 static void test_hex(void) {
285   unsigned n;
286   static const unsigned char h[] = { 0x00, 0xFF, 0x80, 0x7F };
287   uint8_t *u;
288   size_t ul;
289
290   fprintf(stderr, "test_hex\n");
291
292   for(n = 0; n <= UCHAR_MAX; ++n) {
293     if(!isxdigit(n))
294       insist(unhexdigitq(n) == -1);
295   }
296   insist(unhexdigitq('0') == 0);
297   insist(unhexdigitq('1') == 1);
298   insist(unhexdigitq('2') == 2);
299   insist(unhexdigitq('3') == 3);
300   insist(unhexdigitq('4') == 4);
301   insist(unhexdigitq('5') == 5);
302   insist(unhexdigitq('6') == 6);
303   insist(unhexdigitq('7') == 7);
304   insist(unhexdigitq('8') == 8);
305   insist(unhexdigitq('9') == 9);
306   insist(unhexdigitq('a') == 10);
307   insist(unhexdigitq('b') == 11);
308   insist(unhexdigitq('c') == 12);
309   insist(unhexdigitq('d') == 13);
310   insist(unhexdigitq('e') == 14);
311   insist(unhexdigitq('f') == 15);
312   insist(unhexdigitq('A') == 10);
313   insist(unhexdigitq('B') == 11);
314   insist(unhexdigitq('C') == 12);
315   insist(unhexdigitq('D') == 13);
316   insist(unhexdigitq('E') == 14);
317   insist(unhexdigitq('F') == 15);
318   check_string(hex(h, sizeof h), "00ff807f");
319   check_string(hex(0, 0), "");
320   u = unhex("00ff807f", &ul);
321   insist(ul == 4);
322   insist(memcmp(u, h, 4) == 0);
323   u = unhex("00FF807F", &ul);
324   insist(ul == 4);
325   insist(memcmp(u, h, 4) == 0);
326   u = unhex("", &ul);
327   insist(ul == 0);
328   fprintf(stderr, "2 ERROR reports expected {\n");
329   insist(unhex("F", 0) == 0);
330   insist(unhex("az", 0) == 0);
331   fprintf(stderr, "}\n");
332 }
333
334 static void test_casefold(void) {
335   uint32_t c, l, u[2];
336   const char *s, *ls;
337
338   fprintf(stderr, "test_casefold\n");
339
340   for(c = 1; c < 256; ++c) {
341     u[0] = c;
342     u[1] = 0;
343     s = ucs42utf8(u);
344     ls = casefold(s);
345     switch(c) {
346     default:
347       if((c >= 'A' && c <= 'Z')
348          || (c >= 0xC0 && c <= 0xDE && c != 0xD7))
349         l = c ^ 0x20;
350       else
351         l = c;
352       break;
353     case 0xB5:                          /* MICRO SIGN */
354       l = 0x3BC;                        /* GREEK SMALL LETTER MU */
355       break;
356     case 0xDF:                          /* LATIN SMALL LETTER SHARP S */
357       insist(!strcmp(ls, "ss"));
358       l = 0;
359       break;
360     }
361     if(l) {
362       u[0] = l;
363       u[1] = 0;
364       s = ucs42utf8(u);
365       if(strcmp(s, ls)) {
366         fprintf(stderr, "%s:%d: casefolding %#lx got '%s', expected '%s'\n",
367                 __FILE__, __LINE__, (unsigned long)c,
368                 format(ls), format(s));
369         ++errors;
370       }
371       ++tests;
372     }
373   }
374   check_string(casefold(""), "");
375 }
376
377 /** @brief Less-than comparison function for integer heap */
378 static inline int int_lt(int a, int b) { return a < b; }
379
380 /** @struct iheap
381  * @brief A heap with @c int elements */
382 HEAP_TYPE(iheap, int, int_lt);
383
384 /** @brief Tests for @ref heap.h */
385 static void test_heap(void) {
386   struct iheap h[1];
387   int n;
388   int last = -1;
389
390   fprintf(stderr, "test_heap\n");
391
392   iheap_init(h);
393   for(n = 0; n < 1000; ++n)
394     iheap_insert(h, random() % 100);
395   for(n = 0; n < 1000; ++n) {
396     const int latest = iheap_remove(h);
397     if(last > latest)
398       fprintf(stderr, "should have %d <= %d\n", last, latest);
399     insist(last <= latest);
400     last = latest;
401   }
402   putchar('\n');
403 }
404
405 int main(void) {
406   insist('\n' == 0x0A);
407   insist('\r' == 0x0D);
408   insist(' ' == 0x20);
409   insist('0' == 0x30);
410   insist('9' == 0x39);
411   insist('A' == 0x41);
412   insist('Z' == 0x5A);
413   insist('a' == 0x61);
414   insist('z' == 0x7A);
415   /* addr.c */
416   /* asprintf.c */
417   /* authhash.c */
418   /* basen.c */
419   /* charset.c */
420   /* client.c */
421   /* configuration.c */
422   /* event.c */
423   /* fprintf.c */
424   /* heap.c */
425   test_heap();
426   /* hex.c */
427   test_hex();
428   /* inputline.c */
429   /* kvp.c */
430   /* log.c */
431   /* mem.c */
432   /* mime.c */
433   test_mime();
434   /* mixer.c */
435   /* plugin.c */
436   /* printf.c */
437   /* queue.c */
438   /* sink.c */
439   /* snprintf.c */
440   /* split.c */
441   /* syscalls.c */
442   /* table.c */
443   /* utf8.c */
444   test_utf8();
445   /* vector.c */
446   /* words.c */
447   test_casefold();
448   /* XXX words() */
449   /* wstat.c */
450   fprintf(stderr,  "%d errors out of %d tests\n", errors, tests);
451   return !!errors;
452 }
453   
454 /*
455 Local Variables:
456 c-basic-offset:2
457 comment-column:40
458 End:
459 */
460