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/>. */
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)
32 simple_strncmp (const char *s1
, const char *s2
, size_t n
)
36 while (n
-- && (ret
= *(unsigned char *) s1
- * (unsigned char *) s2
++) == 0
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;
47 n
= ns1
< n
? ns1
: n
;
48 n
= ns2
< n
? ns2
: n
;
49 while (n
-- && (ret
= *(unsigned char *) s1
++ - * (unsigned char *) s2
++) == 0);
54 check_result (impl_t
*impl
, const char *s1
, const char *s2
, size_t n
,
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
,
72 do_one_test (impl_t
*impl
, const char *s1
, const char *s2
, size_t n
,
75 if (check_result (impl
, s1
, s2
, n
, exp_result
) < 0)
80 hp_timing_t start
__attribute ((unused
));
81 hp_timing_t stop
__attribute ((unused
));
82 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
85 for (i
= 0; i
< 32; ++i
)
87 HP_TIMING_NOW (start
);
88 CALL (impl
, s1
, s2
, n
);
90 HP_TIMING_BEST (best_time
, start
, stop
);
93 printf ("\t%zd", (size_t) best_time
);
98 do_test_limit (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
106 s1
= (char*)(buf1
+ page_size
);
107 s2
= (char*)(buf2
+ page_size
);
109 printf ("Length %4zd/%4zd:", len
, n
);
111 FOR_EACH_IMPL (impl
, 0)
112 do_one_test (impl
, s1
, s2
, n
, 0);
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
;
142 else if (exp_result
> 0)
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
);
157 do_test (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
167 if (align1
+ n
+ 1 >= page_size
)
171 if (align2
+ n
+ 1 >= page_size
)
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
;
186 else if (exp_result
> 0)
189 s2
[n
- 1] -= exp_result
;
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
);
202 do_page_test (size_t offset1
, size_t offset2
, char *s2
)
207 if (offset1
>= page_size
|| offset2
>= page_size
)
210 s1
= (char *) (buf1
+ offset1
);
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
);
223 do_random_tests (void)
225 size_t i
, j
, n
, align1
, align2
, pos
, len1
, len2
, size
;
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;
235 align2
= random () & 31;
237 align2
= align1
+ (random () & 24);
238 pos
= random () & 511;
239 size
= random () & 511;
240 j
= align1
> align2
? align1
: align2
;
242 pos
= 510 - j
- (random () & 7);
243 len1
= random () & 511;
244 if (pos
>= len1
&& (random () & 1))
245 len1
= pos
+ (random () & 7);
247 len1
= 511 - j
- (random () & 7);
251 len2
= len1
+ (len1
!= 511 - j
? random () % (511 - j
- len1
) : 0);
252 j
= (pos
> len2
? pos
: len2
) + align1
+ 64;
255 for (i
= 0; i
< j
; ++i
)
257 p1
[i
] = random () & 255;
258 if (i
< len1
+ align1
&& !p1
[i
])
260 p1
[i
] = random () & 255;
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;
272 p2
[i
] = 1 + (random () & 127);
277 memcpy (p2
+ align2
, p1
+ align1
, pos
);
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);
289 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
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
);
319 char *s1
= (char *)(buf1
+ 0xb2c);
320 char *s2
= (char *)(buf1
+ 0xfd8);
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
);
342 for (i
= 0; i
< page_size
- 1; i
++)
348 for (i
= 0; i
< 64; ++i
)
349 do_page_test (3990 + i
, 2635, s2
);
365 FOR_EACH_IMPL (impl
, 0)
366 printf ("\t%s", impl
->name
);
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);
427 #include "../test-skeleton.c"