errno-h: document Haiku errors can’t be -1
[gnulib.git] / tests / uninorm / test-u32-nfkc.c
blob454b7afd64039616f73207dd891e980178190756
1 /* Test of compatibility normalization of UTF-32 strings.
2 Copyright (C) 2009-2025 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
19 #include <config.h>
21 #if GNULIB_TEST_UNINORM_U32_NORMALIZE
23 #include "uninorm.h"
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <unistd.h>
29 #include "unistr.h"
30 #define NO_MAIN_HERE
31 #include "macros.h"
33 static int
34 check (const uint32_t *input, size_t input_length,
35 const uint32_t *expected, size_t expected_length)
37 size_t length;
38 uint32_t *result;
40 /* Test return conventions with resultbuf == NULL. */
41 result = u32_normalize (UNINORM_NFKC, input, input_length, NULL, &length);
42 if (!(result != NULL))
43 return 1;
44 if (!(length == expected_length))
45 return 2;
46 if (!(u32_cmp (result, expected, expected_length) == 0))
47 return 3;
48 free (result);
50 /* Test return conventions with resultbuf too small. */
51 if (expected_length > 0)
53 uint32_t *preallocated;
55 length = expected_length - 1;
56 preallocated = (uint32_t *) malloc (length * sizeof (uint32_t));
57 result = u32_normalize (UNINORM_NFKC, input, input_length, preallocated, &length);
58 if (!(result != NULL))
59 return 4;
60 if (!(result != preallocated))
61 return 5;
62 if (!(length == expected_length))
63 return 6;
64 if (!(u32_cmp (result, expected, expected_length) == 0))
65 return 7;
66 free (result);
67 free (preallocated);
70 /* Test return conventions with resultbuf large enough. */
72 uint32_t *preallocated;
74 length = expected_length;
75 preallocated = (uint32_t *) malloc (length * sizeof (uint32_t));
76 result = u32_normalize (UNINORM_NFKC, input, input_length, preallocated, &length);
77 if (!(result != NULL))
78 return 8;
79 if (!(preallocated == NULL || result == preallocated))
80 return 9;
81 if (!(length == expected_length))
82 return 10;
83 if (!(u32_cmp (result, expected, expected_length) == 0))
84 return 11;
85 free (preallocated);
88 return 0;
91 void
92 test_u32_nfkc (void)
94 { /* Empty string. */
95 ASSERT (check (NULL, 0, NULL, 0) == 0);
97 { /* SPACE */
98 static const uint32_t input[] = { 0x0020 };
99 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
102 { /* LATIN CAPITAL LETTER A WITH DIAERESIS */
103 static const uint32_t input[] = { 0x00C4 };
104 static const uint32_t decomposed[] = { 0x0041, 0x0308 };
105 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
106 ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
109 { /* LATIN CAPITAL LETTER A WITH DIAERESIS AND MACRON */
110 static const uint32_t input[] = { 0x01DE };
111 static const uint32_t decomposed[] = { 0x0041, 0x0308, 0x0304 };
112 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
113 ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
116 { /* ANGSTROM SIGN */
117 static const uint32_t input[] = { 0x212B };
118 static const uint32_t decomposed[] = { 0x0041, 0x030A };
119 static const uint32_t expected[] = { 0x00C5 };
120 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
121 ASSERT (check (decomposed, SIZEOF (decomposed), expected, SIZEOF (expected)) == 0);
122 ASSERT (check (expected, SIZEOF (expected), expected, SIZEOF (expected)) == 0);
125 { /* GREEK DIALYTIKA AND PERISPOMENI */
126 static const uint32_t input[] = { 0x1FC1 };
127 static const uint32_t decomposed[] = { 0x0020, 0x0308, 0x0342 };
128 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
129 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
132 { /* SCRIPT SMALL L */
133 static const uint32_t input[] = { 0x2113 };
134 static const uint32_t decomposed[] = { 0x006C };
135 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
136 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
139 { /* NO-BREAK SPACE */
140 static const uint32_t input[] = { 0x00A0 };
141 static const uint32_t decomposed[] = { 0x0020 };
142 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
143 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
146 { /* ARABIC LETTER VEH INITIAL FORM */
147 static const uint32_t input[] = { 0xFB6C };
148 static const uint32_t decomposed[] = { 0x06A4 };
149 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
150 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
153 { /* ARABIC LETTER VEH MEDIAL FORM */
154 static const uint32_t input[] = { 0xFB6D };
155 static const uint32_t decomposed[] = { 0x06A4 };
156 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
157 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
160 { /* ARABIC LETTER VEH FINAL FORM */
161 static const uint32_t input[] = { 0xFB6B };
162 static const uint32_t decomposed[] = { 0x06A4 };
163 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
164 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
167 { /* ARABIC LETTER VEH ISOLATED FORM */
168 static const uint32_t input[] = { 0xFB6A };
169 static const uint32_t decomposed[] = { 0x06A4 };
170 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
171 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
174 { /* CIRCLED NUMBER FIFTEEN */
175 static const uint32_t input[] = { 0x246E };
176 static const uint32_t decomposed[] = { 0x0031, 0x0035 };
177 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
178 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
181 { /* TRADE MARK SIGN */
182 static const uint32_t input[] = { 0x2122 };
183 static const uint32_t decomposed[] = { 0x0054, 0x004D };
184 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
185 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
188 { /* LATIN SUBSCRIPT SMALL LETTER I */
189 static const uint32_t input[] = { 0x1D62 };
190 static const uint32_t decomposed[] = { 0x0069 };
191 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
192 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
195 { /* PRESENTATION FORM FOR VERTICAL LEFT PARENTHESIS */
196 static const uint32_t input[] = { 0xFE35 };
197 static const uint32_t decomposed[] = { 0x0028 };
198 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
199 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
202 { /* FULLWIDTH LATIN CAPITAL LETTER A */
203 static const uint32_t input[] = { 0xFF21 };
204 static const uint32_t decomposed[] = { 0x0041 };
205 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
206 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
209 { /* HALFWIDTH IDEOGRAPHIC COMMA */
210 static const uint32_t input[] = { 0xFF64 };
211 static const uint32_t decomposed[] = { 0x3001 };
212 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
213 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
216 { /* SMALL IDEOGRAPHIC COMMA */
217 static const uint32_t input[] = { 0xFE51 };
218 static const uint32_t decomposed[] = { 0x3001 };
219 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
220 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
223 { /* SQUARE MHZ */
224 static const uint32_t input[] = { 0x3392 };
225 static const uint32_t decomposed[] = { 0x004D, 0x0048, 0x007A };
226 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
227 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
230 { /* VULGAR FRACTION THREE EIGHTHS */
231 static const uint32_t input[] = { 0x215C };
232 static const uint32_t decomposed[] = { 0x0033, 0x2044, 0x0038 };
233 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
234 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
237 { /* MICRO SIGN */
238 static const uint32_t input[] = { 0x00B5 };
239 static const uint32_t decomposed[] = { 0x03BC };
240 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
241 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
244 { /* ARABIC LIGATURE SALLALLAHOU ALAYHE WASALLAM */
245 static const uint32_t input[] = { 0xFDFA };
246 static const uint32_t decomposed[] =
247 { 0x0635, 0x0644, 0x0649, 0x0020, 0x0627, 0x0644, 0x0644, 0x0647, 0x0020,
248 0x0639, 0x0644, 0x064A, 0x0647, 0x0020, 0x0648, 0x0633, 0x0644, 0x0645
250 ASSERT (check (input, SIZEOF (input), decomposed, SIZEOF (decomposed)) == 0);
251 ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
254 { /* HANGUL SYLLABLE GEUL */
255 static const uint32_t input[] = { 0xAE00 };
256 static const uint32_t decomposed[] = { 0x1100, 0x1173, 0x11AF };
257 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
258 ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
261 { /* HANGUL SYLLABLE GEU */
262 static const uint32_t input[] = { 0xADF8 };
263 static const uint32_t decomposed[] = { 0x1100, 0x1173 };
264 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
265 ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
268 { /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a) 日本語,中文,한글" */
269 static const uint32_t input[] =
270 { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
271 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
272 0x0439, 0x0442, 0x0435, '!', ' ',
273 'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
274 '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
275 0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, '\n'
277 static const uint32_t decomposed[] =
278 { 'G', 'r', 0x0075, 0x0308, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
279 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
280 0x0438, 0x0306, 0x0442, 0x0435, '!', ' ',
281 'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x0032,
282 '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
283 0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',',
284 0x1112, 0x1161, 0x11AB, 0x1100, 0x1173, 0x11AF, '\n'
286 static const uint32_t expected[] =
287 { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
288 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
289 0x0439, 0x0442, 0x0435, '!', ' ',
290 'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x0032,
291 '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
292 0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, '\n'
294 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
295 ASSERT (check (decomposed, SIZEOF (decomposed), expected, SIZEOF (expected)) == 0);
296 ASSERT (check (expected, SIZEOF (expected), expected, SIZEOF (expected)) == 0);
299 #if HAVE_DECL_ALARM
300 /* Declare failure if test takes too long, by using default abort
301 caused by SIGALRM. */
302 signal (SIGALRM, SIG_DFL);
303 alarm (50);
304 #endif
306 /* Check that the sorting is not O(n²) but O(n log n). */
308 int pass;
309 for (pass = 0; pass < 3; pass++)
311 size_t repeat = 1;
312 size_t m = 100000;
313 uint32_t *input = (uint32_t *) malloc (2 * m * sizeof (uint32_t));
314 if (input != NULL)
316 uint32_t *expected = input + m;
317 size_t m1 = m / 2;
318 size_t m2 = (m - 1) / 2;
319 /* NB: m1 + m2 == m - 1. */
320 uint32_t *p;
321 size_t i;
323 input[0] = 0x0041;
324 p = input + 1;
325 switch (pass)
327 case 0:
328 for (i = 0; i < m1; i++)
329 *p++ = 0x0319;
330 for (i = 0; i < m2; i++)
331 *p++ = 0x0300;
332 break;
334 case 1:
335 for (i = 0; i < m2; i++)
336 *p++ = 0x0300;
337 for (i = 0; i < m1; i++)
338 *p++ = 0x0319;
339 break;
341 case 2:
342 for (i = 0; i < m2; i++)
344 *p++ = 0x0319;
345 *p++ = 0x0300;
347 for (; i < m1; i++)
348 *p++ = 0x0319;
349 break;
351 default:
352 abort ();
355 expected[0] = 0x00C0;
356 p = expected + 1;
357 for (i = 0; i < m1; i++)
358 *p++ = 0x0319;
359 for (i = 0; i < m2 - 1; i++)
360 *p++ = 0x0300;
362 for (; repeat > 0; repeat--)
364 ASSERT (check (input, m, expected, m - 1) == 0);
365 ASSERT (check (expected, m - 1, expected, m - 1) == 0);
368 free (input);
374 #else
376 void
377 test_u32_nfkc (void)
381 #endif