Parameterize OP_T_THRES from memcopy.h
[glibc.git] / string / test-strcasecmp.c
blob805e8519fb4b03cafd50a31800f8472a9a26e12a
1 /* Test and measure strcasecmp functions.
2 Copyright (C) 1999-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <locale.h>
20 #include <ctype.h>
21 #include <assert.h>
22 #define TEST_LEN (getpagesize () * 3)
23 #define MIN_PAGE_SIZE (TEST_LEN + 2 * getpagesize ())
25 #define TEST_MAIN
26 #define TEST_NAME "strcasecmp"
27 #include "test-string.h"
29 typedef int (*proto_t) (const char *, const char *);
31 IMPL (strcasecmp, 1)
33 /* Naive implementation to verify results. */
34 int
35 simple_strcasecmp (const char *s1, const char *s2)
37 int ret;
39 while ((ret = ((unsigned char) tolower (*s1)
40 - (unsigned char) tolower (*s2))) == 0
41 && *s1++)
42 ++s2;
43 return ret;
46 static void
47 do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
49 int result = CALL (impl, s1, s2);
50 if ((exp_result == 0 && result != 0)
51 || (exp_result < 0 && result >= 0)
52 || (exp_result > 0 && result <= 0))
54 error (0, 0, "Wrong result in function %s %d %d", impl->name,
55 result, exp_result);
56 ret = 1;
57 return;
61 static void
62 do_test (size_t align1, size_t align2, size_t len, int max_char,
63 int exp_result)
65 size_t i;
66 char *s1, *s2;
68 if (len == 0)
69 return;
72 align1 &= getpagesize () - 1;
73 if (align1 + (len + 1) >= page_size)
74 return;
76 align2 &= getpagesize () - 1;
77 if (align2 + (len + 1) >= page_size)
78 return;
80 s1 = (char *) (buf1 + align1);
81 s2 = (char *) (buf2 + align2);
83 for (i = 0; i < len; i++)
85 s1[i] = toupper (1 + 23 * i % max_char);
86 s2[i] = tolower (s1[i]);
89 s1[len] = s2[len] = 0;
90 s1[len + 1] = 23;
91 s2[len + 1] = 24 + exp_result;
93 if ((s2[len - 1] == 'z' && exp_result == -1)
94 || (s2[len - 1] == 'a' && exp_result == 1))
95 s1[len - 1] += exp_result;
96 else if ((s1[len - 1] == 'Z' + 1 && exp_result == 1)
97 || (s1[len - 1] == 'A' - 1 && exp_result == -1))
98 s1[len - 1] = tolower (s2[len - 1]) + exp_result;
99 else
100 s2[len - 1] -= exp_result;
102 /* For some locals this is not guranteed yet. */
103 if (tolower (s1[len - 1]) - tolower (s2[len - 1]) != exp_result)
105 if (exp_result == -1)
107 s1[len - 1] = tolower ('a');
108 s2[len - 1] = toupper (tolower ('a') - 1);
110 else if (exp_result == 0)
111 s1[len - 1] = toupper (s2[len - 1]);
112 else
114 s1[len - 1] = tolower ('a');
115 s2[len - 1] = toupper (tolower ('a') + 1);
119 FOR_EACH_IMPL (impl, 0)
120 do_one_test (impl, s1, s2, exp_result);
123 static void
124 do_random_tests (void)
126 size_t i, j, n, align1, align2, pos, len1, len2;
127 int result;
128 long r;
129 unsigned char *p1 = buf1 + page_size - 512;
130 unsigned char *p2 = buf2 + page_size - 512;
132 for (n = 0; n < ITERATIONS; n++)
134 align1 = random () & 31;
135 if (random () & 1)
136 align2 = random () & 31;
137 else
138 align2 = align1 + (random () & 24);
139 pos = random () & 511;
140 j = align1 > align2 ? align1 : align2;
141 if (pos + j >= 511)
142 pos = 510 - j - (random () & 7);
143 len1 = random () & 511;
144 if (pos >= len1 && (random () & 1))
145 len1 = pos + (random () & 7);
146 if (len1 + j >= 512)
147 len1 = 511 - j - (random () & 7);
148 if (pos >= len1)
149 len2 = len1;
150 else
151 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
152 j = (pos > len2 ? pos : len2) + align1 + 64;
153 if (j > 512)
154 j = 512;
155 for (i = 0; i < j; ++i)
157 p1[i] = tolower (random () & 255);
158 if (i < len1 + align1 && !p1[i])
160 p1[i] = tolower (random () & 255);
161 if (!p1[i])
162 p1[i] = tolower (1 + (random () & 127));
165 for (i = 0; i < j; ++i)
167 p2[i] = toupper (random () & 255);
168 if (i < len2 + align2 && !p2[i])
170 p2[i] = toupper (random () & 255);
171 if (!p2[i])
172 toupper (p2[i] = 1 + (random () & 127));
176 result = 0;
177 memcpy (p2 + align2, p1 + align1, pos);
178 if (pos < len1)
180 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
182 p2[align2 + pos] = toupper (random () & 255);
183 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
184 p2[align2 + pos] = toupper (p1[align1 + pos]
185 + 3 + (random () & 127));
188 if (p1[align1 + pos] < tolower (p2[align2 + pos]))
189 result = -1;
190 else
191 result = 1;
193 p1[len1 + align1] = 0;
194 p2[len2 + align2] = 0;
196 FOR_EACH_IMPL (impl, 1)
198 r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2));
199 /* Test whether on 64-bit architectures where ABI requires
200 callee to promote has the promotion been done. */
201 asm ("" : "=g" (r) : "0" (r));
202 if ((r == 0 && result)
203 || (r < 0 && result >= 0)
204 || (r > 0 && result <= 0))
206 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
207 n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
208 ret = 1;
214 static void
215 test_locale (const char *locale, int extra_tests)
217 size_t i, j, k;
218 const size_t test_len = MIN(TEST_LEN, 3 * 4096);
219 if (setlocale (LC_CTYPE, locale) == NULL)
221 error (0, 0, "cannot set locale \"%s\"", locale);
222 ret = 1;
225 printf ("%-23s", locale);
226 FOR_EACH_IMPL (impl, 0)
227 printf ("\t%s", impl->name);
228 putchar ('\n');
230 for (i = 1; i < 16; ++i)
232 do_test (i, i, i, 127, 0);
233 do_test (i, i, i, 127, 1);
234 do_test (i, i, i, 127, -1);
237 for (i = 1; i < 10; ++i)
239 do_test (0, 0, 2 << i, 127, 0);
240 do_test (0, 0, 2 << i, 254, 0);
241 do_test (0, 0, 2 << i, 127, 1);
242 do_test (0, 0, 2 << i, 254, 1);
243 do_test (0, 0, 2 << i, 127, -1);
244 do_test (0, 0, 2 << i, 254, -1);
247 for (i = 1; i < 8; ++i)
249 do_test (i, 2 * i, 8 << i, 127, 0);
250 do_test (2 * i, i, 8 << i, 254, 0);
251 do_test (i, 2 * i, 8 << i, 127, 1);
252 do_test (2 * i, i, 8 << i, 254, 1);
253 do_test (i, 2 * i, 8 << i, 127, -1);
254 do_test (2 * i, i, 8 << i, 254, -1);
257 for (j = 0; extra_tests && j < 160; ++j)
259 for (i = 0; i < test_len;)
261 do_test (getpagesize () - j - 1, 0, i, 127, 0);
262 do_test (getpagesize () - j - 1, 0, i, 127, 1);
263 do_test (getpagesize () - j - 1, 0, i, 127, -1);
265 do_test (getpagesize () - j - 1, j, i, 127, 0);
266 do_test (getpagesize () - j - 1, j, i, 127, 1);
267 do_test (getpagesize () - j - 1, j, i, 127, -1);
269 do_test (0, getpagesize () - j - 1, i, 127, 0);
270 do_test (0, getpagesize () - j - 1, i, 127, 1);
271 do_test (0, getpagesize () - j - 1, i, 127, -1);
273 do_test (j, getpagesize () - j - 1, i, 127, 0);
274 do_test (j, getpagesize () - j - 1, i, 127, 1);
275 do_test (j, getpagesize () - j - 1, i, 127, -1);
277 for (k = 2; k <= 128; k += k)
279 do_test (getpagesize () - k, getpagesize () - j - 1, i, 127, 0);
280 do_test (getpagesize () - k - 1, getpagesize () - j - 1, i, 127,
282 do_test (getpagesize () - k, getpagesize () - j - 1, i, 127, 1);
283 do_test (getpagesize () - k - 1, getpagesize () - j - 1, i, 127,
285 do_test (getpagesize () - k, getpagesize () - j - 1, i, 127, -1);
286 do_test (getpagesize () - k - 1, getpagesize () - j - 1, i, 127,
287 -1);
290 if (i < 32)
292 i += 1;
294 else if (i < 161)
296 i += 7;
298 else if (i + 161 < test_len)
300 i += 31;
301 i *= 17;
302 i /= 16;
303 if (i + 161 > test_len)
305 i = test_len - 160;
308 else if (i + 32 < test_len)
310 i += 7;
312 else
314 i += 1;
319 do_random_tests ();
323 test_main (void)
325 test_init ();
327 test_locale ("C", 1);
328 test_locale ("en_US.ISO-8859-1", 0);
329 test_locale ("en_US.UTF-8", 0);
330 test_locale ("tr_TR.ISO-8859-9", 0);
331 test_locale ("tr_TR.UTF-8", 0);
333 return ret;
336 #include <support/test-driver.c>