chiark / gitweb /
General-purpose event distribution interface
[disorder] / libtests / t-utf8.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2005, 2007, 2008 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 #include "test.h"
21
22 static void test_utf8(void) {
23   /* Test validutf8, convert to UCS-4, check the answer is right,
24    * convert back to UTF-8, check we got to where we started */
25 #define U8(CHARS, WORDS) do {                   \
26   uint32_t *w = ucs4parse(WORDS);               \
27   uint32_t *ucs;                                \
28   char *u8;                                     \
29                                                 \
30   insist(validutf8(CHARS));                     \
31   ucs = utf8_to_utf32(CHARS, strlen(CHARS), 0); \
32   insist(ucs != 0);                             \
33   insist(!utf32_cmp(w, ucs));                   \
34   u8 = utf32_to_utf8(ucs, utf32_len(ucs), 0);   \
35   insist(u8 != 0);                              \
36   check_string(u8, CHARS);                      \
37 } while(0)
38
39 #define validutf8(S) utf8_valid((S), strlen(S))
40
41   /* empty string */
42
43   U8("", "");
44   
45   /* ASCII characters */
46
47   U8(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
48      "0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d "
49      "0x2e 0x2f 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a "
50      "0x3b 0x3c 0x3d 0x3e 0x3f 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 "
51      "0x48 0x49 0x4a 0x4b 0x4c 0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 "
52      "0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 "
53      "0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e "
54      "0x6f 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a 0x7b "
55      "0x7c 0x7d 0x7e");
56   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",
57      "0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 "
58      "0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d "
59      "0x1e 0x1f 0x7f");
60
61   /* from RFC3629 */
62
63   /* UTF8-2      = %xC2-DF UTF8-tail */
64   insist(!validutf8("\xC0\x80"));
65   insist(!validutf8("\xC1\x80"));
66   insist(!validutf8("\xC2\x7F"));
67   U8("\xC2\x80", "0x80");
68   U8("\xDF\xBF", "0x7FF");
69   insist(!validutf8("\xDF\xC0"));
70
71   /*  UTF8-3      = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
72    *                %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
73    */
74   insist(!validutf8("\xE0\x9F\x80"));
75   U8("\xE0\xA0\x80", "0x800");
76   U8("\xE0\xBF\xBF", "0xFFF");
77   insist(!validutf8("\xE0\xC0\xBF"));
78
79   insist(!validutf8("\xE1\x80\x7F"));
80   U8("\xE1\x80\x80", "0x1000");
81   U8("\xEC\xBF\xBF", "0xCFFF");
82   insist(!validutf8("\xEC\xC0\xBF"));
83   
84   U8("\xED\x80\x80", "0xD000");
85   U8("\xED\x9F\xBF", "0xD7FF");
86   insist(!validutf8("\xED\xA0\xBF"));
87
88   insist(!validutf8("\xEE\x7f\x80"));
89   U8("\xEE\x80\x80", "0xE000");
90   U8("\xEF\xBF\xBF", "0xFFFF");
91   insist(!validutf8("\xEF\xC0\xBF"));
92
93   /*  UTF8-4      = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
94    *                %xF4 %x80-8F 2( UTF8-tail )
95    */
96   insist(!validutf8("\xF0\x8F\x80\x80"));
97   U8("\xF0\x90\x80\x80", "0x10000");
98   U8("\xF0\xBF\xBF\xBF", "0x3FFFF");
99   insist(!validutf8("\xF0\xC0\x80\x80"));
100
101   insist(!validutf8("\xF1\x80\x80\x7F"));
102   U8("\xF1\x80\x80\x80", "0x40000");
103   U8("\xF3\xBF\xBF\xBF", "0xFFFFF");
104   insist(!validutf8("\xF3\xC0\x80\x80"));
105
106   insist(!validutf8("\xF4\x80\x80\x7F"));
107   U8("\xF4\x80\x80\x80", "0x100000");
108   U8("\xF4\x8F\xBF\xBF", "0x10FFFF");
109   insist(!validutf8("\xF4\x90\x80\x80"));
110   insist(!validutf8("\xF4\x80\xFF\x80"));
111
112   /* miscellaneous non-UTF-8 rubbish */
113   insist(!validutf8("\x80"));
114   insist(!validutf8("\xBF"));
115   insist(!validutf8("\xC0"));
116   insist(!validutf8("\xC0\x7F"));
117   insist(!validutf8("\xC0\xC0"));
118   insist(!validutf8("\xE0"));
119   insist(!validutf8("\xE0\x7F"));
120   insist(!validutf8("\xE0\xC0"));
121   insist(!validutf8("\xE0\x80"));
122   insist(!validutf8("\xE0\x80\x7f"));
123   insist(!validutf8("\xE0\x80\xC0"));
124   insist(!validutf8("\xF0"));
125   insist(!validutf8("\xF0\x7F"));
126   insist(!validutf8("\xF0\xC0"));
127   insist(!validutf8("\xF0\x80"));
128   insist(!validutf8("\xF0\x80\x7f"));
129   insist(!validutf8("\xF0\x80\xC0"));
130   insist(!validutf8("\xF0\x80\x80\x7f"));
131   insist(!validutf8("\xF0\x80\x80\xC0"));
132   insist(!validutf8("\xF5\x80\x80\x80"));
133   insist(!validutf8("\xF8"));
134 }
135
136 TEST(utf8);
137
138 /*
139 Local Variables:
140 c-basic-offset:2
141 comment-column:40
142 fill-column:79
143 indent-tabs-mode:nil
144 End:
145 */