Tests for module 'mbmemcasecmp'.
[pspp] / tests / test-mbmemcasecmp.c
1 /* Test of case-insensitive memory area comparison function.
2    Copyright (C) 2007-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 #include "mbmemcasecmp.h"
22
23 #include <locale.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 static void
43 test_ascii (void)
44 {
45   /* Empty string.  */
46   {
47     ASSERT (mbmemcasecmp (NULL, 0, NULL, 0) == 0);
48   }
49   {
50     static const char input[] = { 'x', 'y' };
51
52     ASSERT (mbmemcasecmp (input, SIZEOF (input), NULL, 0) > 0);
53
54     ASSERT (mbmemcasecmp (NULL, 0, input, SIZEOF (input)) < 0);
55
56     ASSERT (mbmemcasecmp (input, SIZEOF (input), input, SIZEOF (input)) == 0);
57   }
58
59   /* Normal lexicographic order.  */
60   {
61     static const char input1[] = { 'A', 'm', 'e', 'r', 'i', 'c', 'a' };
62     static const char input2[] = { 'A', 'm', 'i', 'g', 'o' };
63
64     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) < 0);
65
66     ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) > 0);
67   }
68
69   /* Shorter and longer strings.  */
70   {
71     static const char input1[] = { 'R', 'e', 'a', 'g', 'a', 'n' };
72     static const char input2[] = { 'R', 'e', 'a', 'g', 'a', 'n', 'o', 'm', 'i', 'c', 's' };
73
74     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) < 0);
75
76     ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) > 0);
77   }
78
79   ASSERT (mbmemcasecmp ("paragraph", 9, "Paragraph", 9) == 0);
80
81   ASSERT (mbmemcasecmp ("paragrapH", 9, "parAgRaph", 9) == 0);
82
83   ASSERT (mbmemcasecmp ("paragraph", 9, "paraLyzed", 9) < 0);
84   ASSERT (mbmemcasecmp ("paraLyzed", 9, "paragraph", 9) > 0);
85
86   ASSERT (mbmemcasecmp ("para", 4, "paragraph", 9) < 0);
87   ASSERT (mbmemcasecmp ("paragraph", 9, "para", 4) > 0);
88 }
89
90 static void
91 test_iso_8859_1 (void)
92 {
93   #if ! defined __osf__ /* This test fails on OSF/1 5.1.  */
94   {
95     static const char input1[] = { 'H', 0xF6, 'h', 'l', 'e' };
96     static const char input2[] = { 'H', 0xD6, 'h', 'L', 'e' };
97     static const char input3[] = { 'H', 0xF6, 'h', 'l', 'e', 'n' };
98     static const char input4[] = { 'H', 0xD6, 'h', 'L', 'e', 'n' };
99     static const char input5[] = { 'H', 'u', 'r', 'z' };
100
101     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
102
103     ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) == 0);
104
105     ASSERT (mbmemcasecmp (input3, SIZEOF (input3), input4, SIZEOF (input4)) == 0);
106
107     ASSERT (mbmemcasecmp (input4, SIZEOF (input4), input3, SIZEOF (input3)) == 0);
108
109     ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) < 0);
110
111     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input4, SIZEOF (input4)) < 0);
112
113     /* These results are the opposite of those in test-ulc-casecmp.c, because
114        mbmemcasecmp compares precomposed characters, whereas ulc_casecmp compares
115        decomposed character sequences.  */
116
117     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input5, SIZEOF (input5)) > 0);
118
119     ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input5, SIZEOF (input5)) > 0);
120   }
121   #endif
122
123   #if 0 /* This functionality requires ulc_casecmp.  */
124   /* Uppercasing can increase the number of Unicode characters.  */
125   { /* "heiß" */
126     static const char input1[] = { 0x68, 0x65, 0x69, 0xDF };
127     static const char input2[] = { 0x68, 0x65, 0x69, 0x73, 0x73 };
128
129     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
130   }
131   #endif
132 }
133
134 static void
135 test_utf_8 (bool turkish)
136 {
137   /* The following tests shows how mbmemcasecmp() is different from
138      strcasecmp().  */
139
140   ASSERT (mbmemcasecmp ("\303\266zg\303\274r", 7, "\303\226ZG\303\234R", 7) == 0); /* özgür */
141   ASSERT (mbmemcasecmp ("\303\226ZG\303\234R", 7, "\303\266zg\303\274r", 7) == 0); /* özgür */
142
143   /* This test shows how strings of different size can compare equal.  */
144   ASSERT (mbmemcasecmp ("turkish", 7, "TURK\304\260SH", 8) == 0);
145   ASSERT (mbmemcasecmp ("TURK\304\260SH", 8, "turkish", 7) == 0);
146
147   #if 0 /* This functionality requires ulc_casecmp.  */
148   /* Normalization effects.  */
149   {
150     static const char input1[] = { 'H', 0xC3, 0xB6, 'h', 'l', 'e' };
151     static const char input2[] = { 'H', 'O', 0xCC, 0x88, 'h', 'L', 'e' };
152     static const char input3[] = { 'H', 0xC3, 0xB6, 'h', 'l', 'e', 'n' };
153     static const char input4[] = { 'H', 'O', 0xCC, 0x88, 'h', 'L', 'e', 'n' };
154     static const char input5[] = { 'H', 'u', 'r', 'z' };
155
156     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
157
158     ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) == 0);
159
160     ASSERT (mbmemcasecmp (input3, SIZEOF (input3), input4, SIZEOF (input4)) == 0);
161
162     ASSERT (mbmemcasecmp (input4, SIZEOF (input4), input3, SIZEOF (input3)) == 0);
163
164     ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) < 0);
165
166     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input4, SIZEOF (input4)) < 0);
167
168     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input5, SIZEOF (input5)) < 0);
169
170     ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input5, SIZEOF (input5)) < 0);
171   }
172   { /* LATIN CAPITAL LETTER A WITH DIAERESIS */
173     static const char input1[] = { 0xC3, 0x84 };
174     static const char input2[] = { 0x41, 0xCC, 0x88 };
175
176     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
177   }
178   { /* LATIN CAPITAL LETTER A WITH DIAERESIS AND MACRON */
179     static const char input1[] = { 0xC7, 0x9E };
180     static const char input2[] = { 0x41, 0xCC, 0x88, 0xCC, 0x84 };
181
182     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
183   }
184   { /* GREEK DIALYTIKA AND PERISPOMENI */
185     static const char input1[] = { 0xE1, 0xBF, 0x81 };
186     static const char input2[] = { 0xC2, 0xA8, 0xCD, 0x82 };
187
188     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
189   }
190   { /* HANGUL SYLLABLE GEUL */
191     static const char input1[] = { 0xEA, 0xB8, 0x80 };
192     static const char input2[] = { 0xEA, 0xB7, 0xB8, 0xE1, 0x86, 0xAF };
193     static const char input3[] = { 0xE1, 0x84, 0x80, 0xE1, 0x85, 0xB3, 0xE1, 0x86, 0xAF };
194
195     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
196
197     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input3, SIZEOF (input3)) == 0);
198   }
199   { /* HANGUL SYLLABLE GEU */
200     static const char input1[] = { 0xEA, 0xB7, 0xB8 };
201     static const char input2[] = { 0xE1, 0x84, 0x80, 0xE1, 0x85, 0xB3 };
202
203     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
204   }
205   #endif
206
207   /* Simple string.  */
208   { /* "Grüß Gott. Здравствуйте! x=(-b±sqrt(b²-4ac))/(2a)  日本語,中文,한글" */
209     static const char input1[] =
210       { 'G', 'r', 0xC3, 0xBC, 0xC3, 0x9F, ' ', 'G', 'o', 't', 't', '.', ' ',
211         0xD0, 0x97, 0xD0, 0xB4, 0xD1, 0x80, 0xD0, 0xB0, 0xD0, 0xB2, 0xD1, 0x81,
212         0xD1, 0x82, 0xD0, 0xB2, 0xD1, 0x83, 0xD0, 0xB9, 0xD1, 0x82, 0xD0, 0xB5,
213         '!', ' ', 'x', '=', '(', '-', 'b', 0xC2, 0xB1, 's', 'q', 'r', 't', '(',
214         'b', 0xC2, 0xB2, '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')',
215         ' ', ' ', 0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E, ',',
216         0xE4, 0xB8, 0xAD, 0xE6, 0x96, 0x87, ',',
217         0xED, 0x95, 0x9C, 0xEA, 0xB8, 0x80, '\n'
218       };
219     static const char input2[] =
220       { 'g', 'r', 0xC3, 0xBC, 0x73, 0x73, ' ', 'g', 'o', 't', 't', '.', ' ',
221         0xD0, 0xB7, 0xD0, 0xB4, 0xD1, 0x80, 0xD0, 0xB0, 0xD0, 0xB2, 0xD1, 0x81,
222         0xD1, 0x82, 0xD0, 0xB2, 0xD1, 0x83, 0xD0, 0xB9, 0xD1, 0x82, 0xD0, 0xB5,
223         '!', ' ', 'x', '=', '(', '-', 'b', 0xC2, 0xB1, 's', 'q', 'r', 't', '(',
224         'b', 0xC2, 0xB2, '-', '4', 'a', 'c', ')', ')', '/', '(', '2', 'a', ')',
225         ' ', ' ', 0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E, ',',
226         0xE4, 0xB8, 0xAD, 0xE6, 0x96, 0x87, ',',
227         0xED, 0x95, 0x9C, 0xEA, 0xB8, 0x80, '\n'
228       };
229     static const char input3[] =
230       { 'G', 'R', 0xC3, 0x9C, 0x53, 0x53, ' ', 'G', 'O', 'T', 'T', '.', ' ',
231         0xD0, 0x97, 0xD0, 0x94, 0xD0, 0xA0, 0xD0, 0x90, 0xD0, 0x92, 0xD0, 0xA1,
232         0xD0, 0xA2, 0xD0, 0x92, 0xD0, 0xA3, 0xD0, 0x99, 0xD0, 0xA2, 0xD0, 0x95,
233         '!', ' ', 'X', '=', '(', '-', 'B', 0xC2, 0xB1, 'S', 'Q', 'R', 'T', '(',
234         'B', 0xC2, 0xB2, '-', '4', 'A', 'C', ')', ')', '/', '(', '2', 'A', ')',
235         ' ', ' ', 0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E, ',',
236         0xE4, 0xB8, 0xAD, 0xE6, 0x96, 0x87, ',',
237         0xED, 0x95, 0x9C, 0xEA, 0xB8, 0x80, '\n'
238       };
239
240     (void) input1;
241
242     #if 0 /* This functionality requires ulc_casecmp.  */
243     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
244
245     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input3, SIZEOF (input3)) == 0);
246     #endif
247
248     ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) == 0);
249   }
250
251   #if 0 /* This functionality requires ulc_casecmp.  */
252   /* Case mapping can increase the number of Unicode characters.  */
253   { /* LATIN SMALL LETTER N PRECEDED BY APOSTROPHE */
254     static const char input1[] = { 0xC5, 0x89 };
255     static const char input2[] = { 0xCA, 0xBC, 0x6E };
256     static const char input3[] = { 0xCA, 0xBC, 0x4E };
257
258     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
259
260     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input3, SIZEOF (input3)) == 0);
261   }
262   { /* GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS */
263     static const char input1[] = { 0xCE, 0x90 };
264     static const char input2[] = { 0xCE, 0xB9, 0xCC, 0x88, 0xCC, 0x81 };
265
266     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
267   }
268   #endif
269
270   /* Turkish letters i İ ı I */
271   { /* LATIN CAPITAL LETTER I */
272     static const char input[]         = { 0x49 };
273     static const char casefolded[]    = { 0x69 };
274     static const char casefolded_tr[] = { 0xC4, 0xB1 };
275
276     if (!turkish)
277       ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
278     else
279       ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded_tr, SIZEOF (casefolded_tr)) == 0);
280   }
281   { /* LATIN SMALL LETTER I */
282     static const char input[]         = { 0x69 };
283     static const char casefolded[]    = { 0x49 };
284     static const char casefolded_tr[] = { 0xC4, 0xB0 };
285
286     if (!turkish)
287       ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
288     else
289       ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded_tr, SIZEOF (casefolded_tr)) == 0);
290   }
291   { /* LATIN CAPITAL LETTER I WITH DOT ABOVE */
292     static const char input[]         = { 0xC4, 0xB0 };
293     static const char casefolded[]    = { 0x69, 0xCC, 0x87 };
294     static const char casefolded_tr[] = { 0x69 };
295
296     (void) casefolded;
297
298     if (!turkish)
299       {
300         #if 0 /* This functionality requires ulc_casecmp.  */
301         ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
302         #endif
303       }
304     else
305       ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded_tr, SIZEOF (casefolded_tr)) == 0);
306   }
307   { /* LATIN SMALL LETTER DOTLESS I */
308     static const char input[]      = { 0xC4, 0xB1 };
309     static const char casefolded[] = { 0x49 };
310
311     if (!turkish)
312       ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) > 0);
313     else
314       ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
315   }
316   { /* "topkapı" */
317     static const char input[] =
318       { 0x54, 0x4F, 0x50, 0x4B, 0x41, 0x50, 0x49 };
319     static const char casefolded[] =
320       { 0x74, 0x6F, 0x70, 0x6B, 0x61, 0x70, 0xC4, 0xB1 };
321
322     if (!turkish)
323       ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) < 0);
324     else
325       ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
326   }
327
328   #if 0 /* This functionality requires ulc_casecmp.  */
329   /* Uppercasing can increase the number of Unicode characters.  */
330   { /* "heiß" */
331     static const char input1[] = { 0x68, 0x65, 0x69, 0xC3, 0x9F };
332     static const char input2[] = { 0x68, 0x65, 0x69, 0x73, 0x73 };
333
334     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
335   }
336   #endif
337
338   /* Case mappings for some characters can depend on the surrounding characters.  */
339   { /* "περισσότερες πληροφορίες" */
340     static const char input1[] =
341       {
342         0xCF, 0x80, 0xCE, 0xB5, 0xCF, 0x81, 0xCE, 0xB9, 0xCF, 0x83, 0xCF, 0x83,
343         0xCF, 0x8C, 0xCF, 0x84, 0xCE, 0xB5, 0xCF, 0x81, 0xCE, 0xB5, 0xCF, 0x82,
344         ' ', 0xCF, 0x80, 0xCE, 0xBB, 0xCE, 0xB7, 0xCF, 0x81, 0xCE, 0xBF,
345         0xCF, 0x86, 0xCE, 0xBF, 0xCF, 0x81, 0xCE, 0xAF, 0xCE, 0xB5, 0xCF, 0x82
346       };
347     static const char input2[] =
348       {
349         0xCF, 0x80, 0xCE, 0xB5, 0xCF, 0x81, 0xCE, 0xB9, 0xCF, 0x83, 0xCF, 0x83,
350         0xCF, 0x8C, 0xCF, 0x84, 0xCE, 0xB5, 0xCF, 0x81, 0xCE, 0xB5, 0xCF, 0x83,
351         ' ', 0xCF, 0x80, 0xCE, 0xBB, 0xCE, 0xB7, 0xCF, 0x81, 0xCE, 0xBF,
352         0xCF, 0x86, 0xCE, 0xBF, 0xCF, 0x81, 0xCE, 0xAF, 0xCE, 0xB5, 0xCF, 0x83
353       };
354     static const char input3[] =
355       {
356         0xCE, 0xA0, 0xCE, 0x95, 0xCE, 0xA1, 0xCE, 0x99, 0xCE, 0xA3, 0xCE, 0xA3,
357         0xCE, 0x8C, 0xCE, 0xA4, 0xCE, 0x95, 0xCE, 0xA1, 0xCE, 0x95, 0xCE, 0xA3,
358         ' ', 0xCE, 0xA0, 0xCE, 0x9B, 0xCE, 0x97, 0xCE, 0xA1, 0xCE, 0x9F,
359         0xCE, 0xA6, 0xCE, 0x9F, 0xCE, 0xA1, 0xCE, 0x8A, 0xCE, 0x95, 0xCE, 0xA3
360       };
361
362     (void) input1;
363
364     #if 0 /* This functionality requires ulc_casecmp.  */
365     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
366
367     ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input3, SIZEOF (input3)) == 0);
368     #endif
369
370     ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) == 0);
371   }
372
373   #if 0 /* This functionality requires ulc_casecmp.  */
374   /* Case mapping can require subsequent normalization.  */
375   { /* LATIN SMALL LETTER J WITH CARON, COMBINING DOT BELOW */
376     static const char input[]                 = { 0xC7, 0xB0, 0xCC, 0xA3 };
377     static const char casefolded[]            = { 0x6A, 0xCC, 0x8C, 0xCC, 0xA3 };
378     static const char casefolded_decomposed[] = { 0x6A, 0xCC, 0xA3, 0xCC, 0x8C };
379
380     ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
381
382     ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded_decomposed, SIZEOF (casefolded_decomposed)) != 0);
383
384     ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
385
386     ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded_decomposed, SIZEOF (casefolded_decomposed)) == 0);
387   }
388   #endif
389 }
390
391 int
392 main (int argc, char *argv[])
393 {
394   /* configure should already have checked that the locale is supported.  */
395   if (setlocale (LC_ALL, "") == NULL)
396     return 1;
397
398   test_ascii ();
399
400   if (argc > 1)
401     switch (argv[1][0])
402       {
403       case '1':
404         /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
405         test_iso_8859_1 ();
406         return 0;
407
408       case '2':
409         /* Locale encoding is UTF-8, locale is not Turkish.  */
410         test_utf_8 (false);
411         return 0;
412
413       case '3':
414         /* Locale encoding is UTF-8, locale is Turkish.  */
415         test_utf_8 (true);
416         return 0;
417       }
418
419   return 1;
420 }