1 /* Test and measure strncasecmp functions.
2 Copyright (C) 1999, 2002, 2003, 2005, 2010 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
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)
34 simple_strncasecmp (const char *s1, const char *s2, size_t n)
41 while ((ret = ((unsigned char) tolower (*s1)
42 - (unsigned char) tolower (*s2))) == 0
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;
64 if ((ret = ((unsigned char) tolower (*s1)
65 - (unsigned char) tolower (*s2))) != 0)
74 do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
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,
90 hp_timing_t start __attribute ((unused));
91 hp_timing_t stop __attribute ((unused));
92 hp_timing_t best_time = ~ (hp_timing_t) 0;
95 for (i = 0; i < 32; ++i)
97 HP_TIMING_NOW (start);
98 CALL (impl, s1, s2, n);
100 HP_TIMING_BEST (best_time, start, stop);
103 printf ("\t%zd", (size_t) best_time);
108 do_test (size_t align1, size_t align2, size_t n, size_t len, int max_char,
118 if (align1 + len + 1 >= page_size)
122 if (align2 + len + 1 >= page_size)
125 s1 = (char *) (buf1 + align1);
126 s2 = (char *) (buf2 + align2);
128 for (i = 0; i < len; i++)
130 s1[i] = toupper (1 + 23 * i % max_char);
131 s2[i] = tolower (s1[i]);
134 s1[len] = s2[len] = 0;
136 s2[len + 1] = 24 + exp_result;
137 if ((s2[len - 1] == 'z' && exp_result == -1)
138 || (s2[len - 1] == 'a' && exp_result == 1))
139 s1[len - 1] += exp_result;
141 s2[len - 1] -= exp_result;
144 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
146 FOR_EACH_IMPL (impl, 0)
147 do_one_test (impl, s1, s2, n, exp_result);
154 do_random_tests (void)
156 size_t i, j, n, align1, align2, pos, len1, len2;
159 unsigned char *p1 = buf1 + page_size - 512;
160 unsigned char *p2 = buf2 + page_size - 512;
162 for (n = 0; n < ITERATIONS; n++)
164 align1 = random () & 31;
166 align2 = random () & 31;
168 align2 = align1 + (random () & 24);
169 pos = random () & 511;
170 j = align1 > align2 ? align1 : align2;
172 pos = 510 - j - (random () & 7);
173 len1 = random () & 511;
174 if (pos >= len1 && (random () & 1))
175 len1 = pos + (random () & 7);
177 len1 = 511 - j - (random () & 7);
181 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
182 j = (pos > len2 ? pos : len2) + align1 + 64;
185 for (i = 0; i < j; ++i)
187 p1[i] = tolower (random () & 255);
188 if (i < len1 + align1 && !p1[i])
190 p1[i] = tolower (random () & 255);
192 p1[i] = tolower (1 + (random () & 127));
195 for (i = 0; i < j; ++i)
197 p2[i] = toupper (random () & 255);
198 if (i < len2 + align2 && !p2[i])
200 p2[i] = toupper (random () & 255);
202 toupper (p2[i] = 1 + (random () & 127));
207 memcpy (p2 + align2, p1 + align1, pos);
210 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
212 p2[align2 + pos] = toupper (random () & 255);
213 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
214 p2[align2 + pos] = toupper (p1[align1 + pos]
215 + 3 + (random () & 127));
218 if (p1[align1 + pos] < tolower (p2[align2 + pos]))
223 p1[len1 + align1] = 0;
224 p2[len2 + align2] = 0;
226 FOR_EACH_IMPL (impl, 1)
228 r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2),
229 pos + 1 + (random () & 255));
230 /* Test whether on 64-bit architectures where ABI requires
231 callee to promote has the promotion been done. */
232 asm ("" : "=g" (r) : "0" (r));
233 if ((r == 0 && result)
234 || (r < 0 && result >= 0)
235 || (r > 0 && result <= 0))
237 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
238 n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
253 FOR_EACH_IMPL (impl, 0)
254 printf ("\t%s", impl->name);
257 for (i = 1; i < 16; ++i)
259 do_test (i, i, i - 1, i, 127, 0);
261 do_test (i, i, i, i, 127, 0);
262 do_test (i, i, i, i, 127, 1);
263 do_test (i, i, i, i, 127, -1);
265 do_test (i, i, i + 1, i, 127, 0);
266 do_test (i, i, i + 1, i, 127, 1);
267 do_test (i, i, i + 1, i, 127, -1);
270 for (i = 1; i < 10; ++i)
272 do_test (0, 0, (2 << i) - 1, 2 << i, 127, 0);
273 do_test (0, 0, 2 << i, 2 << i, 254, 0);
274 do_test (0, 0, (2 << i) + 1, 2 << i, 127, 0);
276 do_test (0, 0, (2 << i) + 1, 2 << i, 254, 0);
278 do_test (0, 0, 2 << i, 2 << i, 127, 1);
279 do_test (0, 0, (2 << i) + 10, 2 << i, 127, 1);
281 do_test (0, 0, 2 << i, 2 << i, 254, 1);
282 do_test (0, 0, (2 << i) + 10, 2 << i, 254, 1);
284 do_test (0, 0, 2 << i, 2 << i, 127, -1);
285 do_test (0, 0, (2 << i) + 10, 2 << i, 127, -1);
287 do_test (0, 0, 2 << i, 2 << i, 254, -1);
288 do_test (0, 0, (2 << i) + 10, 2 << i, 254, -1);
291 for (i = 1; i < 8; ++i)
293 do_test (i, 2 * i, (8 << i) - 1, 8 << i, 127, 0);
294 do_test (i, 2 * i, 8 << i, 8 << i, 127, 0);
295 do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 0);
297 do_test (2 * i, i, (8 << i) - 1, 8 << i, 254, 0);
298 do_test (2 * i, i, 8 << i, 8 << i, 254, 0);
299 do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 0);
301 do_test (i, 2 * i, 8 << i, 8 << i, 127, 1);
302 do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 1);
304 do_test (2 * i, i, 8 << i, 8 << i, 254, 1);
305 do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 1);
307 do_test (i, 2 * i, 8 << i, 8 << i, 127, -1);
308 do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, -1);
310 do_test (2 * i, i, 8 << i, 8 << i, 254, -1);
311 do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, -1);
318 #include "../test-skeleton.c"