1 /* Test of case-insensitive memory area comparison function.
2 Copyright (C) 2007-2009 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
21 #include "mbmemcasecmp.h"
29 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
47 ASSERT (mbmemcasecmp (NULL, 0, NULL, 0) == 0);
50 static const char input[] = { 'x', 'y' };
52 ASSERT (mbmemcasecmp (input, SIZEOF (input), NULL, 0) > 0);
54 ASSERT (mbmemcasecmp (NULL, 0, input, SIZEOF (input)) < 0);
56 ASSERT (mbmemcasecmp (input, SIZEOF (input), input, SIZEOF (input)) == 0);
59 /* Normal lexicographic order. */
61 static const char input1[] = { 'A', 'm', 'e', 'r', 'i', 'c', 'a' };
62 static const char input2[] = { 'A', 'm', 'i', 'g', 'o' };
64 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) < 0);
66 ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) > 0);
69 /* Shorter and longer strings. */
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' };
74 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) < 0);
76 ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) > 0);
79 ASSERT (mbmemcasecmp ("paragraph", 9, "Paragraph", 9) == 0);
81 ASSERT (mbmemcasecmp ("paragrapH", 9, "parAgRaph", 9) == 0);
83 ASSERT (mbmemcasecmp ("paragraph", 9, "paraLyzed", 9) < 0);
84 ASSERT (mbmemcasecmp ("paraLyzed", 9, "paragraph", 9) > 0);
86 ASSERT (mbmemcasecmp ("para", 4, "paragraph", 9) < 0);
87 ASSERT (mbmemcasecmp ("paragraph", 9, "para", 4) > 0);
91 test_iso_8859_1 (void)
93 #if ! defined __osf__ /* This test fails on OSF/1 5.1. */
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' };
101 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
103 ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) == 0);
105 ASSERT (mbmemcasecmp (input3, SIZEOF (input3), input4, SIZEOF (input4)) == 0);
107 ASSERT (mbmemcasecmp (input4, SIZEOF (input4), input3, SIZEOF (input3)) == 0);
109 ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) < 0);
111 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input4, SIZEOF (input4)) < 0);
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. */
117 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input5, SIZEOF (input5)) > 0);
119 ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input5, SIZEOF (input5)) > 0);
123 #if 0 /* This functionality requires ulc_casecmp. */
124 /* Uppercasing can increase the number of Unicode characters. */
126 static const char input1[] = { 0x68, 0x65, 0x69, 0xDF };
127 static const char input2[] = { 0x68, 0x65, 0x69, 0x73, 0x73 };
129 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
135 test_utf_8 (bool turkish)
137 /* The following tests shows how mbmemcasecmp() is different from
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 */
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);
147 #if 0 /* This functionality requires ulc_casecmp. */
148 /* Normalization effects. */
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' };
156 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
158 ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) == 0);
160 ASSERT (mbmemcasecmp (input3, SIZEOF (input3), input4, SIZEOF (input4)) == 0);
162 ASSERT (mbmemcasecmp (input4, SIZEOF (input4), input3, SIZEOF (input3)) == 0);
164 ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) < 0);
166 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input4, SIZEOF (input4)) < 0);
168 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input5, SIZEOF (input5)) < 0);
170 ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input5, SIZEOF (input5)) < 0);
172 { /* LATIN CAPITAL LETTER A WITH DIAERESIS */
173 static const char input1[] = { 0xC3, 0x84 };
174 static const char input2[] = { 0x41, 0xCC, 0x88 };
176 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
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 };
182 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
184 { /* GREEK DIALYTIKA AND PERISPOMENI */
185 static const char input1[] = { 0xE1, 0xBF, 0x81 };
186 static const char input2[] = { 0xC2, 0xA8, 0xCD, 0x82 };
188 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
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 };
195 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
197 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input3, SIZEOF (input3)) == 0);
199 { /* HANGUL SYLLABLE GEU */
200 static const char input1[] = { 0xEA, 0xB7, 0xB8 };
201 static const char input2[] = { 0xE1, 0x84, 0x80, 0xE1, 0x85, 0xB3 };
203 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
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'
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'
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'
242 #if 0 /* This functionality requires ulc_casecmp. */
243 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
245 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input3, SIZEOF (input3)) == 0);
248 ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) == 0);
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 };
258 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
260 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input3, SIZEOF (input3)) == 0);
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 };
266 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
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 };
277 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
279 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded_tr, SIZEOF (casefolded_tr)) == 0);
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 };
287 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
289 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded_tr, SIZEOF (casefolded_tr)) == 0);
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 };
300 #if 0 /* This functionality requires ulc_casecmp. */
301 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
305 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded_tr, SIZEOF (casefolded_tr)) == 0);
307 { /* LATIN SMALL LETTER DOTLESS I */
308 static const char input[] = { 0xC4, 0xB1 };
309 static const char casefolded[] = { 0x49 };
312 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) > 0);
314 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
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 };
323 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) < 0);
325 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
328 #if 0 /* This functionality requires ulc_casecmp. */
329 /* Uppercasing can increase the number of Unicode characters. */
331 static const char input1[] = { 0x68, 0x65, 0x69, 0xC3, 0x9F };
332 static const char input2[] = { 0x68, 0x65, 0x69, 0x73, 0x73 };
334 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
338 /* Case mappings for some characters can depend on the surrounding characters. */
339 { /* "περισσότερες πληροφορίες" */
340 static const char input1[] =
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
347 static const char input2[] =
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
354 static const char input3[] =
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
364 #if 0 /* This functionality requires ulc_casecmp. */
365 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
367 ASSERT (mbmemcasecmp (input1, SIZEOF (input1), input3, SIZEOF (input3)) == 0);
370 ASSERT (mbmemcasecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) == 0);
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 };
380 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
382 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded_decomposed, SIZEOF (casefolded_decomposed)) != 0);
384 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
386 ASSERT (mbmemcasecmp (input, SIZEOF (input), casefolded_decomposed, SIZEOF (casefolded_decomposed)) == 0);
392 main (int argc, char *argv[])
394 /* configure should already have checked that the locale is supported. */
395 if (setlocale (LC_ALL, "") == NULL)
404 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
409 /* Locale encoding is UTF-8, locale is not Turkish. */
414 /* Locale encoding is UTF-8, locale is Turkish. */