Tizen 2.1 base
[platform/upstream/glib2.0.git] / glib / tests / utf8-validate.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 2001 Matthias Clasen <matthiasc@poet.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "glib.h"
21
22 #define UNICODE_VALID(Char)                   \
23     ((Char) < 0x110000 &&                     \
24      (((Char) & 0xFFFFF800) != 0xD800) &&     \
25      ((Char) < 0xFDD0 || (Char) > 0xFDEF) &&  \
26      ((Char) & 0xFFFE) != 0xFFFE)
27
28
29 typedef struct {
30   const gchar *text;
31   gint max_len;
32   gint offset;
33   gboolean valid;
34 } Test;
35
36 Test test[] = {
37   /* some tests to check max_len handling */
38   /* length 1 */
39   { "abcde", -1, 5, TRUE },
40   { "abcde", 3, 3, TRUE },
41   { "abcde", 5, 5, TRUE },
42   { "abcde", 7, 5, FALSE },
43   /* length 2 */
44   { "\xc2\xa9\xc2\xa9\xc2\xa9", -1, 6, TRUE },
45   { "\xc2\xa9\xc2\xa9\xc2\xa9",  1, 0, FALSE },
46   { "\xc2\xa9\xc2\xa9\xc2\xa9",  2, 2, TRUE },
47   { "\xc2\xa9\xc2\xa9\xc2\xa9",  3, 2, FALSE },
48   { "\xc2\xa9\xc2\xa9\xc2\xa9",  4, 4, TRUE },
49   { "\xc2\xa9\xc2\xa9\xc2\xa9",  5, 4, FALSE },
50   { "\xc2\xa9\xc2\xa9\xc2\xa9",  6, 6, TRUE },
51   { "\xc2\xa9\xc2\xa9\xc2\xa9",  7, 6, FALSE },
52   /* length 3 */
53   { "\xe2\x89\xa0\xe2\x89\xa0", -1, 6, TRUE },
54   { "\xe2\x89\xa0\xe2\x89\xa0",  1, 0, FALSE },
55   { "\xe2\x89\xa0\xe2\x89\xa0",  2, 0, FALSE },
56   { "\xe2\x89\xa0\xe2\x89\xa0",  3, 3, TRUE },
57   { "\xe2\x89\xa0\xe2\x89\xa0",  4, 3, FALSE },
58   { "\xe2\x89\xa0\xe2\x89\xa0",  5, 3, FALSE },
59   { "\xe2\x89\xa0\xe2\x89\xa0",  6, 6, TRUE },
60   { "\xe2\x89\xa0\xe2\x89\xa0",  7, 6, FALSE },
61
62   /* examples from http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt */
63   /* greek 'kosme' */
64   { "\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5", -1, 11, TRUE },
65   /* first sequence of each length */
66   { "\x00", -1, 0, TRUE },
67   { "\xc2\x80", -1, 2, TRUE },
68   { "\xe0\xa0\x80", -1, 3, TRUE },
69   { "\xf0\x90\x80\x80", -1, 4, TRUE },
70   { "\xf8\x88\x80\x80\x80", -1, 0, FALSE },
71   { "\xfc\x84\x80\x80\x80\x80", -1, 0, FALSE },
72   /* last sequence of each length */
73   { "\x7f", -1, 1, TRUE },
74   { "\xdf\xbf", -1, 2, TRUE },
75   { "\xef\xbf\xbf", -1, 0, FALSE },
76   { "\xf7\xbf\xbf\xbf", -1, 0, FALSE },
77   { "\xfb\xbf\xbf\xbf\xbf", -1, 0, FALSE },
78   { "\xfd\xbf\xbf\xbf\xbf\xbf", -1, 0, FALSE },
79   /* other boundary conditions */
80   { "\xed\x9f\xbf", -1, 3, TRUE },
81   { "\xee\x80\x80", -1, 3, TRUE },
82   { "\xef\xbf\xbd", -1, 3, TRUE },
83   { "\xf4\x8f\xbf\xbf", -1, 0, FALSE },
84   { "\xf4\x90\x80\x80", -1, 0, FALSE },
85   /* malformed sequences */
86   /* continuation bytes */
87   { "\x80", -1, 0, FALSE },
88   { "\xbf", -1, 0, FALSE },
89   { "\x80\xbf", -1, 0, FALSE },
90   { "\x80\xbf\x80", -1, 0, FALSE },
91   { "\x80\xbf\x80\xbf", -1, 0, FALSE },
92   { "\x80\xbf\x80\xbf\x80", -1, 0, FALSE },
93   { "\x80\xbf\x80\xbf\x80\xbf", -1, 0, FALSE },
94   { "\x80\xbf\x80\xbf\x80\xbf\x80", -1, 0, FALSE },
95
96   /* all possible continuation byte */
97   { "\x80", -1, 0, FALSE },
98   { "\x81", -1, 0, FALSE },
99   { "\x82", -1, 0, FALSE },
100   { "\x83", -1, 0, FALSE },
101   { "\x84", -1, 0, FALSE },
102   { "\x85", -1, 0, FALSE },
103   { "\x86", -1, 0, FALSE },
104   { "\x87", -1, 0, FALSE },
105   { "\x88", -1, 0, FALSE },
106   { "\x89", -1, 0, FALSE },
107   { "\x8a", -1, 0, FALSE },
108   { "\x8b", -1, 0, FALSE },
109   { "\x8c", -1, 0, FALSE },
110   { "\x8d", -1, 0, FALSE },
111   { "\x8e", -1, 0, FALSE },
112   { "\x8f", -1, 0, FALSE },
113   { "\x90", -1, 0, FALSE },
114   { "\x91", -1, 0, FALSE },
115   { "\x92", -1, 0, FALSE },
116   { "\x93", -1, 0, FALSE },
117   { "\x94", -1, 0, FALSE },
118   { "\x95", -1, 0, FALSE },
119   { "\x96", -1, 0, FALSE },
120   { "\x97", -1, 0, FALSE },
121   { "\x98", -1, 0, FALSE },
122   { "\x99", -1, 0, FALSE },
123   { "\x9a", -1, 0, FALSE },
124   { "\x9b", -1, 0, FALSE },
125   { "\x9c", -1, 0, FALSE },
126   { "\x9d", -1, 0, FALSE },
127   { "\x9e", -1, 0, FALSE },
128   { "\x9f", -1, 0, FALSE },
129   { "\xa0", -1, 0, FALSE },
130   { "\xa1", -1, 0, FALSE },
131   { "\xa2", -1, 0, FALSE },
132   { "\xa3", -1, 0, FALSE },
133   { "\xa4", -1, 0, FALSE },
134   { "\xa5", -1, 0, FALSE },
135   { "\xa6", -1, 0, FALSE },
136   { "\xa7", -1, 0, FALSE },
137   { "\xa8", -1, 0, FALSE },
138   { "\xa9", -1, 0, FALSE },
139   { "\xaa", -1, 0, FALSE },
140   { "\xab", -1, 0, FALSE },
141   { "\xac", -1, 0, FALSE },
142   { "\xad", -1, 0, FALSE },
143   { "\xae", -1, 0, FALSE },
144   { "\xaf", -1, 0, FALSE },
145   { "\xb0", -1, 0, FALSE },
146   { "\xb1", -1, 0, FALSE },
147   { "\xb2", -1, 0, FALSE },
148   { "\xb3", -1, 0, FALSE },
149   { "\xb4", -1, 0, FALSE },
150   { "\xb5", -1, 0, FALSE },
151   { "\xb6", -1, 0, FALSE },
152   { "\xb7", -1, 0, FALSE },
153   { "\xb8", -1, 0, FALSE },
154   { "\xb9", -1, 0, FALSE },
155   { "\xba", -1, 0, FALSE },
156   { "\xbb", -1, 0, FALSE },
157   { "\xbc", -1, 0, FALSE },
158   { "\xbd", -1, 0, FALSE },
159   { "\xbe", -1, 0, FALSE },
160   { "\xbf", -1, 0, FALSE },
161   /* lone start characters */
162   { "\xc0\x20", -1, 0, FALSE },
163   { "\xc1\x20", -1, 0, FALSE },
164   { "\xc2\x20", -1, 0, FALSE },
165   { "\xc3\x20", -1, 0, FALSE },
166   { "\xc4\x20", -1, 0, FALSE },
167   { "\xc5\x20", -1, 0, FALSE },
168   { "\xc6\x20", -1, 0, FALSE },
169   { "\xc7\x20", -1, 0, FALSE },
170   { "\xc8\x20", -1, 0, FALSE },
171   { "\xc9\x20", -1, 0, FALSE },
172   { "\xca\x20", -1, 0, FALSE },
173   { "\xcb\x20", -1, 0, FALSE },
174   { "\xcc\x20", -1, 0, FALSE },
175   { "\xcd\x20", -1, 0, FALSE },
176   { "\xce\x20", -1, 0, FALSE },
177   { "\xcf\x20", -1, 0, FALSE },
178   { "\xd0\x20", -1, 0, FALSE },
179   { "\xd1\x20", -1, 0, FALSE },
180   { "\xd2\x20", -1, 0, FALSE },
181   { "\xd3\x20", -1, 0, FALSE },
182   { "\xd4\x20", -1, 0, FALSE },
183   { "\xd5\x20", -1, 0, FALSE },
184   { "\xd6\x20", -1, 0, FALSE },
185   { "\xd7\x20", -1, 0, FALSE },
186   { "\xd8\x20", -1, 0, FALSE },
187   { "\xd9\x20", -1, 0, FALSE },
188   { "\xda\x20", -1, 0, FALSE },
189   { "\xdb\x20", -1, 0, FALSE },
190   { "\xdc\x20", -1, 0, FALSE },
191   { "\xdd\x20", -1, 0, FALSE },
192   { "\xde\x20", -1, 0, FALSE },
193   { "\xdf\x20", -1, 0, FALSE },
194   { "\xe0\x20", -1, 0, FALSE },
195   { "\xe1\x20", -1, 0, FALSE },
196   { "\xe2\x20", -1, 0, FALSE },
197   { "\xe3\x20", -1, 0, FALSE },
198   { "\xe4\x20", -1, 0, FALSE },
199   { "\xe5\x20", -1, 0, FALSE },
200   { "\xe6\x20", -1, 0, FALSE },
201   { "\xe7\x20", -1, 0, FALSE },
202   { "\xe8\x20", -1, 0, FALSE },
203   { "\xe9\x20", -1, 0, FALSE },
204   { "\xea\x20", -1, 0, FALSE },
205   { "\xeb\x20", -1, 0, FALSE },
206   { "\xec\x20", -1, 0, FALSE },
207   { "\xed\x20", -1, 0, FALSE },
208   { "\xee\x20", -1, 0, FALSE },
209   { "\xef\x20", -1, 0, FALSE },
210   { "\xf0\x20", -1, 0, FALSE },
211   { "\xf1\x20", -1, 0, FALSE },
212   { "\xf2\x20", -1, 0, FALSE },
213   { "\xf3\x20", -1, 0, FALSE },
214   { "\xf4\x20", -1, 0, FALSE },
215   { "\xf5\x20", -1, 0, FALSE },
216   { "\xf6\x20", -1, 0, FALSE },
217   { "\xf7\x20", -1, 0, FALSE },
218   { "\xf8\x20", -1, 0, FALSE },
219   { "\xf9\x20", -1, 0, FALSE },
220   { "\xfa\x20", -1, 0, FALSE },
221   { "\xfb\x20", -1, 0, FALSE },
222   { "\xfc\x20", -1, 0, FALSE },
223   { "\xfd\x20", -1, 0, FALSE },
224   /* missing continuation bytes */
225   { "\x20\xc0", -1, 1, FALSE },
226   { "\x20\xe0\x80", -1, 1, FALSE },
227   { "\x20\xf0\x80\x80", -1, 1, FALSE },
228   { "\x20\xf8\x80\x80\x80", -1, 1, FALSE },
229   { "\x20\xfc\x80\x80\x80\x80", -1, 1, FALSE },
230   { "\x20\xdf", -1, 1, FALSE },
231   { "\x20\xef\xbf", -1, 1, FALSE },
232   { "\x20\xf7\xbf\xbf", -1, 1, FALSE },
233   { "\x20\xfb\xbf\xbf\xbf", -1, 1, FALSE },
234   { "\x20\xfd\xbf\xbf\xbf\xbf", -1, 1, FALSE },
235   /* impossible bytes */
236   { "\x20\xfe\x20", -1, 1, FALSE },
237   { "\x20\xff\x20", -1, 1, FALSE },
238   /* overlong sequences */
239   { "\x20\xc0\xaf\x20", -1, 1, FALSE },
240   { "\x20\xe0\x80\xaf\x20", -1, 1, FALSE },
241   { "\x20\xf0\x80\x80\xaf\x20", -1, 1, FALSE },
242   { "\x20\xf8\x80\x80\x80\xaf\x20", -1, 1, FALSE },
243   { "\x20\xfc\x80\x80\x80\x80\xaf\x20", -1, 1, FALSE },
244   { "\x20\xc1\xbf\x20", -1, 1, FALSE },
245   { "\x20\xe0\x9f\xbf\x20", -1, 1, FALSE },
246   { "\x20\xf0\x8f\xbf\xbf\x20", -1, 1, FALSE },
247   { "\x20\xf8\x87\xbf\xbf\xbf\x20", -1, 1, FALSE },
248   { "\x20\xfc\x83\xbf\xbf\xbf\xbf\x20", -1, 1, FALSE },
249   { "\x20\xc0\x80\x20", -1, 1, FALSE },
250   { "\x20\xe0\x80\x80\x20", -1, 1, FALSE },
251   { "\x20\xf0\x80\x80\x80\x20", -1, 1, FALSE },
252   { "\x20\xf8\x80\x80\x80\x80\x20", -1, 1, FALSE },
253   { "\x20\xfc\x80\x80\x80\x80\x80\x20", -1, 1, FALSE },
254   /* illegal code positions */
255   { "\x20\xed\xa0\x80\x20", -1, 1, FALSE },
256   { "\x20\xed\xad\xbf\x20", -1, 1, FALSE },
257   { "\x20\xed\xae\x80\x20", -1, 1, FALSE },
258   { "\x20\xed\xaf\xbf\x20", -1, 1, FALSE },
259   { "\x20\xed\xb0\x80\x20", -1, 1, FALSE },
260   { "\x20\xed\xbe\x80\x20", -1, 1, FALSE },
261   { "\x20\xed\xbf\xbf\x20", -1, 1, FALSE },
262   { "\x20\xed\xa0\x80\xed\xb0\x80\x20", -1, 1, FALSE },
263   { "\x20\xed\xa0\x80\xed\xbf\xbf\x20", -1, 1, FALSE },
264   { "\x20\xed\xad\xbf\xed\xb0\x80\x20", -1, 1, FALSE },
265   { "\x20\xed\xad\xbf\xed\xbf\xbf\x20", -1, 1, FALSE },
266   { "\x20\xed\xae\x80\xed\xb0\x80\x20", -1, 1, FALSE },
267   { "\x20\xed\xae\x80\xed\xbf\xbf\x20", -1, 1, FALSE },
268   { "\x20\xed\xaf\xbf\xed\xb0\x80\x20", -1, 1, FALSE },
269   { "\x20\xed\xaf\xbf\xed\xbf\xbf\x20", -1, 1, FALSE },
270   { "\x20\xef\xbf\xbe\x20", -1, 1, FALSE },
271   { "\x20\xef\xbf\xbf\x20", -1, 1, FALSE },
272
273   { NULL, }
274 };
275
276 static void
277 do_test (gconstpointer d)
278 {
279   const Test *test = d;
280   const gchar *end;
281   gboolean result;
282
283   result = g_utf8_validate (test->text, test->max_len, &end);
284
285   g_assert (result == test->valid);
286   g_assert (end - test->text == test->offset);
287 }
288
289 int
290 main (int argc, char *argv[])
291 {
292   gint i;
293   gchar *path;
294
295   g_test_init (&argc, &argv, NULL);
296
297   for (i = 0; test[i].text; i++)
298     {
299       path = g_strdup_printf ("/utf8/validate/%d", i);
300       g_test_add_data_func (path, &test[i], do_test);
301       g_free (path);
302     }
303
304   return g_test_run ();
305 }