1 /* Test and measure strncmp functions.
2 Copyright (C) 1999, 2002, 2003 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 do_one_test (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
,
71 hp_timing_t start
__attribute ((unused
));
72 hp_timing_t stop
__attribute ((unused
));
73 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
76 for (i
= 0; i
< 32; ++i
)
78 HP_TIMING_NOW (start
);
79 CALL (impl
, s1
, s2
, n
);
81 HP_TIMING_BEST (best_time
, start
, stop
);
84 printf ("\t%zd", (size_t) best_time
);
89 do_test_limit (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
97 s1
= (char*)(buf1
+ page_size
);
98 s2
= (char*)(buf2
+ page_size
);
100 printf ("Length %4zd/%4zd:", len
, n
);
102 FOR_EACH_IMPL (impl
, 0)
103 do_one_test (impl
, s1
, s2
, n
, 0);
113 align_n
= (page_size
- n
) & 15;
115 s1
= (char*)(buf1
+ page_size
- n
);
116 s2
= (char*)(buf2
+ page_size
- n
);
118 if (align1
< align_n
)
119 s1
-= (align_n
- align1
);
121 if (align2
< align_n
)
122 s2
-= (align_n
- align2
);
124 for (i
= 0; i
< n
; i
++)
125 s1
[i
] = s2
[i
] = 1 + 23 * i
% max_char
;
133 else if (exp_result
> 0)
138 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len
, n
, align1
, align2
);
140 FOR_EACH_IMPL (impl
, 0)
141 do_one_test (impl
, s1
, s2
, n
, exp_result
);
148 do_test (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
158 if (align1
+ n
+ 1 >= page_size
)
162 if (align2
+ n
+ 1 >= page_size
)
165 s1
= (char*)(buf1
+ align1
);
166 s2
= (char*)(buf2
+ align2
);
168 for (i
= 0; i
< n
; i
++)
169 s1
[i
] = s2
[i
] = 1 + 23 * i
% max_char
;
171 s1
[n
] = 24 + exp_result
;
177 else if (exp_result
> 0)
180 s2
[n
- 1] -= exp_result
;
183 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len
, n
, align1
, align2
);
185 FOR_EACH_IMPL (impl
, 0)
186 do_one_test (impl
, (char*)s1
, (char*)s2
, n
, exp_result
);
193 do_random_tests (void)
195 size_t i
, j
, n
, align1
, align2
, pos
, len1
, len2
, size
;
198 unsigned char *p1
= buf1
+ page_size
- 512;
199 unsigned char *p2
= buf2
+ page_size
- 512;
201 for (n
= 0; n
< ITERATIONS
; n
++)
203 align1
= random () & 31;
205 align2
= random () & 31;
207 align2
= align1
+ (random () & 24);
208 pos
= random () & 511;
209 size
= random () & 511;
210 j
= align1
> align2
? align1
: align2
;
212 pos
= 510 - j
- (random () & 7);
213 len1
= random () & 511;
214 if (pos
>= len1
&& (random () & 1))
215 len1
= pos
+ (random () & 7);
217 len1
= 511 - j
- (random () & 7);
221 len2
= len1
+ (len1
!= 511 - j
? random () % (511 - j
- len1
) : 0);
222 j
= (pos
> len2
? pos
: len2
) + align1
+ 64;
225 for (i
= 0; i
< j
; ++i
)
227 p1
[i
] = random () & 255;
228 if (i
< len1
+ align1
&& !p1
[i
])
230 p1
[i
] = random () & 255;
232 p1
[i
] = 1 + (random () & 127);
235 for (i
= 0; i
< j
; ++i
)
237 p2
[i
] = random () & 255;
238 if (i
< len2
+ align2
&& !p2
[i
])
240 p2
[i
] = random () & 255;
242 p2
[i
] = 1 + (random () & 127);
247 memcpy (p2
+ align2
, p1
+ align1
, pos
);
250 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
252 p2
[align2
+ pos
] = random () & 255;
253 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
254 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
259 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
265 p1
[len1
+ align1
] = 0;
266 p2
[len2
+ align2
] = 0;
268 FOR_EACH_IMPL (impl
, 1)
270 r
= CALL (impl
, (char*)(p1
+ align1
), (char*)(p2
+ align2
), size
);
271 /* Test whether on 64-bit architectures where ABI requires
272 callee to promote has the promotion been done. */
273 asm ("" : "=g" (r
) : "0" (r
));
274 if ((r
== 0 && result
)
275 || (r
< 0 && result
>= 0)
276 || (r
> 0 && result
<= 0))
278 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
279 n
, impl
->name
, align1
, align2
, len1
, len2
, pos
, size
, r
, result
, p1
, p2
);
294 FOR_EACH_IMPL (impl
, 0)
295 printf ("\t%s", impl
->name
);
298 for (i
=0; i
< 16; ++i
)
300 do_test (0, 0, 8, i
, 127, 0);
301 do_test (0, 0, 8, i
, 127, -1);
302 do_test (0, 0, 8, i
, 127, 1);
303 do_test (i
, i
, 8, i
, 127, 0);
304 do_test (i
, i
, 8, i
, 127, 1);
305 do_test (i
, i
, 8, i
, 127, -1);
306 do_test (i
, 2 * i
, 8, i
, 127, 0);
307 do_test (2 * i
, i
, 8, i
, 127, 1);
308 do_test (i
, 3 * i
, 8, i
, 127, -1);
309 do_test (0, 0, 8, i
, 255, 0);
310 do_test (0, 0, 8, i
, 255, -1);
311 do_test (0, 0, 8, i
, 255, 1);
312 do_test (i
, i
, 8, i
, 255, 0);
313 do_test (i
, i
, 8, i
, 255, 1);
314 do_test (i
, i
, 8, i
, 255, -1);
315 do_test (i
, 2 * i
, 8, i
, 255, 0);
316 do_test (2 * i
, i
, 8, i
, 255, 1);
317 do_test (i
, 3 * i
, 8, i
, 255, -1);
320 for (i
= 1; i
< 8; ++i
)
322 do_test (0, 0, 8 << i
, 16 << i
, 127, 0);
323 do_test (0, 0, 8 << i
, 16 << i
, 127, 1);
324 do_test (0, 0, 8 << i
, 16 << i
, 127, -1);
325 do_test (0, 0, 8 << i
, 16 << i
, 255, 0);
326 do_test (0, 0, 8 << i
, 16 << i
, 255, 1);
327 do_test (0, 0, 8 << i
, 16 << i
, 255, -1);
328 do_test (8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 0);
329 do_test (8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 1);
330 do_test (2 * i
, i
, 8 << i
, 16 << i
, 255, 0);
331 do_test (2 * i
, i
, 8 << i
, 16 << i
, 255, 1);
334 do_test_limit (0, 0, 0, 0, 127, 0);
335 do_test_limit (4, 0, 21, 20, 127, 0);
336 do_test_limit (0, 4, 21, 20, 127, 0);
337 do_test_limit (8, 0, 25, 24, 127, 0);
338 do_test_limit (0, 8, 25, 24, 127, 0);
340 for (i
= 0; i
< 8; ++i
)
342 do_test_limit (0, 0, 17 - i
, 16 - i
, 127, 0);
343 do_test_limit (0, 0, 17 - i
, 16 - i
, 255, 0);
344 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, 0);
345 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, 1);
346 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, -1);
347 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, 0);
348 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, 1);
349 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, -1);
356 #include "../test-skeleton.c"