Set GL(dl_nns) to 1 for vDSO in libc.a
[glibc.git] / string / test-strncasecmp.c
blobacfe668cabd3adcc0cf444ffb0d108712b18a895
1 /* Test and measure strncasecmp functions.
2 Copyright (C) 1999-2012 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 #include "test-string.h"
24 typedef int (*proto_t) (const char *, const char *, size_t);
25 static int simple_strncasecmp (const char *, const char *, size_t);
26 static int stupid_strncasecmp (const char *, const char *, size_t);
28 IMPL (stupid_strncasecmp, 0)
29 IMPL (simple_strncasecmp, 0)
30 IMPL (strncasecmp, 1)
32 static int
33 simple_strncasecmp (const char *s1, const char *s2, size_t n)
35 int ret;
37 if (n == 0)
38 return 0;
40 while ((ret = ((unsigned char) tolower (*s1)
41 - (unsigned char) tolower (*s2))) == 0
42 && *s1++)
44 if (--n == 0)
45 return 0;
46 ++s2;
48 return ret;
51 static int
52 stupid_strncasecmp (const char *s1, const char *s2, size_t max)
54 size_t ns1 = strlen (s1) + 1;
55 size_t ns2 = strlen (s2) + 1;
56 size_t n = ns1 < ns2 ? ns1 : ns2;
57 if (n > max)
58 n = max;
59 int ret = 0;
61 while (n--)
63 if ((ret = ((unsigned char) tolower (*s1)
64 - (unsigned char) tolower (*s2))) != 0)
65 break;
66 ++s1;
67 ++s2;
69 return ret;
72 static int
73 check_result (impl_t *impl, const char *s1, const char *s2, size_t n,
74 int exp_result)
76 int result = CALL (impl, s1, s2, n);
77 if ((exp_result == 0 && result != 0)
78 || (exp_result < 0 && result >= 0)
79 || (exp_result > 0 && result <= 0))
81 error (0, 0, "Wrong result in function %s %d %d", impl->name,
82 result, exp_result);
83 ret = 1;
84 return -1;
87 return 0;
90 static void
91 do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
92 int exp_result)
94 if (check_result (impl, s1, s2, n, exp_result) < 0)
95 return;
97 if (HP_TIMING_AVAIL)
99 hp_timing_t start __attribute ((unused));
100 hp_timing_t stop __attribute ((unused));
101 hp_timing_t best_time = ~ (hp_timing_t) 0;
102 size_t i;
104 for (i = 0; i < 32; ++i)
106 HP_TIMING_NOW (start);
107 CALL (impl, s1, s2, n);
108 HP_TIMING_NOW (stop);
109 HP_TIMING_BEST (best_time, start, stop);
112 printf ("\t%zd", (size_t) best_time);
116 static void
117 do_test (size_t align1, size_t align2, size_t n, size_t len, int max_char,
118 int exp_result)
120 size_t i;
121 char *s1, *s2;
123 if (len == 0)
124 return;
126 align1 &= 7;
127 if (align1 + len + 1 >= page_size)
128 return;
130 align2 &= 7;
131 if (align2 + len + 1 >= page_size)
132 return;
134 s1 = (char *) (buf1 + align1);
135 s2 = (char *) (buf2 + align2);
137 for (i = 0; i < len; i++)
139 s1[i] = toupper (1 + 23 * i % max_char);
140 s2[i] = tolower (s1[i]);
143 s1[len] = s2[len] = 0;
144 s1[len + 1] = 23;
145 s2[len + 1] = 24 + exp_result;
146 if ((s2[len - 1] == 'z' && exp_result == -1)
147 || (s2[len - 1] == 'a' && exp_result == 1))
148 s1[len - 1] += exp_result;
149 else
150 s2[len - 1] -= exp_result;
152 if (HP_TIMING_AVAIL)
153 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
155 FOR_EACH_IMPL (impl, 0)
156 do_one_test (impl, s1, s2, n, exp_result);
158 if (HP_TIMING_AVAIL)
159 putchar ('\n');
162 static void
163 do_random_tests (void)
165 size_t i, j, n, align1, align2, pos, len1, len2;
166 int result;
167 long r;
168 unsigned char *p1 = buf1 + page_size - 512;
169 unsigned char *p2 = buf2 + page_size - 512;
171 for (n = 0; n < ITERATIONS; n++)
173 align1 = random () & 31;
174 if (random () & 1)
175 align2 = random () & 31;
176 else
177 align2 = align1 + (random () & 24);
178 pos = random () & 511;
179 j = align1 > align2 ? align1 : align2;
180 if (pos + j >= 511)
181 pos = 510 - j - (random () & 7);
182 len1 = random () & 511;
183 if (pos >= len1 && (random () & 1))
184 len1 = pos + (random () & 7);
185 if (len1 + j >= 512)
186 len1 = 511 - j - (random () & 7);
187 if (pos >= len1)
188 len2 = len1;
189 else
190 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
191 j = (pos > len2 ? pos : len2) + align1 + 64;
192 if (j > 512)
193 j = 512;
194 for (i = 0; i < j; ++i)
196 p1[i] = tolower (random () & 255);
197 if (i < len1 + align1 && !p1[i])
199 p1[i] = tolower (random () & 255);
200 if (!p1[i])
201 p1[i] = tolower (1 + (random () & 127));
204 for (i = 0; i < j; ++i)
206 p2[i] = toupper (random () & 255);
207 if (i < len2 + align2 && !p2[i])
209 p2[i] = toupper (random () & 255);
210 if (!p2[i])
211 toupper (p2[i] = 1 + (random () & 127));
215 result = 0;
216 memcpy (p2 + align2, p1 + align1, pos);
217 if (pos < len1)
219 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
221 p2[align2 + pos] = toupper (random () & 255);
222 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
223 p2[align2 + pos] = toupper (p1[align1 + pos]
224 + 3 + (random () & 127));
227 if (p1[align1 + pos] < tolower (p2[align2 + pos]))
228 result = -1;
229 else
230 result = 1;
232 p1[len1 + align1] = 0;
233 p2[len2 + align2] = 0;
235 FOR_EACH_IMPL (impl, 1)
237 r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2),
238 pos + 1 + (random () & 255));
239 /* Test whether on 64-bit architectures where ABI requires
240 callee to promote has the promotion been done. */
241 asm ("" : "=g" (r) : "0" (r));
242 if ((r == 0 && result)
243 || (r < 0 && result >= 0)
244 || (r > 0 && result <= 0))
246 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
247 n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
248 ret = 1;
254 /* Regression test for BZ #12205 */
255 static void
256 bz12205 (void)
258 static char cp [4096+16] __attribute__ ((aligned(4096)));
259 static char gotrel[4096] __attribute__ ((aligned(4096)));
260 char *s1 = cp + 0xffa;
261 char *s2 = gotrel + 0xcbe;
262 int exp_result;
263 size_t n = 6;
265 strcpy (s1, "gottpoff");
266 strcpy (s2, "GOTPLT");
268 exp_result = simple_strncasecmp (s1, s2, n);
269 FOR_EACH_IMPL (impl, 0)
270 check_result (impl, s1, s2, n, exp_result);
273 /* Regression test for BZ #14195 */
274 static void
275 bz14195 (void)
277 const char *empty_string = "";
278 FOR_EACH_IMPL (impl, 0)
279 check_result (impl, empty_string, "", 5, 0);
283 test_main (void)
285 size_t i;
287 test_init ();
289 bz12205 ();
290 bz14195 ();
292 printf ("%23s", "");
293 FOR_EACH_IMPL (impl, 0)
294 printf ("\t%s", impl->name);
295 putchar ('\n');
297 for (i = 1; i < 16; ++i)
299 do_test (i, i, i - 1, i, 127, 0);
301 do_test (i, i, i, i, 127, 0);
302 do_test (i, i, i, i, 127, 1);
303 do_test (i, i, i, i, 127, -1);
305 do_test (i, i, i + 1, i, 127, 0);
306 do_test (i, i, i + 1, i, 127, 1);
307 do_test (i, i, i + 1, i, 127, -1);
310 for (i = 1; i < 10; ++i)
312 do_test (0, 0, (2 << i) - 1, 2 << i, 127, 0);
313 do_test (0, 0, 2 << i, 2 << i, 254, 0);
314 do_test (0, 0, (2 << i) + 1, 2 << i, 127, 0);
316 do_test (0, 0, (2 << i) + 1, 2 << i, 254, 0);
318 do_test (0, 0, 2 << i, 2 << i, 127, 1);
319 do_test (0, 0, (2 << i) + 10, 2 << i, 127, 1);
321 do_test (0, 0, 2 << i, 2 << i, 254, 1);
322 do_test (0, 0, (2 << i) + 10, 2 << i, 254, 1);
324 do_test (0, 0, 2 << i, 2 << i, 127, -1);
325 do_test (0, 0, (2 << i) + 10, 2 << i, 127, -1);
327 do_test (0, 0, 2 << i, 2 << i, 254, -1);
328 do_test (0, 0, (2 << i) + 10, 2 << i, 254, -1);
331 for (i = 1; i < 8; ++i)
333 do_test (i, 2 * i, (8 << i) - 1, 8 << i, 127, 0);
334 do_test (i, 2 * i, 8 << i, 8 << i, 127, 0);
335 do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 0);
337 do_test (2 * i, i, (8 << i) - 1, 8 << i, 254, 0);
338 do_test (2 * i, i, 8 << i, 8 << i, 254, 0);
339 do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 0);
341 do_test (i, 2 * i, 8 << i, 8 << i, 127, 1);
342 do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 1);
344 do_test (2 * i, i, 8 << i, 8 << i, 254, 1);
345 do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 1);
347 do_test (i, 2 * i, 8 << i, 8 << i, 127, -1);
348 do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, -1);
350 do_test (2 * i, i, 8 << i, 8 << i, 254, -1);
351 do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, -1);
354 do_random_tests ();
355 return ret;
358 #include "../test-skeleton.c"