gnupload: Support option -h as alias of --help.
[gnulib.git] / tests / uninorm / test-u32-nfd.c
bloba5914eb67f65f39360664c4680cbe34eb23ad3e9
1 /* Test of canonical decomposition of UTF-32 strings.
2 Copyright (C) 2009-2018 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 #include "macros.h"
32 static int
33 check (const uint32_t *input, size_t input_length,
34 const uint32_t *expected, size_t expected_length)
36 size_t length;
37 uint32_t *result;
39 /* Test return conventions with resultbuf == NULL. */
40 result = u32_normalize (UNINORM_NFD, input, input_length, NULL, &length);
41 if (!(result != NULL))
42 return 1;
43 if (!(length == expected_length))
44 return 2;
45 if (!(u32_cmp (result, expected, expected_length) == 0))
46 return 3;
47 free (result);
49 /* Test return conventions with resultbuf too small. */
50 if (expected_length > 0)
52 uint32_t *preallocated;
54 length = expected_length - 1;
55 preallocated = (uint32_t *) malloc (length * sizeof (uint32_t));
56 result = u32_normalize (UNINORM_NFD, input, input_length, preallocated, &length);
57 if (!(result != NULL))
58 return 4;
59 if (!(result != preallocated))
60 return 5;
61 if (!(length == expected_length))
62 return 6;
63 if (!(u32_cmp (result, expected, expected_length) == 0))
64 return 7;
65 free (result);
66 free (preallocated);
69 /* Test return conventions with resultbuf large enough. */
71 uint32_t *preallocated;
73 length = expected_length;
74 preallocated = (uint32_t *) malloc (length * sizeof (uint32_t));
75 result = u32_normalize (UNINORM_NFD, input, input_length, preallocated, &length);
76 if (!(result != NULL))
77 return 8;
78 if (!(preallocated == NULL || result == preallocated))
79 return 9;
80 if (!(length == expected_length))
81 return 10;
82 if (!(u32_cmp (result, expected, expected_length) == 0))
83 return 11;
84 free (preallocated);
87 return 0;
90 void
91 test_u32_nfd (void)
93 { /* Empty string. */
94 ASSERT (check (NULL, 0, NULL, 0) == 0);
96 { /* SPACE */
97 static const uint32_t input[] = { 0x0020 };
98 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
101 { /* LATIN CAPITAL LETTER A WITH DIAERESIS */
102 static const uint32_t input[] = { 0x00C4 };
103 static const uint32_t expected[] = { 0x0041, 0x0308 };
104 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
107 { /* LATIN CAPITAL LETTER A WITH DIAERESIS AND MACRON */
108 static const uint32_t input[] = { 0x01DE };
109 static const uint32_t expected[] = { 0x0041, 0x0308, 0x0304 };
110 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
113 { /* GREEK DIALYTIKA AND PERISPOMENI */
114 static const uint32_t input[] = { 0x1FC1 };
115 static const uint32_t expected[] = { 0x00A8, 0x0342 };
116 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
119 { /* SCRIPT SMALL L */
120 static const uint32_t input[] = { 0x2113 };
121 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
124 { /* NO-BREAK SPACE */
125 static const uint32_t input[] = { 0x00A0 };
126 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
129 { /* ARABIC LETTER VEH INITIAL FORM */
130 static const uint32_t input[] = { 0xFB6C };
131 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
134 { /* ARABIC LETTER VEH MEDIAL FORM */
135 static const uint32_t input[] = { 0xFB6D };
136 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
139 { /* ARABIC LETTER VEH FINAL FORM */
140 static const uint32_t input[] = { 0xFB6B };
141 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
144 { /* ARABIC LETTER VEH ISOLATED FORM */
145 static const uint32_t input[] = { 0xFB6A };
146 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
149 { /* CIRCLED NUMBER FIFTEEN */
150 static const uint32_t input[] = { 0x246E };
151 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
154 { /* TRADE MARK SIGN */
155 static const uint32_t input[] = { 0x2122 };
156 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
159 { /* LATIN SUBSCRIPT SMALL LETTER I */
160 static const uint32_t input[] = { 0x1D62 };
161 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
164 { /* PRESENTATION FORM FOR VERTICAL LEFT PARENTHESIS */
165 static const uint32_t input[] = { 0xFE35 };
166 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
169 { /* FULLWIDTH LATIN CAPITAL LETTER A */
170 static const uint32_t input[] = { 0xFF21 };
171 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
174 { /* HALFWIDTH IDEOGRAPHIC COMMA */
175 static const uint32_t input[] = { 0xFF64 };
176 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
179 { /* SMALL IDEOGRAPHIC COMMA */
180 static const uint32_t input[] = { 0xFE51 };
181 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
184 { /* SQUARE MHZ */
185 static const uint32_t input[] = { 0x3392 };
186 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
189 { /* VULGAR FRACTION THREE EIGHTHS */
190 static const uint32_t input[] = { 0x215C };
191 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
194 { /* MICRO SIGN */
195 static const uint32_t input[] = { 0x00B5 };
196 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
199 { /* ARABIC LIGATURE SALLALLAHOU ALAYHE WASALLAM */
200 static const uint32_t input[] = { 0xFDFA };
201 ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
204 { /* HANGUL SYLLABLE GEUL */
205 static const uint32_t input[] = { 0xAE00 };
206 static const uint32_t expected[] = { 0x1100, 0x1173, 0x11AF };
207 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
210 { /* HANGUL SYLLABLE GEU */
211 static const uint32_t input[] = { 0xADF8 };
212 static const uint32_t expected[] = { 0x1100, 0x1173 };
213 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
216 { /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a) 日本語,中文,한글" */
217 static const uint32_t input[] =
218 { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
219 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
220 0x0439, 0x0442, 0x0435, '!', ' ',
221 'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
222 '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
223 0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, '\n'
225 static const uint32_t expected[] =
226 { 'G', 'r', 0x0075, 0x0308, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
227 0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
228 0x0438, 0x0306, 0x0442, 0x0435, '!', ' ',
229 'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
230 '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
231 0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',',
232 0x1112, 0x1161, 0x11AB, 0x1100, 0x1173, 0x11AF, '\n'
234 ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
237 #if HAVE_DECL_ALARM
238 /* Declare failure if test takes too long, by using default abort
239 caused by SIGALRM. */
240 signal (SIGALRM, SIG_DFL);
241 alarm (50);
242 #endif
244 /* Check that the sorting is not O(n²) but O(n log n). */
246 int pass;
247 for (pass = 0; pass < 3; pass++)
249 size_t repeat = 1;
250 size_t m = 100000;
251 uint32_t *input = (uint32_t *) malloc (2 * m * sizeof (uint32_t));
252 if (input != NULL)
254 uint32_t *expected = input + m;
255 size_t m1 = m / 2;
256 size_t m2 = (m - 1) / 2;
257 /* NB: m1 + m2 == m - 1. */
258 uint32_t *p;
259 size_t i;
261 input[0] = 0x0041;
262 p = input + 1;
263 switch (pass)
265 case 0:
266 for (i = 0; i < m1; i++)
267 *p++ = 0x0319;
268 for (i = 0; i < m2; i++)
269 *p++ = 0x0300;
270 break;
272 case 1:
273 for (i = 0; i < m2; i++)
274 *p++ = 0x0300;
275 for (i = 0; i < m1; i++)
276 *p++ = 0x0319;
277 break;
279 case 2:
280 for (i = 0; i < m2; i++)
282 *p++ = 0x0319;
283 *p++ = 0x0300;
285 for (; i < m1; i++)
286 *p++ = 0x0319;
287 break;
289 default:
290 abort ();
293 expected[0] = 0x0041;
294 p = expected + 1;
295 for (i = 0; i < m1; i++)
296 *p++ = 0x0319;
297 for (i = 0; i < m2; i++)
298 *p++ = 0x0300;
300 for (; repeat > 0; repeat--)
301 ASSERT (check (input, m, expected, m) == 0);
303 free (input);
309 #else
311 void
312 test_u32_nfd (void)
316 #endif