1 /* Test of compatibility normalization of UTF-16 strings.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
21 #if GNULIB_UNINORM_U16_NORMALIZE
32 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
33 #define ASSERT(expr) \
38 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
46 check (const uint16_t *input, size_t input_length,
47 const uint16_t *expected, size_t expected_length)
52 /* Test return conventions with resultbuf == NULL. */
53 result = u16_normalize (UNINORM_NFKC, input, input_length, NULL, &length);
54 if (!(result != NULL))
56 if (!(length == expected_length))
58 if (!(u16_cmp (result, expected, expected_length) == 0))
62 /* Test return conventions with resultbuf too small. */
63 if (expected_length > 0)
65 uint16_t *preallocated;
67 length = expected_length - 1;
68 preallocated = (uint16_t *) malloc (length * sizeof (uint16_t));
69 result = u16_normalize (UNINORM_NFKC, input, input_length, preallocated, &length);
70 if (!(result != NULL))
72 if (!(result != preallocated))
74 if (!(length == expected_length))
76 if (!(u16_cmp (result, expected, expected_length) == 0))
82 /* Test return conventions with resultbuf large enough. */
84 uint16_t *preallocated;
86 length = expected_length;
87 preallocated = (uint16_t *) malloc (length * sizeof (uint16_t));
88 result = u16_normalize (UNINORM_NFKC, input, input_length, preallocated, &length);
89 if (!(result != NULL))
91 if (!(result == preallocated))
93 if (!(length == expected_length))
95 if (!(u16_cmp (result, expected, expected_length) == 0))
106 { /* Empty string. */
107 ASSERT (check (NULL, 0, NULL, 0) == 0);
110 static const uint16_t input[] = { 0x0020 };
111 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
114 { /* LATIN CAPITAL LETTER A WITH DIAERESIS */
115 static const uint16_t input[] = { 0x00C4 };
116 static const uint16_t decomposed[] = { 0x0041, 0x0308 };
117 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
118 ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
121 { /* LATIN CAPITAL LETTER A WITH DIAERESIS AND MACRON */
122 static const uint16_t input[] = { 0x01DE };
123 static const uint16_t decomposed[] = { 0x0041, 0x0308, 0x0304 };
124 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
125 ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
128 { /* ANGSTROM SIGN */
129 static const uint16_t input[] = { 0x212B };
130 static const uint16_t decomposed[] = { 0x0041, 0x030A };
131 static const uint16_t expected[] = { 0x00C5 };
132 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
133 ASSERT (check (decomposed, SIZEOF (decomposed), expected, SIZEOF (expected)) == 0);
134 ASSERT (check (expected, SIZEOF (expected), expected, SIZEOF (expected)) == 0);
137 { /* GREEK DIALYTIKA AND PERISPOMENI */
138 static const uint16_t input[] = { 0x1FC1 };
139 static const uint16_t decomposed[] = { 0x0020, 0x0308, 0x0342 };
140 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
141 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
144 { /* SCRIPT SMALL L */
145 static const uint16_t input[] = { 0x2113 };
146 static const uint16_t decomposed[] = { 0x006C };
147 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
148 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
151 { /* NO-BREAK SPACE */
152 static const uint16_t input[] = { 0x00A0 };
153 static const uint16_t decomposed[] = { 0x0020 };
154 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
155 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
158 { /* ARABIC LETTER VEH INITIAL FORM */
159 static const uint16_t input[] = { 0xFB6C };
160 static const uint16_t decomposed[] = { 0x06A4 };
161 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
162 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
165 { /* ARABIC LETTER VEH MEDIAL FORM */
166 static const uint16_t input[] = { 0xFB6D };
167 static const uint16_t decomposed[] = { 0x06A4 };
168 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
169 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
172 { /* ARABIC LETTER VEH FINAL FORM */
173 static const uint16_t input[] = { 0xFB6B };
174 static const uint16_t decomposed[] = { 0x06A4 };
175 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
176 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
179 { /* ARABIC LETTER VEH ISOLATED FORM */
180 static const uint16_t input[] = { 0xFB6A };
181 static const uint16_t decomposed[] = { 0x06A4 };
182 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
183 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
186 { /* CIRCLED NUMBER FIFTEEN */
187 static const uint16_t input[] = { 0x246E };
188 static const uint16_t decomposed[] = { 0x0031, 0x0035 };
189 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
190 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
193 { /* TRADE MARK SIGN */
194 static const uint16_t input[] = { 0x2122 };
195 static const uint16_t decomposed[] = { 0x0054, 0x004D };
196 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
197 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
200 { /* LATIN SUBSCRIPT SMALL LETTER I */
201 static const uint16_t input[] = { 0x1D62 };
202 static const uint16_t decomposed[] = { 0x0069 };
203 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
204 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
207 { /* PRESENTATION FORM FOR VERTICAL LEFT PARENTHESIS */
208 static const uint16_t input[] = { 0xFE35 };
209 static const uint16_t decomposed[] = { 0x0028 };
210 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
211 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
214 { /* FULLWIDTH LATIN CAPITAL LETTER A */
215 static const uint16_t input[] = { 0xFF21 };
216 static const uint16_t decomposed[] = { 0x0041 };
217 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
218 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
221 { /* HALFWIDTH IDEOGRAPHIC COMMA */
222 static const uint16_t input[] = { 0xFF64 };
223 static const uint16_t decomposed[] = { 0x3001 };
224 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
225 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
228 { /* SMALL IDEOGRAPHIC COMMA */
229 static const uint16_t input[] = { 0xFE51 };
230 static const uint16_t decomposed[] = { 0x3001 };
231 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
232 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
236 static const uint16_t input[] = { 0x3392 };
237 static const uint16_t decomposed[] = { 0x004D, 0x0048, 0x007A };
238 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
239 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
242 { /* VULGAR FRACTION THREE EIGHTHS */
243 static const uint16_t input[] = { 0x215C };
244 static const uint16_t decomposed[] = { 0x0033, 0x2044, 0x0038 };
245 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
246 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
250 static const uint16_t input[] = { 0x00B5 };
251 static const uint16_t decomposed[] = { 0x03BC };
252 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
253 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
256 { /* ARABIC LIGATURE SALLALLAHOU ALAYHE WASALLAM */
257 static const uint16_t input[] = { 0xFDFA };
258 static const uint16_t decomposed[] =
259 { 0x0635, 0x0644, 0x0649, 0x0020, 0x0627, 0x0644, 0x0644, 0x0647, 0x0020,
260 0x0639, 0x0644, 0x064A, 0x0647, 0x0020, 0x0648, 0x0633, 0x0644, 0x0645
262 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
263 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
266 { /* HANGUL SYLLABLE GEUL */
267 static const uint16_t input[] = { 0xAE00 };
268 static const uint16_t decomposed[] = { 0x1100, 0x1173, 0x11AF };
269 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
270 ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
273 { /* HANGUL SYLLABLE GEU */
274 static const uint16_t input[] = { 0xADF8 };
275 static const uint16_t decomposed[] = { 0x1100, 0x1173 };
276 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
277 ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
280 { /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a) 日本語,中文,한글" */
281 static const uint16_t input[] =
282 { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
283 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
284 0x0439, 0x0442, 0x0435, '!', ' ',
285 'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
286 '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
287 0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, '\n'
289 static const uint16_t decomposed[] =
290 { 'G', 'r', 0x0075, 0x0308, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
291 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
292 0x0438, 0x0306, 0x0442, 0x0435, '!', ' ',
293 'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x0032,
294 '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
295 0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',',
296 0x1112, 0x1161, 0x11AB, 0x1100, 0x1173, 0x11AF, '\n'
298 static const uint16_t expected[] =
299 { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
300 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
301 0x0439, 0x0442, 0x0435, '!', ' ',
302 'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x0032,
303 '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
304 0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, '\n'
306 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
307 ASSERT (check (decomposed, SIZEOF (decomposed), expected, SIZEOF (expected)) == 0);
308 ASSERT (check (expected, SIZEOF (expected), expected, SIZEOF (expected)) == 0);
312 /* Declare failure if test takes too long, by using default abort
313 caused by SIGALRM. */
314 signal (SIGALRM, SIG_DFL);
318 /* Check that the sorting is not O(n²) but O(n log n). */
321 for (pass = 0; pass < 3; pass++)
325 uint16_t *input = (uint16_t *) malloc (2 * m * sizeof (uint16_t));
328 uint16_t *expected = input + m;
330 size_t m2 = (m - 1) / 2;
331 /* NB: m1 + m2 == m - 1. */
340 for (i = 0; i < m1; i++)
342 for (i = 0; i < m2; i++)
347 for (i = 0; i < m2; i++)
349 for (i = 0; i < m1; i++)
354 for (i = 0; i < m2; i++)
367 expected[0] = 0x00C0;
369 for (i = 0; i < m1; i++)
371 for (i = 0; i < m2 - 1; i++)
374 for (; repeat > 0; repeat--)
376 ASSERT (check (input, m, expected, m - 1) == 0);
377 ASSERT (check (expected, m - 1, expected, m - 1) == 0);