Update translations.
[glibc.git] / string / test-strcasecmp.c
blob3d994f9d6487823277d1c6f38d645234c1b296a5
1 /* Test and measure strcasecmp functions.
2 Copyright (C) 1999-2022 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 #define TEST_MAIN
22 #define TEST_NAME "strcasecmp"
23 #include "test-string.h"
25 typedef int (*proto_t) (const char *, const char *);
26 static int simple_strcasecmp (const char *, const char *);
27 static int stupid_strcasecmp (const char *, const char *);
29 IMPL (stupid_strcasecmp, 0)
30 IMPL (simple_strcasecmp, 0)
31 IMPL (strcasecmp, 1)
33 static int
34 simple_strcasecmp (const char *s1, const char *s2)
36 int ret;
38 while ((ret = ((unsigned char) tolower (*s1)
39 - (unsigned char) tolower (*s2))) == 0
40 && *s1++)
41 ++s2;
42 return ret;
45 static int
46 stupid_strcasecmp (const char *s1, const char *s2)
48 size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1;
49 size_t n = ns1 < ns2 ? ns1 : ns2;
50 int ret = 0;
52 while (n--)
54 if ((ret = ((unsigned char) tolower (*s1)
55 - (unsigned char) tolower (*s2))) != 0)
56 break;
57 ++s1;
58 ++s2;
60 return ret;
63 static void
64 do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
66 int result = CALL (impl, s1, s2);
67 if ((exp_result == 0 && result != 0)
68 || (exp_result < 0 && result >= 0)
69 || (exp_result > 0 && result <= 0))
71 error (0, 0, "Wrong result in function %s %d %d", impl->name,
72 result, exp_result);
73 ret = 1;
74 return;
78 static void
79 do_test (size_t align1, size_t align2, size_t len, int max_char,
80 int exp_result)
82 size_t i;
83 char *s1, *s2;
85 if (len == 0)
86 return;
88 align1 &= 7;
89 if (align1 + len + 1 >= page_size)
90 return;
92 align2 &= 7;
93 if (align2 + len + 1 >= page_size)
94 return;
96 s1 = (char *) (buf1 + align1);
97 s2 = (char *) (buf2 + align2);
99 for (i = 0; i < len; i++)
101 s1[i] = toupper (1 + 23 * i % max_char);
102 s2[i] = tolower (s1[i]);
105 s1[len] = s2[len] = 0;
106 s1[len + 1] = 23;
107 s2[len + 1] = 24 + exp_result;
108 if ((s2[len - 1] == 'z' && exp_result == -1)
109 || (s2[len - 1] == 'a' && exp_result == 1))
110 s1[len - 1] += exp_result;
111 else
112 s2[len - 1] -= exp_result;
114 FOR_EACH_IMPL (impl, 0)
115 do_one_test (impl, s1, s2, exp_result);
118 static void
119 do_random_tests (void)
121 size_t i, j, n, align1, align2, pos, len1, len2;
122 int result;
123 long r;
124 unsigned char *p1 = buf1 + page_size - 512;
125 unsigned char *p2 = buf2 + page_size - 512;
127 for (n = 0; n < ITERATIONS; n++)
129 align1 = random () & 31;
130 if (random () & 1)
131 align2 = random () & 31;
132 else
133 align2 = align1 + (random () & 24);
134 pos = random () & 511;
135 j = align1 > align2 ? align1 : align2;
136 if (pos + j >= 511)
137 pos = 510 - j - (random () & 7);
138 len1 = random () & 511;
139 if (pos >= len1 && (random () & 1))
140 len1 = pos + (random () & 7);
141 if (len1 + j >= 512)
142 len1 = 511 - j - (random () & 7);
143 if (pos >= len1)
144 len2 = len1;
145 else
146 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
147 j = (pos > len2 ? pos : len2) + align1 + 64;
148 if (j > 512)
149 j = 512;
150 for (i = 0; i < j; ++i)
152 p1[i] = tolower (random () & 255);
153 if (i < len1 + align1 && !p1[i])
155 p1[i] = tolower (random () & 255);
156 if (!p1[i])
157 p1[i] = tolower (1 + (random () & 127));
160 for (i = 0; i < j; ++i)
162 p2[i] = toupper (random () & 255);
163 if (i < len2 + align2 && !p2[i])
165 p2[i] = toupper (random () & 255);
166 if (!p2[i])
167 toupper (p2[i] = 1 + (random () & 127));
171 result = 0;
172 memcpy (p2 + align2, p1 + align1, pos);
173 if (pos < len1)
175 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
177 p2[align2 + pos] = toupper (random () & 255);
178 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
179 p2[align2 + pos] = toupper (p1[align1 + pos]
180 + 3 + (random () & 127));
183 if (p1[align1 + pos] < tolower (p2[align2 + pos]))
184 result = -1;
185 else
186 result = 1;
188 p1[len1 + align1] = 0;
189 p2[len2 + align2] = 0;
191 FOR_EACH_IMPL (impl, 1)
193 r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2));
194 /* Test whether on 64-bit architectures where ABI requires
195 callee to promote has the promotion been done. */
196 asm ("" : "=g" (r) : "0" (r));
197 if ((r == 0 && result)
198 || (r < 0 && result >= 0)
199 || (r > 0 && result <= 0))
201 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
202 n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
203 ret = 1;
209 static void
210 test_locale (const char *locale)
212 size_t i;
214 if (setlocale (LC_CTYPE, locale) == NULL)
216 error (0, 0, "cannot set locale \"%s\"", locale);
217 ret = 1;
220 printf ("%-23s", locale);
221 FOR_EACH_IMPL (impl, 0)
222 printf ("\t%s", impl->name);
223 putchar ('\n');
225 for (i = 1; i < 16; ++i)
227 do_test (i, i, i, 127, 0);
228 do_test (i, i, i, 127, 1);
229 do_test (i, i, i, 127, -1);
232 for (i = 1; i < 10; ++i)
234 do_test (0, 0, 2 << i, 127, 0);
235 do_test (0, 0, 2 << i, 254, 0);
236 do_test (0, 0, 2 << i, 127, 1);
237 do_test (0, 0, 2 << i, 254, 1);
238 do_test (0, 0, 2 << i, 127, -1);
239 do_test (0, 0, 2 << i, 254, -1);
242 for (i = 1; i < 8; ++i)
244 do_test (i, 2 * i, 8 << i, 127, 0);
245 do_test (2 * i, i, 8 << i, 254, 0);
246 do_test (i, 2 * i, 8 << i, 127, 1);
247 do_test (2 * i, i, 8 << i, 254, 1);
248 do_test (i, 2 * i, 8 << i, 127, -1);
249 do_test (2 * i, i, 8 << i, 254, -1);
252 do_random_tests ();
256 test_main (void)
258 test_init ();
260 test_locale ("C");
261 test_locale ("en_US.ISO-8859-1");
262 test_locale ("en_US.UTF-8");
263 test_locale ("tr_TR.ISO-8859-9");
264 test_locale ("tr_TR.UTF-8");
266 return ret;
269 #include <support/test-driver.c>