Use x86-64 bits/byteswap.h for both i386 and x86_64
[glibc.git] / string / test-strncmp.c
blob266781bc4468d632d5468a9166f720848a479c38
1 /* Test and measure strncmp functions.
2 Copyright (C) 1999, 2002, 2003, 2010, 2011 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 #define TEST_MAIN
21 #include "test-string.h"
23 typedef int (*proto_t) (const char *, const char *, size_t);
24 int simple_strncmp (const char *, const char *, size_t);
25 int stupid_strncmp (const char *, const char *, size_t);
27 IMPL (stupid_strncmp, 0)
28 IMPL (simple_strncmp, 0)
29 IMPL (strncmp, 1)
31 int
32 simple_strncmp (const char *s1, const char *s2, size_t n)
34 int ret = 0;
36 while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
37 && *s1++);
38 return ret;
41 int
42 stupid_strncmp (const char *s1, const char *s2, size_t n)
44 size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1;
45 int ret = 0;
47 n = ns1 < n ? ns1 : n;
48 n = ns2 < n ? ns2 : n;
49 while (n-- && (ret = *(unsigned char *) s1++ - * (unsigned char *) s2++) == 0);
50 return ret;
53 static int
54 check_result (impl_t *impl, const char *s1, const char *s2, size_t n,
55 int exp_result)
57 int result = CALL (impl, s1, s2, n);
58 if ((exp_result == 0 && result != 0)
59 || (exp_result < 0 && result >= 0)
60 || (exp_result > 0 && result <= 0))
62 error (0, 0, "Wrong result in function %s %d %d", impl->name,
63 result, exp_result);
64 ret = 1;
65 return -1;
68 return 0;
71 static void
72 do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
73 int exp_result)
75 if (check_result (impl, s1, s2, n, exp_result) < 0)
76 return;
78 if (HP_TIMING_AVAIL)
80 hp_timing_t start __attribute ((unused));
81 hp_timing_t stop __attribute ((unused));
82 hp_timing_t best_time = ~ (hp_timing_t) 0;
83 size_t i;
85 for (i = 0; i < 32; ++i)
87 HP_TIMING_NOW (start);
88 CALL (impl, s1, s2, n);
89 HP_TIMING_NOW (stop);
90 HP_TIMING_BEST (best_time, start, stop);
93 printf ("\t%zd", (size_t) best_time);
97 static void
98 do_test_limit (size_t align1, size_t align2, size_t len, size_t n, int max_char,
99 int exp_result)
101 size_t i, align_n;
102 char *s1, *s2;
104 if (n == 0)
106 s1 = (char*)(buf1 + page_size);
107 s2 = (char*)(buf2 + page_size);
108 if (HP_TIMING_AVAIL)
109 printf ("Length %4zd/%4zd:", len, n);
111 FOR_EACH_IMPL (impl, 0)
112 do_one_test (impl, s1, s2, n, 0);
114 if (HP_TIMING_AVAIL)
115 putchar ('\n');
117 return;
120 align1 &= 15;
121 align2 &= 15;
122 align_n = (page_size - n) & 15;
124 s1 = (char*)(buf1 + page_size - n);
125 s2 = (char*)(buf2 + page_size - n);
127 if (align1 < align_n)
128 s1 -= (align_n - align1);
130 if (align2 < align_n)
131 s2 -= (align_n - align2);
133 for (i = 0; i < n; i++)
134 s1[i] = s2[i] = 1 + 23 * i % max_char;
136 if (len < n)
138 s1[len] = 0;
139 s2[len] = 0;
140 if (exp_result < 0)
141 s2[len] = 32;
142 else if (exp_result > 0)
143 s1[len] = 64;
146 if (HP_TIMING_AVAIL)
147 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2);
149 FOR_EACH_IMPL (impl, 0)
150 do_one_test (impl, s1, s2, n, exp_result);
152 if (HP_TIMING_AVAIL)
153 putchar ('\n');
156 static void
157 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char,
158 int exp_result)
160 size_t i;
161 char *s1, *s2;
163 if (n == 0)
164 return;
166 align1 &= 7;
167 if (align1 + n + 1 >= page_size)
168 return;
170 align2 &= 7;
171 if (align2 + n + 1 >= page_size)
172 return;
174 s1 = (char*)(buf1 + align1);
175 s2 = (char*)(buf2 + align2);
177 for (i = 0; i < n; i++)
178 s1[i] = s2[i] = 1 + 23 * i % max_char;
180 s1[n] = 24 + exp_result;
181 s2[n] = 23;
182 s1[len] = 0;
183 s2[len] = 0;
184 if (exp_result < 0)
185 s2[len] = 32;
186 else if (exp_result > 0)
187 s1[len] = 64;
188 if (len >= n)
189 s2[n - 1] -= exp_result;
191 if (HP_TIMING_AVAIL)
192 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2);
194 FOR_EACH_IMPL (impl, 0)
195 do_one_test (impl, (char*)s1, (char*)s2, n, exp_result);
197 if (HP_TIMING_AVAIL)
198 putchar ('\n');
201 static void
202 do_page_test (size_t offset1, size_t offset2, char *s2)
204 char *s1;
205 int exp_result;
207 if (offset1 >= page_size || offset2 >= page_size)
208 return;
210 s1 = (char *) (buf1 + offset1);
211 s2 += offset2;
213 exp_result= *s1;
215 FOR_EACH_IMPL (impl, 0)
217 check_result (impl, s1, s2, page_size, -exp_result);
218 check_result (impl, s2, s1, page_size, exp_result);
222 static void
223 do_random_tests (void)
225 size_t i, j, n, align1, align2, pos, len1, len2, size;
226 int result;
227 long r;
228 unsigned char *p1 = buf1 + page_size - 512;
229 unsigned char *p2 = buf2 + page_size - 512;
231 for (n = 0; n < ITERATIONS; n++)
233 align1 = random () & 31;
234 if (random () & 1)
235 align2 = random () & 31;
236 else
237 align2 = align1 + (random () & 24);
238 pos = random () & 511;
239 size = random () & 511;
240 j = align1 > align2 ? align1 : align2;
241 if (pos + j >= 511)
242 pos = 510 - j - (random () & 7);
243 len1 = random () & 511;
244 if (pos >= len1 && (random () & 1))
245 len1 = pos + (random () & 7);
246 if (len1 + j >= 512)
247 len1 = 511 - j - (random () & 7);
248 if (pos >= len1)
249 len2 = len1;
250 else
251 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
252 j = (pos > len2 ? pos : len2) + align1 + 64;
253 if (j > 512)
254 j = 512;
255 for (i = 0; i < j; ++i)
257 p1[i] = random () & 255;
258 if (i < len1 + align1 && !p1[i])
260 p1[i] = random () & 255;
261 if (!p1[i])
262 p1[i] = 1 + (random () & 127);
265 for (i = 0; i < j; ++i)
267 p2[i] = random () & 255;
268 if (i < len2 + align2 && !p2[i])
270 p2[i] = random () & 255;
271 if (!p2[i])
272 p2[i] = 1 + (random () & 127);
276 result = 0;
277 memcpy (p2 + align2, p1 + align1, pos);
278 if (pos < len1)
280 if (p2[align2 + pos] == p1[align1 + pos])
282 p2[align2 + pos] = random () & 255;
283 if (p2[align2 + pos] == p1[align1 + pos])
284 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
287 if (pos < size)
289 if (p1[align1 + pos] < p2[align2 + pos])
290 result = -1;
291 else
292 result = 1;
295 p1[len1 + align1] = 0;
296 p2[len2 + align2] = 0;
298 FOR_EACH_IMPL (impl, 1)
300 r = CALL (impl, (char*)(p1 + align1), (char*)(p2 + align2), size);
301 /* Test whether on 64-bit architectures where ABI requires
302 callee to promote has the promotion been done. */
303 asm ("" : "=g" (r) : "0" (r));
304 if ((r == 0 && result)
305 || (r < 0 && result >= 0)
306 || (r > 0 && result <= 0))
308 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
309 n, impl->name, align1, align2, len1, len2, pos, size, r, result, p1, p2);
310 ret = 1;
316 static void
317 check1 (void)
319 char *s1 = (char *)(buf1 + 0xb2c);
320 char *s2 = (char *)(buf1 + 0xfd8);
321 size_t i;
322 int exp_result;
324 strcpy(s1, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs");
325 strcpy(s2, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV");
327 for (i = 0; i < 80; i++)
329 exp_result = simple_strncmp (s1, s2, i);
330 FOR_EACH_IMPL (impl, 0)
331 check_result (impl, s1, s2, i, exp_result);
335 static void
336 check2 (void)
338 size_t i;
339 char *s1, *s2;
341 s1 = (char *) buf1;
342 for (i = 0; i < page_size - 1; i++)
343 s1[i] = 23;
344 s1[i] = 0;
346 s2 = strdup (s1);
348 for (i = 0; i < 64; ++i)
349 do_page_test (3990 + i, 2635, s2);
351 free (s2);
355 test_main (void)
357 size_t i;
359 test_init ();
361 check1 ();
362 check2 ();
364 printf ("%23s", "");
365 FOR_EACH_IMPL (impl, 0)
366 printf ("\t%s", impl->name);
367 putchar ('\n');
369 for (i =0; i < 16; ++i)
371 do_test (0, 0, 8, i, 127, 0);
372 do_test (0, 0, 8, i, 127, -1);
373 do_test (0, 0, 8, i, 127, 1);
374 do_test (i, i, 8, i, 127, 0);
375 do_test (i, i, 8, i, 127, 1);
376 do_test (i, i, 8, i, 127, -1);
377 do_test (i, 2 * i, 8, i, 127, 0);
378 do_test (2 * i, i, 8, i, 127, 1);
379 do_test (i, 3 * i, 8, i, 127, -1);
380 do_test (0, 0, 8, i, 255, 0);
381 do_test (0, 0, 8, i, 255, -1);
382 do_test (0, 0, 8, i, 255, 1);
383 do_test (i, i, 8, i, 255, 0);
384 do_test (i, i, 8, i, 255, 1);
385 do_test (i, i, 8, i, 255, -1);
386 do_test (i, 2 * i, 8, i, 255, 0);
387 do_test (2 * i, i, 8, i, 255, 1);
388 do_test (i, 3 * i, 8, i, 255, -1);
391 for (i = 1; i < 8; ++i)
393 do_test (0, 0, 8 << i, 16 << i, 127, 0);
394 do_test (0, 0, 8 << i, 16 << i, 127, 1);
395 do_test (0, 0, 8 << i, 16 << i, 127, -1);
396 do_test (0, 0, 8 << i, 16 << i, 255, 0);
397 do_test (0, 0, 8 << i, 16 << i, 255, 1);
398 do_test (0, 0, 8 << i, 16 << i, 255, -1);
399 do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
400 do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
401 do_test (2 * i, i, 8 << i, 16 << i, 255, 0);
402 do_test (2 * i, i, 8 << i, 16 << i, 255, 1);
405 do_test_limit (0, 0, 0, 0, 127, 0);
406 do_test_limit (4, 0, 21, 20, 127, 0);
407 do_test_limit (0, 4, 21, 20, 127, 0);
408 do_test_limit (8, 0, 25, 24, 127, 0);
409 do_test_limit (0, 8, 25, 24, 127, 0);
411 for (i = 0; i < 8; ++i)
413 do_test_limit (0, 0, 17 - i, 16 - i, 127, 0);
414 do_test_limit (0, 0, 17 - i, 16 - i, 255, 0);
415 do_test_limit (0, 0, 15 - i, 16 - i, 127, 0);
416 do_test_limit (0, 0, 15 - i, 16 - i, 127, 1);
417 do_test_limit (0, 0, 15 - i, 16 - i, 127, -1);
418 do_test_limit (0, 0, 15 - i, 16 - i, 255, 0);
419 do_test_limit (0, 0, 15 - i, 16 - i, 255, 1);
420 do_test_limit (0, 0, 15 - i, 16 - i, 255, -1);
423 do_random_tests ();
424 return ret;
427 #include "../test-skeleton.c"