1 /* Test of canonical decomposition 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_NFD, 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_NFD, 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_NFD, 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))
107 static const uint16_t input[] = { 0x0020 };
108 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
111 { /* LATIN CAPITAL LETTER A WITH DIAERESIS */
112 static const uint16_t input[] = { 0x00C4 };
113 static const uint16_t expected[] = { 0x0041, 0x0308 };
114 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
117 { /* LATIN CAPITAL LETTER A WITH DIAERESIS AND MACRON */
118 static const uint16_t input[] = { 0x01DE };
119 static const uint16_t expected[] = { 0x0041, 0x0308, 0x0304 };
120 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
123 { /* GREEK DIALYTIKA AND PERISPOMENI */
124 static const uint16_t input[] = { 0x1FC1 };
125 static const uint16_t expected[] = { 0x00A8, 0x0342 };
126 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
129 { /* SCRIPT SMALL L */
130 static const uint16_t input[] = { 0x2113 };
131 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
134 { /* NO-BREAK SPACE */
135 static const uint16_t input[] = { 0x00A0 };
136 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
139 { /* ARABIC LETTER VEH INITIAL FORM */
140 static const uint16_t input[] = { 0xFB6C };
141 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
144 { /* ARABIC LETTER VEH MEDIAL FORM */
145 static const uint16_t input[] = { 0xFB6D };
146 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
149 { /* ARABIC LETTER VEH FINAL FORM */
150 static const uint16_t input[] = { 0xFB6B };
151 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
154 { /* ARABIC LETTER VEH ISOLATED FORM */
155 static const uint16_t input[] = { 0xFB6A };
156 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
159 { /* CIRCLED NUMBER FIFTEEN */
160 static const uint16_t input[] = { 0x246E };
161 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
164 { /* TRADE MARK SIGN */
165 static const uint16_t input[] = { 0x2122 };
166 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
169 { /* LATIN SUBSCRIPT SMALL LETTER I */
170 static const uint16_t input[] = { 0x1D62 };
171 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
174 { /* PRESENTATION FORM FOR VERTICAL LEFT PARENTHESIS */
175 static const uint16_t input[] = { 0xFE35 };
176 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
179 { /* FULLWIDTH LATIN CAPITAL LETTER A */
180 static const uint16_t input[] = { 0xFF21 };
181 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
184 { /* HALFWIDTH IDEOGRAPHIC COMMA */
185 static const uint16_t input[] = { 0xFF64 };
186 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
189 { /* SMALL IDEOGRAPHIC COMMA */
190 static const uint16_t input[] = { 0xFE51 };
191 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
195 static const uint16_t input[] = { 0x3392 };
196 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
199 { /* VULGAR FRACTION THREE EIGHTHS */
200 static const uint16_t input[] = { 0x215C };
201 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
205 static const uint16_t input[] = { 0x00B5 };
206 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
209 { /* ARABIC LIGATURE SALLALLAHOU ALAYHE WASALLAM */
210 static const uint16_t input[] = { 0xFDFA };
211 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
214 { /* HANGUL SYLLABLE GEUL */
215 static const uint16_t input[] = { 0xAE00 };
216 static const uint16_t expected[] = { 0x1100, 0x1173, 0x11AF };
217 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
220 { /* HANGUL SYLLABLE GEU */
221 static const uint16_t input[] = { 0xADF8 };
222 static const uint16_t expected[] = { 0x1100, 0x1173 };
223 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
226 { /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a) 日本語,中文,한글" */
227 static const uint16_t input[] =
228 { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
229 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
230 0x0439, 0x0442, 0x0435, '!', ' ',
231 'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
232 '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
233 0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, '\n'
235 static const uint16_t expected[] =
236 { 'G', 'r', 0x0075, 0x0308, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
237 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
238 0x0438, 0x0306, 0x0442, 0x0435, '!', ' ',
239 'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
240 '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
241 0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',',
242 0x1112, 0x1161, 0x11AB, 0x1100, 0x1173, 0x11AF, '\n'
244 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
248 /* Declare failure if test takes too long, by using default abort
249 caused by SIGALRM. */
250 signal (SIGALRM, SIG_DFL);
254 /* Check that the sorting is not O(n²) but O(n log n). */
257 for (pass = 0; pass < 3; pass++)
261 uint16_t *input = (uint16_t *) malloc (2 * m * sizeof (uint16_t));
264 uint16_t *expected = input + m;
266 size_t m2 = (m - 1) / 2;
267 /* NB: m1 + m2 == m - 1. */
276 for (i = 0; i < m1; i++)
278 for (i = 0; i < m2; i++)
283 for (i = 0; i < m2; i++)
285 for (i = 0; i < m1; i++)
290 for (i = 0; i < m2; i++)
303 expected[0] = 0x0041;
305 for (i = 0; i < m1; i++)
307 for (i = 0; i < m2; i++)
310 for (; repeat > 0; repeat--)
311 ASSERT (check (input, m, expected, m) == 0);