Add a test for BZ #15674
[glibc.git] / string / test-strncasecmp.c
blob4eedd3d0225d8a0d38377bcf70a5e9fecd452065
1 /* Test and measure strncasecmp functions.
2 Copyright (C) 1999-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <ctype.h>
21 #define TEST_MAIN
22 #define TEST_NAME "strncasecmp"
23 #include "test-string.h"
25 typedef int (*proto_t) (const char *, const char *, size_t);
26 static int simple_strncasecmp (const char *, const char *, size_t);
27 static int stupid_strncasecmp (const char *, const char *, size_t);
29 IMPL (stupid_strncasecmp, 0)
30 IMPL (simple_strncasecmp, 0)
31 IMPL (strncasecmp, 1)
33 static int
34 simple_strncasecmp (const char *s1, const char *s2, size_t n)
36 int ret;
38 if (n == 0)
39 return 0;
41 while ((ret = ((unsigned char) tolower (*s1)
42 - (unsigned char) tolower (*s2))) == 0
43 && *s1++)
45 if (--n == 0)
46 return 0;
47 ++s2;
49 return ret;
52 static int
53 stupid_strncasecmp (const char *s1, const char *s2, size_t max)
55 size_t ns1 = strlen (s1) + 1;
56 size_t ns2 = strlen (s2) + 1;
57 size_t n = ns1 < ns2 ? ns1 : ns2;
58 if (n > max)
59 n = max;
60 int ret = 0;
62 while (n--)
64 if ((ret = ((unsigned char) tolower (*s1)
65 - (unsigned char) tolower (*s2))) != 0)
66 break;
67 ++s1;
68 ++s2;
70 return ret;
73 static int
74 check_result (impl_t *impl, const char *s1, const char *s2, size_t n,
75 int exp_result)
77 int result = CALL (impl, s1, s2, n);
78 if ((exp_result == 0 && result != 0)
79 || (exp_result < 0 && result >= 0)
80 || (exp_result > 0 && result <= 0))
82 error (0, 0, "Wrong result in function %s %d %d", impl->name,
83 result, exp_result);
84 ret = 1;
85 return -1;
88 return 0;
91 static void
92 do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
93 int exp_result)
95 if (check_result (impl, s1, s2, n, exp_result) < 0)
96 return;
99 static void
100 do_test (size_t align1, size_t align2, size_t n, size_t len, int max_char,
101 int exp_result)
103 size_t i;
104 char *s1, *s2;
106 if (len == 0)
107 return;
109 align1 &= 7;
110 if (align1 + len + 1 >= page_size)
111 return;
113 align2 &= 7;
114 if (align2 + len + 1 >= page_size)
115 return;
117 s1 = (char *) (buf1 + align1);
118 s2 = (char *) (buf2 + align2);
120 for (i = 0; i < len; i++)
122 s1[i] = toupper (1 + 23 * i % max_char);
123 s2[i] = tolower (s1[i]);
126 s1[len] = s2[len] = 0;
127 s1[len + 1] = 23;
128 s2[len + 1] = 24 + exp_result;
129 if ((s2[len - 1] == 'z' && exp_result == -1)
130 || (s2[len - 1] == 'a' && exp_result == 1))
131 s1[len - 1] += exp_result;
132 else
133 s2[len - 1] -= exp_result;
135 FOR_EACH_IMPL (impl, 0)
136 do_one_test (impl, s1, s2, n, exp_result);
139 static void
140 do_random_tests (void)
142 size_t i, j, n, align1, align2, pos, len1, len2;
143 int result;
144 long r;
145 unsigned char *p1 = buf1 + page_size - 512;
146 unsigned char *p2 = buf2 + page_size - 512;
148 for (n = 0; n < ITERATIONS; n++)
150 align1 = random () & 31;
151 if (random () & 1)
152 align2 = random () & 31;
153 else
154 align2 = align1 + (random () & 24);
155 pos = random () & 511;
156 j = align1 > align2 ? align1 : align2;
157 if (pos + j >= 511)
158 pos = 510 - j - (random () & 7);
159 len1 = random () & 511;
160 if (pos >= len1 && (random () & 1))
161 len1 = pos + (random () & 7);
162 if (len1 + j >= 512)
163 len1 = 511 - j - (random () & 7);
164 if (pos >= len1)
165 len2 = len1;
166 else
167 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
168 j = (pos > len2 ? pos : len2) + align1 + 64;
169 if (j > 512)
170 j = 512;
171 for (i = 0; i < j; ++i)
173 p1[i] = tolower (random () & 255);
174 if (i < len1 + align1 && !p1[i])
176 p1[i] = tolower (random () & 255);
177 if (!p1[i])
178 p1[i] = tolower (1 + (random () & 127));
181 for (i = 0; i < j; ++i)
183 p2[i] = toupper (random () & 255);
184 if (i < len2 + align2 && !p2[i])
186 p2[i] = toupper (random () & 255);
187 if (!p2[i])
188 toupper (p2[i] = 1 + (random () & 127));
192 result = 0;
193 memcpy (p2 + align2, p1 + align1, pos);
194 if (pos < len1)
196 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
198 p2[align2 + pos] = toupper (random () & 255);
199 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
200 p2[align2 + pos] = toupper (p1[align1 + pos]
201 + 3 + (random () & 127));
204 if (p1[align1 + pos] < tolower (p2[align2 + pos]))
205 result = -1;
206 else
207 result = 1;
209 p1[len1 + align1] = 0;
210 p2[len2 + align2] = 0;
212 FOR_EACH_IMPL (impl, 1)
214 r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2),
215 pos + 1 + (random () & 255));
216 /* Test whether on 64-bit architectures where ABI requires
217 callee to promote has the promotion been done. */
218 asm ("" : "=g" (r) : "0" (r));
219 if ((r == 0 && result)
220 || (r < 0 && result >= 0)
221 || (r > 0 && result <= 0))
223 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
224 n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
225 ret = 1;
231 /* Regression test for BZ #12205 */
232 static void
233 bz12205 (void)
235 static char cp [4096+16] __attribute__ ((aligned(4096)));
236 static char gotrel[4096] __attribute__ ((aligned(4096)));
237 char *s1 = cp + 0xffa;
238 char *s2 = gotrel + 0xcbe;
239 int exp_result;
240 size_t n = 6;
242 strcpy (s1, "gottpoff");
243 strcpy (s2, "GOTPLT");
245 exp_result = simple_strncasecmp (s1, s2, n);
246 FOR_EACH_IMPL (impl, 0)
247 check_result (impl, s1, s2, n, exp_result);
250 /* Regression test for BZ #14195 */
251 static void
252 bz14195 (void)
254 const char *empty_string = "";
255 FOR_EACH_IMPL (impl, 0)
256 check_result (impl, empty_string, "", 5, 0);
260 test_main (void)
262 size_t i;
264 test_init ();
266 bz12205 ();
267 bz14195 ();
269 printf ("%23s", "");
270 FOR_EACH_IMPL (impl, 0)
271 printf ("\t%s", impl->name);
272 putchar ('\n');
274 for (i = 1; i < 16; ++i)
276 do_test (i, i, i - 1, i, 127, 0);
278 do_test (i, i, i, i, 127, 0);
279 do_test (i, i, i, i, 127, 1);
280 do_test (i, i, i, i, 127, -1);
282 do_test (i, i, i + 1, i, 127, 0);
283 do_test (i, i, i + 1, i, 127, 1);
284 do_test (i, i, i + 1, i, 127, -1);
287 for (i = 1; i < 10; ++i)
289 do_test (0, 0, (2 << i) - 1, 2 << i, 127, 0);
290 do_test (0, 0, 2 << i, 2 << i, 254, 0);
291 do_test (0, 0, (2 << i) + 1, 2 << i, 127, 0);
293 do_test (0, 0, (2 << i) + 1, 2 << i, 254, 0);
295 do_test (0, 0, 2 << i, 2 << i, 127, 1);
296 do_test (0, 0, (2 << i) + 10, 2 << i, 127, 1);
298 do_test (0, 0, 2 << i, 2 << i, 254, 1);
299 do_test (0, 0, (2 << i) + 10, 2 << i, 254, 1);
301 do_test (0, 0, 2 << i, 2 << i, 127, -1);
302 do_test (0, 0, (2 << i) + 10, 2 << i, 127, -1);
304 do_test (0, 0, 2 << i, 2 << i, 254, -1);
305 do_test (0, 0, (2 << i) + 10, 2 << i, 254, -1);
308 for (i = 1; i < 8; ++i)
310 do_test (i, 2 * i, (8 << i) - 1, 8 << i, 127, 0);
311 do_test (i, 2 * i, 8 << i, 8 << i, 127, 0);
312 do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 0);
314 do_test (2 * i, i, (8 << i) - 1, 8 << i, 254, 0);
315 do_test (2 * i, i, 8 << i, 8 << i, 254, 0);
316 do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 0);
318 do_test (i, 2 * i, 8 << i, 8 << i, 127, 1);
319 do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 1);
321 do_test (2 * i, i, 8 << i, 8 << i, 254, 1);
322 do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 1);
324 do_test (i, 2 * i, 8 << i, 8 << i, 127, -1);
325 do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, -1);
327 do_test (2 * i, i, 8 << i, 8 << i, 254, -1);
328 do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, -1);
331 do_random_tests ();
332 return ret;
335 #include "../test-skeleton.c"