Avoid test failures on AIX and OSF/1.
[gnulib.git] / tests / uninorm / test-u32-nfc.c
1 /* Test of canonical normalization of UTF-32 strings.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009.  */
18
19 #include <config.h>
20
21 #if GNULIB_UNINORM_U32_NORMALIZE
22
23 #include "uninorm.h"
24
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29
30 #include "unistr.h"
31
32 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
33 #define ASSERT(expr) \
34   do                                                                         \
35     {                                                                        \
36       if (!(expr))                                                           \
37         {                                                                    \
38           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
39           fflush (stderr);                                                   \
40           abort ();                                                          \
41         }                                                                    \
42     }                                                                        \
43   while (0)
44
45 static int
46 check (const uint32_t *input, size_t input_length,
47        const uint32_t *expected, size_t expected_length)
48 {
49   size_t length;
50   uint32_t *result;
51
52   /* Test return conventions with resultbuf == NULL.  */
53   result = u32_normalize (UNINORM_NFC, input, input_length, NULL, &length);
54   if (!(result != NULL))
55     return 1;
56   if (!(length == expected_length))
57     return 2;
58   if (!(u32_cmp (result, expected, expected_length) == 0))
59     return 3;
60   free (result);
61
62   /* Test return conventions with resultbuf too small.  */
63   if (expected_length > 0)
64     {
65       uint32_t *preallocated;
66
67       length = expected_length - 1;
68       preallocated = (uint32_t *) malloc (length * sizeof (uint32_t));
69       result = u32_normalize (UNINORM_NFC, input, input_length, preallocated, &length);
70       if (!(result != NULL))
71         return 4;
72       if (!(result != preallocated))
73         return 5;
74       if (!(length == expected_length))
75         return 6;
76       if (!(u32_cmp (result, expected, expected_length) == 0))
77         return 7;
78       free (result);
79       free (preallocated);
80     }
81
82   /* Test return conventions with resultbuf large enough.  */
83   {
84     uint32_t *preallocated;
85
86     length = expected_length;
87     preallocated = (uint32_t *) malloc (length * sizeof (uint32_t));
88     result = u32_normalize (UNINORM_NFC, input, input_length, preallocated, &length);
89     if (!(result != NULL))
90       return 8;
91     if (!(preallocated == NULL || result == preallocated))
92       return 9;
93     if (!(length == expected_length))
94       return 10;
95     if (!(u32_cmp (result, expected, expected_length) == 0))
96       return 11;
97     free (preallocated);
98   }
99
100   return 0;
101 }
102
103 void
104 test_u32_nfc (void)
105 {
106   { /* Empty string.  */
107     ASSERT (check (NULL, 0, NULL, 0) == 0);
108   }
109   { /* SPACE */
110     static const uint32_t input[]    = { 0x0020 };
111     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
112   }
113
114   { /* LATIN CAPITAL LETTER A WITH DIAERESIS */
115     static const uint32_t input[]      = { 0x00C4 };
116     static const uint32_t decomposed[] = { 0x0041, 0x0308 };
117     ASSERT (check (input, SIZEOF (input),           input, SIZEOF (input)) == 0);
118     ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
119   }
120
121   { /* LATIN CAPITAL LETTER A WITH DIAERESIS AND MACRON */
122     static const uint32_t input[]      = { 0x01DE };
123     static const uint32_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);
126   }
127
128   { /* ANGSTROM SIGN */
129     static const uint32_t input[]      = { 0x212B };
130     static const uint32_t decomposed[] = { 0x0041, 0x030A };
131     static const uint32_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);
135   }
136
137   { /* GREEK DIALYTIKA AND PERISPOMENI */
138     static const uint32_t input[]      = { 0x1FC1 };
139     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
140   }
141
142   { /* SCRIPT SMALL L */
143     static const uint32_t input[]      = { 0x2113 };
144     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
145   }
146
147   { /* NO-BREAK SPACE */
148     static const uint32_t input[]      = { 0x00A0 };
149     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
150   }
151
152   { /* ARABIC LETTER VEH INITIAL FORM */
153     static const uint32_t input[]      = { 0xFB6C };
154     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
155   }
156
157   { /* ARABIC LETTER VEH MEDIAL FORM */
158     static const uint32_t input[]      = { 0xFB6D };
159     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
160   }
161
162   { /* ARABIC LETTER VEH FINAL FORM */
163     static const uint32_t input[]      = { 0xFB6B };
164     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
165   }
166
167   { /* ARABIC LETTER VEH ISOLATED FORM */
168     static const uint32_t input[]      = { 0xFB6A };
169     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
170   }
171
172   { /* CIRCLED NUMBER FIFTEEN */
173     static const uint32_t input[]      = { 0x246E };
174     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
175   }
176
177   { /* TRADE MARK SIGN */
178     static const uint32_t input[]      = { 0x2122 };
179     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
180   }
181
182   { /* LATIN SUBSCRIPT SMALL LETTER I */
183     static const uint32_t input[]      = { 0x1D62 };
184     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
185   }
186
187   { /* PRESENTATION FORM FOR VERTICAL LEFT PARENTHESIS */
188     static const uint32_t input[]      = { 0xFE35 };
189     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
190   }
191
192   { /* FULLWIDTH LATIN CAPITAL LETTER A */
193     static const uint32_t input[]      = { 0xFF21 };
194     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
195   }
196
197   { /* HALFWIDTH IDEOGRAPHIC COMMA */
198     static const uint32_t input[]      = { 0xFF64 };
199     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
200   }
201
202   { /* SMALL IDEOGRAPHIC COMMA */
203     static const uint32_t input[]      = { 0xFE51 };
204     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
205   }
206
207   { /* SQUARE MHZ */
208     static const uint32_t input[]      = { 0x3392 };
209     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
210   }
211
212   { /* VULGAR FRACTION THREE EIGHTHS */
213     static const uint32_t input[]      = { 0x215C };
214     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
215   }
216
217   { /* MICRO SIGN */
218     static const uint32_t input[]      = { 0x00B5 };
219     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
220   }
221
222   { /* ARABIC LIGATURE SALLALLAHOU ALAYHE WASALLAM */
223     static const uint32_t input[]      = { 0xFDFA };
224     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
225   }
226
227   { /* HANGUL SYLLABLE GEUL */
228     static const uint32_t input[]      = { 0xAE00 };
229     static const uint32_t decomposed[] = { 0x1100, 0x1173, 0x11AF };
230     ASSERT (check (input, SIZEOF (input),           input, SIZEOF (input)) == 0);
231     ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
232   }
233
234   { /* HANGUL SYLLABLE GEU */
235     static const uint32_t input[]      = { 0xADF8 };
236     static const uint32_t decomposed[] = { 0x1100, 0x1173 };
237     ASSERT (check (input, SIZEOF (input),           input, SIZEOF (input)) == 0);
238     ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
239   }
240
241   { /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a)  日本語,中文,한글" */
242     static const uint32_t input[] =
243       { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
244         0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
245         0x0439, 0x0442, 0x0435, '!', ' ',
246         'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
247         '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
248         0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, '\n'
249       };
250     static const uint32_t decomposed[] =
251       { 'G', 'r', 0x0075, 0x0308, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
252         0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
253         0x0438, 0x0306, 0x0442, 0x0435, '!', ' ',
254         'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
255         '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
256         0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',',
257         0x1112, 0x1161, 0x11AB, 0x1100, 0x1173, 0x11AF, '\n'
258       };
259     ASSERT (check (input, SIZEOF (input),           input, SIZEOF (input)) == 0);
260     ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
261   }
262
263 #if HAVE_DECL_ALARM
264   /* Declare failure if test takes too long, by using default abort
265      caused by SIGALRM.  */
266   signal (SIGALRM, SIG_DFL);
267   alarm (50);
268 #endif
269
270   /* Check that the sorting is not O(n²) but O(n log n).  */
271   {
272     int pass;
273     for (pass = 0; pass < 3; pass++)
274       {
275         size_t repeat = 1;
276         size_t m = 100000;
277         uint32_t *input = (uint32_t *) malloc (2 * m * sizeof (uint32_t));
278         if (input != NULL)
279           {
280             uint32_t *expected = input + m;
281             size_t m1 = m / 2;
282             size_t m2 = (m - 1) / 2;
283             /* NB: m1 + m2 == m - 1.  */
284             uint32_t *p;
285             size_t i;
286
287             input[0] = 0x0041;
288             p = input + 1;
289             switch (pass)
290               {
291               case 0:
292                 for (i = 0; i < m1; i++)
293                   *p++ = 0x0319;
294                 for (i = 0; i < m2; i++)
295                   *p++ = 0x0300;
296                 break;
297
298               case 1:
299                 for (i = 0; i < m2; i++)
300                   *p++ = 0x0300;
301                 for (i = 0; i < m1; i++)
302                   *p++ = 0x0319;
303                 break;
304
305               case 2:
306                 for (i = 0; i < m2; i++)
307                   {
308                     *p++ = 0x0319;
309                     *p++ = 0x0300;
310                   }
311                 for (; i < m1; i++)
312                   *p++ = 0x0319;
313                 break;
314
315               default:
316                 abort ();
317               }
318
319             expected[0] = 0x00C0;
320             p = expected + 1;
321             for (i = 0; i < m1; i++)
322               *p++ = 0x0319;
323             for (i = 0; i < m2 - 1; i++)
324               *p++ = 0x0300;
325
326             for (; repeat > 0; repeat--)
327               {
328                 ASSERT (check (input, m,        expected, m - 1) == 0);
329                 ASSERT (check (expected, m - 1, expected, m - 1) == 0);
330               }
331
332             free (input);
333           }
334       }
335   }
336 }
337
338 #else
339
340 void
341 test_u32_nfc (void)
342 {
343 }
344
345 #endif