1 /* Test and measure strncmp functions.
2 Copyright (C) 1999, 2002, 2003, 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
22 #include "test-string.h"
24 typedef int (*proto_t
) (const char *, const char *, size_t);
25 int simple_strncmp (const char *, const char *, size_t);
26 int stupid_strncmp (const char *, const char *, size_t);
28 IMPL (stupid_strncmp
, 0)
29 IMPL (simple_strncmp
, 0)
33 simple_strncmp (const char *s1
, const char *s2
, size_t n
)
37 while (n
-- && (ret
= *(unsigned char *) s1
- * (unsigned char *) s2
++) == 0
43 stupid_strncmp (const char *s1
, const char *s2
, size_t n
)
45 size_t ns1
= strnlen (s1
, n
) + 1, ns2
= strnlen (s2
, n
) + 1;
48 n
= ns1
< n
? ns1
: n
;
49 n
= ns2
< n
? ns2
: n
;
50 while (n
-- && (ret
= *(unsigned char *) s1
++ - * (unsigned char *) s2
++) == 0);
55 check_result (impl_t
*impl
, const char *s1
, const char *s2
, size_t n
,
58 int result
= CALL (impl
, s1
, s2
, n
);
59 if ((exp_result
== 0 && result
!= 0)
60 || (exp_result
< 0 && result
>= 0)
61 || (exp_result
> 0 && result
<= 0))
63 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
73 do_one_test (impl_t
*impl
, const char *s1
, const char *s2
, size_t n
,
76 if (check_result (impl
, s1
, s2
, n
, exp_result
) < 0)
81 hp_timing_t start
__attribute ((unused
));
82 hp_timing_t stop
__attribute ((unused
));
83 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
86 for (i
= 0; i
< 32; ++i
)
88 HP_TIMING_NOW (start
);
89 CALL (impl
, s1
, s2
, n
);
91 HP_TIMING_BEST (best_time
, start
, stop
);
94 printf ("\t%zd", (size_t) best_time
);
99 do_test_limit (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
107 s1
= (char*)(buf1
+ page_size
);
108 s2
= (char*)(buf2
+ page_size
);
110 printf ("Length %4zd/%4zd:", len
, n
);
112 FOR_EACH_IMPL (impl
, 0)
113 do_one_test (impl
, s1
, s2
, n
, 0);
123 align_n
= (page_size
- n
) & 15;
125 s1
= (char*)(buf1
+ page_size
- n
);
126 s2
= (char*)(buf2
+ page_size
- n
);
128 if (align1
< align_n
)
129 s1
-= (align_n
- align1
);
131 if (align2
< align_n
)
132 s2
-= (align_n
- align2
);
134 for (i
= 0; i
< n
; i
++)
135 s1
[i
] = s2
[i
] = 1 + 23 * i
% max_char
;
143 else if (exp_result
> 0)
148 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len
, n
, align1
, align2
);
150 FOR_EACH_IMPL (impl
, 0)
151 do_one_test (impl
, s1
, s2
, n
, exp_result
);
158 do_test (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
168 if (align1
+ n
+ 1 >= page_size
)
172 if (align2
+ n
+ 1 >= page_size
)
175 s1
= (char*)(buf1
+ align1
);
176 s2
= (char*)(buf2
+ align2
);
178 for (i
= 0; i
< n
; i
++)
179 s1
[i
] = s2
[i
] = 1 + 23 * i
% max_char
;
181 s1
[n
] = 24 + exp_result
;
187 else if (exp_result
> 0)
190 s2
[n
- 1] -= exp_result
;
193 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len
, n
, align1
, align2
);
195 FOR_EACH_IMPL (impl
, 0)
196 do_one_test (impl
, (char*)s1
, (char*)s2
, n
, exp_result
);
203 do_random_tests (void)
205 size_t i
, j
, n
, align1
, align2
, pos
, len1
, len2
, size
;
208 unsigned char *p1
= buf1
+ page_size
- 512;
209 unsigned char *p2
= buf2
+ page_size
- 512;
211 for (n
= 0; n
< ITERATIONS
; n
++)
213 align1
= random () & 31;
215 align2
= random () & 31;
217 align2
= align1
+ (random () & 24);
218 pos
= random () & 511;
219 size
= random () & 511;
220 j
= align1
> align2
? align1
: align2
;
222 pos
= 510 - j
- (random () & 7);
223 len1
= random () & 511;
224 if (pos
>= len1
&& (random () & 1))
225 len1
= pos
+ (random () & 7);
227 len1
= 511 - j
- (random () & 7);
231 len2
= len1
+ (len1
!= 511 - j
? random () % (511 - j
- len1
) : 0);
232 j
= (pos
> len2
? pos
: len2
) + align1
+ 64;
235 for (i
= 0; i
< j
; ++i
)
237 p1
[i
] = random () & 255;
238 if (i
< len1
+ align1
&& !p1
[i
])
240 p1
[i
] = random () & 255;
242 p1
[i
] = 1 + (random () & 127);
245 for (i
= 0; i
< j
; ++i
)
247 p2
[i
] = random () & 255;
248 if (i
< len2
+ align2
&& !p2
[i
])
250 p2
[i
] = random () & 255;
252 p2
[i
] = 1 + (random () & 127);
257 memcpy (p2
+ align2
, p1
+ align1
, pos
);
260 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
262 p2
[align2
+ pos
] = random () & 255;
263 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
264 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
269 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
275 p1
[len1
+ align1
] = 0;
276 p2
[len2
+ align2
] = 0;
278 FOR_EACH_IMPL (impl
, 1)
280 r
= CALL (impl
, (char*)(p1
+ align1
), (char*)(p2
+ align2
), size
);
281 /* Test whether on 64-bit architectures where ABI requires
282 callee to promote has the promotion been done. */
283 asm ("" : "=g" (r
) : "0" (r
));
284 if ((r
== 0 && result
)
285 || (r
< 0 && result
>= 0)
286 || (r
> 0 && result
<= 0))
288 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
289 n
, impl
->name
, align1
, align2
, len1
, len2
, pos
, size
, r
, result
, p1
, p2
);
299 char *s1
= (char *)(buf1
+ 0xb2c);
300 char *s2
= (char *)(buf1
+ 0xfd8);
304 strcpy(s1
, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs");
305 strcpy(s2
, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV");
307 for (i
= 0; i
< 80; i
++)
309 exp_result
= simple_strncmp (s1
, s2
, i
);
310 FOR_EACH_IMPL (impl
, 0)
311 check_result (impl
, s1
, s2
, i
, exp_result
);
325 FOR_EACH_IMPL (impl
, 0)
326 printf ("\t%s", impl
->name
);
329 for (i
=0; i
< 16; ++i
)
331 do_test (0, 0, 8, i
, 127, 0);
332 do_test (0, 0, 8, i
, 127, -1);
333 do_test (0, 0, 8, i
, 127, 1);
334 do_test (i
, i
, 8, i
, 127, 0);
335 do_test (i
, i
, 8, i
, 127, 1);
336 do_test (i
, i
, 8, i
, 127, -1);
337 do_test (i
, 2 * i
, 8, i
, 127, 0);
338 do_test (2 * i
, i
, 8, i
, 127, 1);
339 do_test (i
, 3 * i
, 8, i
, 127, -1);
340 do_test (0, 0, 8, i
, 255, 0);
341 do_test (0, 0, 8, i
, 255, -1);
342 do_test (0, 0, 8, i
, 255, 1);
343 do_test (i
, i
, 8, i
, 255, 0);
344 do_test (i
, i
, 8, i
, 255, 1);
345 do_test (i
, i
, 8, i
, 255, -1);
346 do_test (i
, 2 * i
, 8, i
, 255, 0);
347 do_test (2 * i
, i
, 8, i
, 255, 1);
348 do_test (i
, 3 * i
, 8, i
, 255, -1);
351 for (i
= 1; i
< 8; ++i
)
353 do_test (0, 0, 8 << i
, 16 << i
, 127, 0);
354 do_test (0, 0, 8 << i
, 16 << i
, 127, 1);
355 do_test (0, 0, 8 << i
, 16 << i
, 127, -1);
356 do_test (0, 0, 8 << i
, 16 << i
, 255, 0);
357 do_test (0, 0, 8 << i
, 16 << i
, 255, 1);
358 do_test (0, 0, 8 << i
, 16 << i
, 255, -1);
359 do_test (8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 0);
360 do_test (8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 1);
361 do_test (2 * i
, i
, 8 << i
, 16 << i
, 255, 0);
362 do_test (2 * i
, i
, 8 << i
, 16 << i
, 255, 1);
365 do_test_limit (0, 0, 0, 0, 127, 0);
366 do_test_limit (4, 0, 21, 20, 127, 0);
367 do_test_limit (0, 4, 21, 20, 127, 0);
368 do_test_limit (8, 0, 25, 24, 127, 0);
369 do_test_limit (0, 8, 25, 24, 127, 0);
371 for (i
= 0; i
< 8; ++i
)
373 do_test_limit (0, 0, 17 - i
, 16 - i
, 127, 0);
374 do_test_limit (0, 0, 17 - i
, 16 - i
, 255, 0);
375 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, 0);
376 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, 1);
377 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, -1);
378 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, 0);
379 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, 1);
380 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, -1);
387 #include "../test-skeleton.c"