Tests for module 'uninorm/nfkc'.
[gnulib.git] / tests / uninorm / test-u32-nfkc.c
1 /* Test of compatibility 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_NFKC, 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_NFKC, 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_NFKC, input, input_length, preallocated, &length);
89     if (!(result != NULL))
90       return 8;
91     if (!(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_nfkc (void)
105 {
106   { /* SPACE */
107     static const uint32_t input[]    = { 0x0020 };
108     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
109   }
110
111   { /* LATIN CAPITAL LETTER A WITH DIAERESIS */
112     static const uint32_t input[]      = { 0x00C4 };
113     static const uint32_t decomposed[] = { 0x0041, 0x0308 };
114     ASSERT (check (input, SIZEOF (input),           input, SIZEOF (input)) == 0);
115     ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
116   }
117
118   { /* LATIN CAPITAL LETTER A WITH DIAERESIS AND MACRON */
119     static const uint32_t input[]      = { 0x01DE };
120     static const uint32_t decomposed[] = { 0x0041, 0x0308, 0x0304 };
121     ASSERT (check (input, SIZEOF (input),           input, SIZEOF (input)) == 0);
122     ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
123   }
124
125   { /* ANGSTROM SIGN */
126     static const uint32_t input[]      = { 0x212B };
127     static const uint32_t decomposed[] = { 0x0041, 0x030A };
128     static const uint32_t expected[]   = { 0x00C5 };
129     ASSERT (check (input, SIZEOF (input),           expected, SIZEOF (expected)) == 0);
130     ASSERT (check (decomposed, SIZEOF (decomposed), expected, SIZEOF (expected)) == 0);
131     ASSERT (check (expected, SIZEOF (expected),     expected, SIZEOF (expected)) == 0);
132   }
133
134   { /* GREEK DIALYTIKA AND PERISPOMENI */
135     static const uint32_t input[]      = { 0x1FC1 };
136     static const uint32_t decomposed[] = { 0x0020, 0x0308, 0x0342 };
137     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
138     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
139   }
140
141   { /* SCRIPT SMALL L */
142     static const uint32_t input[]      = { 0x2113 };
143     static const uint32_t decomposed[] = { 0x006C };
144     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
145     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
146   }
147
148   { /* NO-BREAK SPACE */
149     static const uint32_t input[]      = { 0x00A0 };
150     static const uint32_t decomposed[] = { 0x0020 };
151     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
152     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
153   }
154
155   { /* ARABIC LETTER VEH INITIAL FORM */
156     static const uint32_t input[]      = { 0xFB6C };
157     static const uint32_t decomposed[] = { 0x06A4 };
158     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
159     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
160   }
161
162   { /* ARABIC LETTER VEH MEDIAL FORM */
163     static const uint32_t input[]      = { 0xFB6D };
164     static const uint32_t decomposed[] = { 0x06A4 };
165     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
166     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
167   }
168
169   { /* ARABIC LETTER VEH FINAL FORM */
170     static const uint32_t input[]      = { 0xFB6B };
171     static const uint32_t decomposed[] = { 0x06A4 };
172     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
173     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
174   }
175
176   { /* ARABIC LETTER VEH ISOLATED FORM */
177     static const uint32_t input[]      = { 0xFB6A };
178     static const uint32_t decomposed[] = { 0x06A4 };
179     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
180     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
181   }
182
183   { /* CIRCLED NUMBER FIFTEEN */
184     static const uint32_t input[]      = { 0x246E };
185     static const uint32_t decomposed[] = { 0x0031, 0x0035 };
186     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
187     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
188   }
189
190   { /* TRADE MARK SIGN */
191     static const uint32_t input[]      = { 0x2122 };
192     static const uint32_t decomposed[] = { 0x0054, 0x004D };
193     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
194     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
195   }
196
197   { /* LATIN SUBSCRIPT SMALL LETTER I */
198     static const uint32_t input[]      = { 0x1D62 };
199     static const uint32_t decomposed[] = { 0x0069 };
200     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
201     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
202   }
203
204   { /* PRESENTATION FORM FOR VERTICAL LEFT PARENTHESIS */
205     static const uint32_t input[]      = { 0xFE35 };
206     static const uint32_t decomposed[] = { 0x0028 };
207     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
208     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
209   }
210
211   { /* FULLWIDTH LATIN CAPITAL LETTER A */
212     static const uint32_t input[]      = { 0xFF21 };
213     static const uint32_t decomposed[] = { 0x0041 };
214     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
215     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
216   }
217
218   { /* HALFWIDTH IDEOGRAPHIC COMMA */
219     static const uint32_t input[]      = { 0xFF64 };
220     static const uint32_t decomposed[] = { 0x3001 };
221     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
222     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
223   }
224
225   { /* SMALL IDEOGRAPHIC COMMA */
226     static const uint32_t input[]      = { 0xFE51 };
227     static const uint32_t decomposed[] = { 0x3001 };
228     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
229     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
230   }
231
232   { /* SQUARE MHZ */
233     static const uint32_t input[]      = { 0x3392 };
234     static const uint32_t decomposed[] = { 0x004D, 0x0048, 0x007A };
235     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
236     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
237   }
238
239   { /* VULGAR FRACTION THREE EIGHTHS */
240     static const uint32_t input[]      = { 0x215C };
241     static const uint32_t decomposed[] = { 0x0033, 0x2044, 0x0038 };
242     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
243     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
244   }
245
246   { /* MICRO SIGN */
247     static const uint32_t input[]      = { 0x00B5 };
248     static const uint32_t decomposed[] = { 0x03BC };
249     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
250     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
251   }
252
253   { /* ARABIC LIGATURE SALLALLAHOU ALAYHE WASALLAM */
254     static const uint32_t input[]      = { 0xFDFA };
255     static const uint32_t decomposed[] =
256       { 0x0635, 0x0644, 0x0649, 0x0020, 0x0627, 0x0644, 0x0644, 0x0647, 0x0020,
257         0x0639, 0x0644, 0x064A, 0x0647, 0x0020, 0x0648, 0x0633, 0x0644, 0x0645
258       };
259     ASSERT (check (input, SIZEOF (input),           decomposed, SIZEOF (decomposed)) == 0);
260     ASSERT (check (decomposed, SIZEOF (decomposed), decomposed, SIZEOF (decomposed)) == 0);
261   }
262
263   { /* HANGUL SYLLABLE GEUL */
264     static const uint32_t input[]      = { 0xAE00 };
265     static const uint32_t decomposed[] = { 0x1100, 0x1173, 0x11AF };
266     ASSERT (check (input, SIZEOF (input),           input, SIZEOF (input)) == 0);
267     ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
268   }
269
270   { /* HANGUL SYLLABLE GEU */
271     static const uint32_t input[]      = { 0xADF8 };
272     static const uint32_t decomposed[] = { 0x1100, 0x1173 };
273     ASSERT (check (input, SIZEOF (input),           input, SIZEOF (input)) == 0);
274     ASSERT (check (decomposed, SIZEOF (decomposed), input, SIZEOF (input)) == 0);
275   }
276
277   { /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a)  日本語,中文,한글" */
278     static const uint32_t input[] =
279       { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
280         0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
281         0x0439, 0x0442, 0x0435, '!', ' ',
282         'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
283         '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
284         0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, '\n'
285       };
286     static const uint32_t decomposed[] =
287       { 'G', 'r', 0x0075, 0x0308, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
288         0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
289         0x0438, 0x0306, 0x0442, 0x0435, '!', ' ',
290         'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x0032,
291         '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
292         0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',',
293         0x1112, 0x1161, 0x11AB, 0x1100, 0x1173, 0x11AF, '\n'
294       };
295     static const uint32_t expected[] =
296       { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
297         0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
298         0x0439, 0x0442, 0x0435, '!', ' ',
299         'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x0032,
300         '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
301         0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, '\n'
302       };
303     ASSERT (check (input, SIZEOF (input),           expected, SIZEOF (expected)) == 0);
304     ASSERT (check (decomposed, SIZEOF (decomposed), expected, SIZEOF (expected)) == 0);
305     ASSERT (check (expected, SIZEOF (expected),     expected, SIZEOF (expected)) == 0);
306   }
307
308 #if HAVE_DECL_ALARM
309   /* Declare failure if test takes too long, by using default abort
310      caused by SIGALRM.  */
311   signal (SIGALRM, SIG_DFL);
312   alarm (50);
313 #endif
314
315   /* Check that the sorting is not O(n²) but O(n log n).  */
316   {
317     int pass;
318     for (pass = 0; pass < 3; pass++)
319       {
320         size_t repeat = 1;
321         size_t m = 100000;
322         uint32_t *input = (uint32_t *) malloc (2 * m * sizeof (uint32_t));
323         if (input != NULL)
324           {
325             uint32_t *expected = input + m;
326             size_t m1 = m / 2;
327             size_t m2 = (m - 1) / 2;
328             /* NB: m1 + m2 == m - 1.  */
329             uint32_t *p;
330             size_t i;
331
332             input[0] = 0x0041;
333             p = input + 1;
334             switch (pass)
335               {
336               case 0:
337                 for (i = 0; i < m1; i++)
338                   *p++ = 0x0319;
339                 for (i = 0; i < m2; i++)
340                   *p++ = 0x0300;
341                 break;
342
343               case 1:
344                 for (i = 0; i < m2; i++)
345                   *p++ = 0x0300;
346                 for (i = 0; i < m1; i++)
347                   *p++ = 0x0319;
348                 break;
349
350               case 2:
351                 for (i = 0; i < m2; i++)
352                   {
353                     *p++ = 0x0319;
354                     *p++ = 0x0300;
355                   }
356                 for (; i < m1; i++)
357                   *p++ = 0x0319;
358                 break;
359
360               default:
361                 abort ();
362               }
363
364             expected[0] = 0x00C0;
365             p = expected + 1;
366             for (i = 0; i < m1; i++)
367               *p++ = 0x0319;
368             for (i = 0; i < m2 - 1; i++)
369               *p++ = 0x0300;
370
371             for (; repeat > 0; repeat--)
372               {
373                 ASSERT (check (input, m,        expected, m - 1) == 0);
374                 ASSERT (check (expected, m - 1, expected, m - 1) == 0);
375               }
376
377             free (input);
378           }
379       }
380   }
381 }
382
383 #else
384
385 void
386 test_u32_nfkc (void)
387 {
388 }
389
390 #endif