1 /* Test and measure strcasecmp functions.
2    Copyright (C) 1999-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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/>.  */
18 
19 #include <locale.h>
20 #include <ctype.h>
21 #define TEST_MAIN
22 #define TEST_NAME "strcasecmp"
23 #include "test-string.h"
24 
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 *);
28 
29 IMPL (stupid_strcasecmp, 0)
30 IMPL (simple_strcasecmp, 0)
31 IMPL (strcasecmp, 1)
32 
33 static int
simple_strcasecmp(const char * s1,const char * s2)34 simple_strcasecmp (const char *s1, const char *s2)
35 {
36   int ret;
37 
38   while ((ret = ((unsigned char) tolower (*s1)
39 		 - (unsigned char) tolower (*s2))) == 0
40 	 && *s1++)
41     ++s2;
42   return ret;
43 }
44 
45 static int
stupid_strcasecmp(const char * s1,const char * s2)46 stupid_strcasecmp (const char *s1, const char *s2)
47 {
48   size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1;
49   size_t n = ns1 < ns2 ? ns1 : ns2;
50   int ret = 0;
51 
52   while (n--)
53     {
54       if ((ret = ((unsigned char) tolower (*s1)
55 		  - (unsigned char) tolower (*s2))) != 0)
56 	break;
57       ++s1;
58       ++s2;
59     }
60   return ret;
61 }
62 
63 static void
do_one_test(impl_t * impl,const char * s1,const char * s2,int exp_result)64 do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
65 {
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))
70     {
71       error (0, 0, "Wrong result in function %s %d %d", impl->name,
72 	     result, exp_result);
73       ret = 1;
74       return;
75     }
76 }
77 
78 static void
do_test(size_t align1,size_t align2,size_t len,int max_char,int exp_result)79 do_test (size_t align1, size_t align2, size_t len, int max_char,
80 	 int exp_result)
81 {
82   size_t i;
83   char *s1, *s2;
84 
85   if (len == 0)
86     return;
87 
88   align1 &= 7;
89   if (align1 + len + 1 >= page_size)
90     return;
91 
92   align2 &= 7;
93   if (align2 + len + 1 >= page_size)
94     return;
95 
96   s1 = (char *) (buf1 + align1);
97   s2 = (char *) (buf2 + align2);
98 
99   for (i = 0; i < len; i++)
100     {
101       s1[i] = toupper (1 + 23 * i % max_char);
102       s2[i] = tolower (s1[i]);
103     }
104 
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;
113 
114   FOR_EACH_IMPL (impl, 0)
115     do_one_test (impl, s1, s2, exp_result);
116 }
117 
118 static void
do_random_tests(void)119 do_random_tests (void)
120 {
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;
126 
127   for (n = 0; n < ITERATIONS; n++)
128     {
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)
151 	{
152 	  p1[i] = tolower (random () & 255);
153 	  if (i < len1 + align1 && !p1[i])
154 	    {
155 	      p1[i] = tolower (random () & 255);
156 	      if (!p1[i])
157 		p1[i] = tolower (1 + (random () & 127));
158 	    }
159 	}
160       for (i = 0; i < j; ++i)
161 	{
162 	  p2[i] = toupper (random () & 255);
163 	  if (i < len2 + align2 && !p2[i])
164 	    {
165 	      p2[i] = toupper (random () & 255);
166 	      if (!p2[i])
167 		toupper (p2[i] = 1 + (random () & 127));
168 	    }
169 	}
170 
171       result = 0;
172       memcpy (p2 + align2, p1 + align1, pos);
173       if (pos < len1)
174 	{
175 	  if (tolower (p2[align2 + pos]) == p1[align1 + pos])
176 	    {
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));
181 	    }
182 
183 	  if (p1[align1 + pos] < tolower (p2[align2 + pos]))
184 	    result = -1;
185 	  else
186 	    result = 1;
187 	}
188       p1[len1 + align1] = 0;
189       p2[len2 + align2] = 0;
190 
191       FOR_EACH_IMPL (impl, 1)
192 	{
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))
200 	    {
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;
204 	    }
205 	}
206     }
207 }
208 
209 static void
test_locale(const char * locale)210 test_locale (const char *locale)
211 {
212   size_t i;
213 
214   if (setlocale (LC_CTYPE, locale) == NULL)
215     {
216       error (0, 0, "cannot set locale \"%s\"", locale);
217       ret = 1;
218     }
219 
220   printf ("%-23s", locale);
221   FOR_EACH_IMPL (impl, 0)
222     printf ("\t%s", impl->name);
223   putchar ('\n');
224 
225   for (i = 1; i < 16; ++i)
226     {
227       do_test (i, i, i, 127, 0);
228       do_test (i, i, i, 127, 1);
229       do_test (i, i, i, 127, -1);
230     }
231 
232   for (i = 1; i < 10; ++i)
233     {
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);
240     }
241 
242   for (i = 1; i < 8; ++i)
243     {
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);
250     }
251 
252   do_random_tests ();
253 }
254 
255 int
test_main(void)256 test_main (void)
257 {
258   test_init ();
259 
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");
265 
266   return ret;
267 }
268 
269 #include <support/test-driver.c>
270