Handle empty strings correctly.
[gnulib.git] / tests / uninorm / test-u16-nfd.c
1 /* Test of canonical decomposition of UTF-16 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_U16_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 uint16_t *input, size_t input_length,
47        const uint16_t *expected, size_t expected_length)
48 {
49   size_t length;
50   uint16_t *result;
51
52   /* Test return conventions with resultbuf == NULL.  */
53   result = u16_normalize (UNINORM_NFD, input, input_length, NULL, &length);
54   if (!(result != NULL))
55     return 1;
56   if (!(length == expected_length))
57     return 2;
58   if (!(u16_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       uint16_t *preallocated;
66
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))
71         return 4;
72       if (!(result != preallocated))
73         return 5;
74       if (!(length == expected_length))
75         return 6;
76       if (!(u16_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     uint16_t *preallocated;
85
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))
90       return 8;
91     if (!(result == preallocated))
92       return 9;
93     if (!(length == expected_length))
94       return 10;
95     if (!(u16_cmp (result, expected, expected_length) == 0))
96       return 11;
97     free (preallocated);
98   }
99
100   return 0;
101 }
102
103 void
104 test_u16_nfd (void)
105 {
106   { /* Empty string.  */
107     ASSERT (check (NULL, 0, NULL, 0) == 0);
108   }
109   { /* SPACE */
110     static const uint16_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 uint16_t input[]    = { 0x00C4 };
116     static const uint16_t expected[] = { 0x0041, 0x0308 };
117     ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
118   }
119
120   { /* LATIN CAPITAL LETTER A WITH DIAERESIS AND MACRON */
121     static const uint16_t input[]    = { 0x01DE };
122     static const uint16_t expected[] = { 0x0041, 0x0308, 0x0304 };
123     ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
124   }
125
126   { /* GREEK DIALYTIKA AND PERISPOMENI */
127     static const uint16_t input[]    = { 0x1FC1 };
128     static const uint16_t expected[] = { 0x00A8, 0x0342 };
129     ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
130   }
131
132   { /* SCRIPT SMALL L */
133     static const uint16_t input[]    = { 0x2113 };
134     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
135   }
136
137   { /* NO-BREAK SPACE */
138     static const uint16_t input[]    = { 0x00A0 };
139     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
140   }
141
142   { /* ARABIC LETTER VEH INITIAL FORM */
143     static const uint16_t input[]    = { 0xFB6C };
144     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
145   }
146
147   { /* ARABIC LETTER VEH MEDIAL FORM */
148     static const uint16_t input[]    = { 0xFB6D };
149     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
150   }
151
152   { /* ARABIC LETTER VEH FINAL FORM */
153     static const uint16_t input[]    = { 0xFB6B };
154     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
155   }
156
157   { /* ARABIC LETTER VEH ISOLATED FORM */
158     static const uint16_t input[]    = { 0xFB6A };
159     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
160   }
161
162   { /* CIRCLED NUMBER FIFTEEN */
163     static const uint16_t input[]    = { 0x246E };
164     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
165   }
166
167   { /* TRADE MARK SIGN */
168     static const uint16_t input[]    = { 0x2122 };
169     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
170   }
171
172   { /* LATIN SUBSCRIPT SMALL LETTER I */
173     static const uint16_t input[]    = { 0x1D62 };
174     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
175   }
176
177   { /* PRESENTATION FORM FOR VERTICAL LEFT PARENTHESIS */
178     static const uint16_t input[]    = { 0xFE35 };
179     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
180   }
181
182   { /* FULLWIDTH LATIN CAPITAL LETTER A */
183     static const uint16_t input[]    = { 0xFF21 };
184     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
185   }
186
187   { /* HALFWIDTH IDEOGRAPHIC COMMA */
188     static const uint16_t input[]    = { 0xFF64 };
189     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
190   }
191
192   { /* SMALL IDEOGRAPHIC COMMA */
193     static const uint16_t input[]    = { 0xFE51 };
194     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
195   }
196
197   { /* SQUARE MHZ */
198     static const uint16_t input[]    = { 0x3392 };
199     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
200   }
201
202   { /* VULGAR FRACTION THREE EIGHTHS */
203     static const uint16_t input[]    = { 0x215C };
204     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
205   }
206
207   { /* MICRO SIGN */
208     static const uint16_t input[]    = { 0x00B5 };
209     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
210   }
211
212   { /* ARABIC LIGATURE SALLALLAHOU ALAYHE WASALLAM */
213     static const uint16_t input[]    = { 0xFDFA };
214     ASSERT (check (input, SIZEOF (input), input, SIZEOF (input)) == 0);
215   }
216
217   { /* HANGUL SYLLABLE GEUL */
218     static const uint16_t input[]    = { 0xAE00 };
219     static const uint16_t expected[] = { 0x1100, 0x1173, 0x11AF };
220     ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
221   }
222
223   { /* HANGUL SYLLABLE GEU */
224     static const uint16_t input[]    = { 0xADF8 };
225     static const uint16_t expected[] = { 0x1100, 0x1173 };
226     ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
227   }
228
229   { /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a)  日本語,中文,한글" */
230     static const uint16_t input[] =
231       { 'G', 'r', 0x00FC, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
232         0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
233         0x0439, 0x0442, 0x0435, '!', ' ',
234         'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
235         '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
236         0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',', 0xD55C, 0xAE00, '\n'
237       };
238     static const uint16_t expected[] =
239       { 'G', 'r', 0x0075, 0x0308, 0x00DF, ' ', 'G', 'o', 't', 't', '.', ' ',
240         0x0417, 0x0434, 0x0440, 0x0430, 0x0432, 0x0441, 0x0442, 0x0432, 0x0443,
241         0x0438, 0x0306, 0x0442, 0x0435, '!', ' ',
242         'x', '=', '(', '-', 'b', 0x00B1, 's', 'q', 'r', 't', '(', 'b', 0x00B2,
243         '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')', ' ', ' ',
244         0x65E5, 0x672C, 0x8A9E, ',', 0x4E2D, 0x6587, ',',
245         0x1112, 0x1161, 0x11AB, 0x1100, 0x1173, 0x11AF, '\n'
246       };
247     ASSERT (check (input, SIZEOF (input), expected, SIZEOF (expected)) == 0);
248   }
249
250 #if HAVE_DECL_ALARM
251   /* Declare failure if test takes too long, by using default abort
252      caused by SIGALRM.  */
253   signal (SIGALRM, SIG_DFL);
254   alarm (50);
255 #endif
256
257   /* Check that the sorting is not O(n²) but O(n log n).  */
258   {
259     int pass;
260     for (pass = 0; pass < 3; pass++)
261       {
262         size_t repeat = 1;
263         size_t m = 100000;
264         uint16_t *input = (uint16_t *) malloc (2 * m * sizeof (uint16_t));
265         if (input != NULL)
266           {
267             uint16_t *expected = input + m;
268             size_t m1 = m / 2;
269             size_t m2 = (m - 1) / 2;
270             /* NB: m1 + m2 == m - 1.  */
271             uint16_t *p;
272             size_t i;
273
274             input[0] = 0x0041;
275             p = input + 1;
276             switch (pass)
277               {
278               case 0:
279                 for (i = 0; i < m1; i++)
280                   *p++ = 0x0319;
281                 for (i = 0; i < m2; i++)
282                   *p++ = 0x0300;
283                 break;
284
285               case 1:
286                 for (i = 0; i < m2; i++)
287                   *p++ = 0x0300;
288                 for (i = 0; i < m1; i++)
289                   *p++ = 0x0319;
290                 break;
291
292               case 2:
293                 for (i = 0; i < m2; i++)
294                   {
295                     *p++ = 0x0319;
296                     *p++ = 0x0300;
297                   }
298                 for (; i < m1; i++)
299                   *p++ = 0x0319;
300                 break;
301
302               default:
303                 abort ();
304               }
305
306             expected[0] = 0x0041;
307             p = expected + 1;
308             for (i = 0; i < m1; i++)
309               *p++ = 0x0319;
310             for (i = 0; i < m2; i++)
311               *p++ = 0x0300;
312
313             for (; repeat > 0; repeat--)
314               ASSERT (check (input, m, expected, m) == 0);
315
316             free (input);
317           }
318       }
319   }
320 }
321
322 #else
323
324 void
325 test_u16_nfd (void)
326 {
327 }
328
329 #endif