usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-mbmemcasecmp.h
blobf74aaab1b44153019ae157e8c6bbfe7d3ffeea8f
1 /* Test of case-insensitive memory area comparison function.
2 Copyright (C) 2007-2024 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2009. */
19 static void
20 test_ascii (int (*my_casecmp) (const char *, size_t, const char *, size_t))
22 /* Empty string. */
24 ASSERT (my_casecmp (NULL, 0, NULL, 0) == 0);
27 static const char input[] = { 'x', 'y' };
29 ASSERT (my_casecmp (input, SIZEOF (input), NULL, 0) > 0);
31 ASSERT (my_casecmp (NULL, 0, input, SIZEOF (input)) < 0);
33 ASSERT (my_casecmp (input, SIZEOF (input), input, SIZEOF (input)) == 0);
36 /* Normal lexicographic order. */
38 static const char input1[] = { 'A', 'm', 'e', 'r', 'i', 'c', 'a' };
39 static const char input2[] = { 'A', 'm', 'i', 'g', 'o' };
41 ASSERT (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) < 0);
43 ASSERT (my_casecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) > 0);
46 /* Shorter and longer strings. */
48 static const char input1[] = { 'R', 'e', 'a', 'g', 'a', 'n' };
49 static const char input2[] = { 'R', 'e', 'a', 'g', 'a', 'n', 'o', 'm', 'i', 'c', 's' };
51 ASSERT (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) < 0);
53 ASSERT (my_casecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) > 0);
56 ASSERT (my_casecmp ("paragraph", 9, "Paragraph", 9) == 0);
58 ASSERT (my_casecmp ("paragrapH", 9, "parAgRaph", 9) == 0);
60 ASSERT (my_casecmp ("paragraph", 9, "paraLyzed", 9) < 0);
61 ASSERT (my_casecmp ("paraLyzed", 9, "paragraph", 9) > 0);
63 ASSERT (my_casecmp ("para", 4, "paragraph", 9) < 0);
64 ASSERT (my_casecmp ("paragraph", 9, "para", 4) > 0);
66 /* Embedded NULs. */
67 ASSERT (my_casecmp ("1\0", 2, "2\0", 2) < 0);
68 ASSERT (my_casecmp ("2\0", 2, "1\0", 2) > 0);
69 ASSERT (my_casecmp ("x\0""1", 3, "x\0""2", 3) < 0);
70 ASSERT (my_casecmp ("x\0""2", 3, "x\0""1", 3) > 0);
73 static void
74 test_iso_8859_1 (int (*my_casecmp) (const char *, size_t, const char *, size_t), bool assume_byte_values)
76 #if ! defined __osf__ /* This test fails on OSF/1 5.1. */
78 static const char input1[] = { 'H', 0xF6, 'h', 'l', 'e' };
79 static const char input2[] = { 'H', 0xD6, 'h', 'L', 'e' };
80 static const char input3[] = { 'H', 0xF6, 'h', 'l', 'e', 'n' };
81 static const char input4[] = { 'H', 0xD6, 'h', 'L', 'e', 'n' };
82 static const char input5[] = { 'H', 'u', 'r', 'z' };
84 ASSERT (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
86 ASSERT (my_casecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) == 0);
88 ASSERT (my_casecmp (input3, SIZEOF (input3), input4, SIZEOF (input4)) == 0);
90 ASSERT (my_casecmp (input4, SIZEOF (input4), input3, SIZEOF (input3)) == 0);
92 ASSERT (my_casecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) < 0);
94 ASSERT (my_casecmp (input1, SIZEOF (input1), input4, SIZEOF (input4)) < 0);
96 if (assume_byte_values)
98 /* These results are the opposite of those in test-ulc-casecmp.c,
99 because my_casecmp compares precomposed characters, whereas
100 ulc_casecmp compares decomposed character sequences. */
102 ASSERT (my_casecmp (input1, SIZEOF (input1), input5, SIZEOF (input5)) > 0);
104 ASSERT (my_casecmp (input2, SIZEOF (input2), input5, SIZEOF (input5)) > 0);
107 #endif
109 #if 0 /* This functionality requires ulc_casecmp. */
110 /* Uppercasing can increase the number of Unicode characters. */
111 { /* "heiß" */
112 static const char input1[] = { 0x68, 0x65, 0x69, 0xDF };
113 static const char input2[] = { 0x68, 0x65, 0x69, 0x73, 0x73 };
115 ASSERT (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
117 #endif
120 static void
121 test_utf_8 (int (*my_casecmp) (const char *, size_t, const char *, size_t), bool turkish)
123 /* The following tests shows how my_casecmp() is different from
124 strcasecmp(). */
127 /* Some platforms, e.g. MSVC 14, lack the upper/lower mappings for these
128 wide characters in the *.65001 locales. */
129 mbstate_t state;
130 wchar_t wc;
131 memset (&state, 0, sizeof (mbstate_t));
132 if (mbrtowc (&wc, "\303\274", 2, &state) == 2
133 && towupper (wc) != wc)
135 ASSERT (my_casecmp ("\303\266zg\303\274r", 7, "\303\226ZG\303\234R", 7) == 0); /* özgür */
136 ASSERT (my_casecmp ("\303\226ZG\303\234R", 7, "\303\266zg\303\274r", 7) == 0); /* özgür */
140 /* This test shows how strings of different size can compare equal. */
141 if (turkish)
143 ASSERT (my_casecmp ("turkish", 7, "TURK\304\260SH", 8) == 0);
144 ASSERT (my_casecmp ("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 (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
158 ASSERT (my_casecmp (input2, SIZEOF (input2), input1, SIZEOF (input1)) == 0);
160 ASSERT (my_casecmp (input3, SIZEOF (input3), input4, SIZEOF (input4)) == 0);
162 ASSERT (my_casecmp (input4, SIZEOF (input4), input3, SIZEOF (input3)) == 0);
164 ASSERT (my_casecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) < 0);
166 ASSERT (my_casecmp (input1, SIZEOF (input1), input4, SIZEOF (input4)) < 0);
168 ASSERT (my_casecmp (input1, SIZEOF (input1), input5, SIZEOF (input5)) < 0);
170 ASSERT (my_casecmp (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 (my_casecmp (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 (my_casecmp (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 (my_casecmp (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 (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
197 ASSERT (my_casecmp (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 (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
205 #endif
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'
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'
240 (void) input1;
242 #if 0 /* This functionality requires ulc_casecmp. */
243 ASSERT (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
245 ASSERT (my_casecmp (input1, SIZEOF (input1), input3, SIZEOF (input3)) == 0);
246 #endif
249 /* Some platforms, e.g. MSVC 14, lack the upper/lower mappings for the
250 'ü'/'Ü' wide characters in the *.65001 locales. */
251 mbstate_t state;
252 wchar_t wc;
253 memset (&state, 0, sizeof (mbstate_t));
254 if (mbrtowc (&wc, "\303\234", 2, &state) == 2
255 && towlower (wc) != wc)
257 ASSERT (my_casecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) == 0);
262 #if 0 /* This functionality requires ulc_casecmp. */
263 /* Case mapping can increase the number of Unicode characters. */
264 { /* LATIN SMALL LETTER N PRECEDED BY APOSTROPHE */
265 static const char input1[] = { 0xC5, 0x89 };
266 static const char input2[] = { 0xCA, 0xBC, 0x6E };
267 static const char input3[] = { 0xCA, 0xBC, 0x4E };
269 ASSERT (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
271 ASSERT (my_casecmp (input1, SIZEOF (input1), input3, SIZEOF (input3)) == 0);
273 { /* GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS */
274 static const char input1[] = { 0xCE, 0x90 };
275 static const char input2[] = { 0xCE, 0xB9, 0xCC, 0x88, 0xCC, 0x81 };
277 ASSERT (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
279 #endif
281 /* Turkish letters i İ ı I */
282 { /* LATIN CAPITAL LETTER I */
283 static const char input[] = { 0x49 };
284 static const char casefolded[] = { 0x69 };
285 static const char casefolded_tr[] = { 0xC4, 0xB1 };
287 if (!turkish)
288 ASSERT (my_casecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
289 else
290 ASSERT (my_casecmp (input, SIZEOF (input), casefolded_tr, SIZEOF (casefolded_tr)) == 0);
292 { /* LATIN SMALL LETTER I */
293 static const char input[] = { 0x69 };
294 static const char casefolded[] = { 0x49 };
295 static const char casefolded_tr[] = { 0xC4, 0xB0 };
297 if (!turkish)
298 ASSERT (my_casecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
299 else
300 ASSERT (my_casecmp (input, SIZEOF (input), casefolded_tr, SIZEOF (casefolded_tr)) == 0);
302 { /* LATIN CAPITAL LETTER I WITH DOT ABOVE */
303 static const char input[] = { 0xC4, 0xB0 };
304 static const char casefolded[] = { 0x69, 0xCC, 0x87 };
305 static const char casefolded_tr[] = { 0x69 };
307 (void) casefolded;
309 if (!turkish)
311 #if 0 /* This functionality requires ulc_casecmp. */
312 ASSERT (my_casecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
313 #endif
315 else
316 ASSERT (my_casecmp (input, SIZEOF (input), casefolded_tr, SIZEOF (casefolded_tr)) == 0);
318 { /* LATIN SMALL LETTER DOTLESS I */
319 static const char input[] = { 0xC4, 0xB1 };
320 static const char casefolded[] = { 0x49 };
322 if (!turkish)
323 ASSERT (my_casecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) > 0);
324 else
325 ASSERT (my_casecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
327 { /* "topkapı" */
328 static const char input[] =
329 { 0x54, 0x4F, 0x50, 0x4B, 0x41, 0x50, 0x49 };
330 static const char casefolded[] =
331 { 0x74, 0x6F, 0x70, 0x6B, 0x61, 0x70, 0xC4, 0xB1 };
333 if (!turkish)
334 ASSERT (my_casecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) < 0);
335 else
336 ASSERT (my_casecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
339 #if 0 /* This functionality requires ulc_casecmp. */
340 /* Uppercasing can increase the number of Unicode characters. */
341 { /* "heiß" */
342 static const char input1[] = { 0x68, 0x65, 0x69, 0xC3, 0x9F };
343 static const char input2[] = { 0x68, 0x65, 0x69, 0x73, 0x73 };
345 ASSERT (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
347 #endif
349 /* Case mappings for some characters can depend on the surrounding characters. */
350 { /* "περισσότερες πληροφορίες" */
351 static const char input1[] =
353 0xCF, 0x80, 0xCE, 0xB5, 0xCF, 0x81, 0xCE, 0xB9, 0xCF, 0x83, 0xCF, 0x83,
354 0xCF, 0x8C, 0xCF, 0x84, 0xCE, 0xB5, 0xCF, 0x81, 0xCE, 0xB5, 0xCF, 0x82,
355 ' ', 0xCF, 0x80, 0xCE, 0xBB, 0xCE, 0xB7, 0xCF, 0x81, 0xCE, 0xBF,
356 0xCF, 0x86, 0xCE, 0xBF, 0xCF, 0x81, 0xCE, 0xAF, 0xCE, 0xB5, 0xCF, 0x82
358 static const char input2[] =
360 0xCF, 0x80, 0xCE, 0xB5, 0xCF, 0x81, 0xCE, 0xB9, 0xCF, 0x83, 0xCF, 0x83,
361 0xCF, 0x8C, 0xCF, 0x84, 0xCE, 0xB5, 0xCF, 0x81, 0xCE, 0xB5, 0xCF, 0x83,
362 ' ', 0xCF, 0x80, 0xCE, 0xBB, 0xCE, 0xB7, 0xCF, 0x81, 0xCE, 0xBF,
363 0xCF, 0x86, 0xCE, 0xBF, 0xCF, 0x81, 0xCE, 0xAF, 0xCE, 0xB5, 0xCF, 0x83
365 static const char input3[] =
367 0xCE, 0xA0, 0xCE, 0x95, 0xCE, 0xA1, 0xCE, 0x99, 0xCE, 0xA3, 0xCE, 0xA3,
368 0xCE, 0x8C, 0xCE, 0xA4, 0xCE, 0x95, 0xCE, 0xA1, 0xCE, 0x95, 0xCE, 0xA3,
369 ' ', 0xCE, 0xA0, 0xCE, 0x9B, 0xCE, 0x97, 0xCE, 0xA1, 0xCE, 0x9F,
370 0xCE, 0xA6, 0xCE, 0x9F, 0xCE, 0xA1, 0xCE, 0x8A, 0xCE, 0x95, 0xCE, 0xA3
373 (void) input1;
375 #if 0 /* This functionality requires ulc_casecmp. */
376 ASSERT (my_casecmp (input1, SIZEOF (input1), input2, SIZEOF (input2)) == 0);
378 ASSERT (my_casecmp (input1, SIZEOF (input1), input3, SIZEOF (input3)) == 0);
379 #endif
381 ASSERT (my_casecmp (input2, SIZEOF (input2), input3, SIZEOF (input3)) == 0);
384 #if 0 /* This functionality requires ulc_casecmp. */
385 /* Case mapping can require subsequent normalization. */
386 { /* LATIN SMALL LETTER J WITH CARON, COMBINING DOT BELOW */
387 static const char input[] = { 0xC7, 0xB0, 0xCC, 0xA3 };
388 static const char casefolded[] = { 0x6A, 0xCC, 0x8C, 0xCC, 0xA3 };
389 static const char casefolded_decomposed[] = { 0x6A, 0xCC, 0xA3, 0xCC, 0x8C };
391 ASSERT (my_casecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
393 ASSERT (my_casecmp (input, SIZEOF (input), casefolded_decomposed, SIZEOF (casefolded_decomposed)) != 0);
395 ASSERT (my_casecmp (input, SIZEOF (input), casefolded, SIZEOF (casefolded)) == 0);
397 ASSERT (my_casecmp (input, SIZEOF (input), casefolded_decomposed, SIZEOF (casefolded_decomposed)) == 0);
399 #endif