93
by Marcus Shawcroft
Add strncmp tests from glibc. |
1 |
/* Test and measure strncmp functions.
|
2 |
Copyright (C) 1999-2012 Free Software Foundation, Inc.
|
|
3 |
This file is part of the GNU C Library.
|
|
4 |
Written by Jakub Jelinek <jakub@redhat.com>, 1999.
|
|
5 |
||
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.
|
|
10 |
||
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.
|
|
15 |
||
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/>. */
|
|
19 |
||
20 |
#define TEST_MAIN
|
|
21 |
#define TEST_NAME "strncmp"
|
|
22 |
#include "test-string.h" |
|
23 |
||
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); |
|
27 |
||
28 |
IMPL (stupid_strncmp, 0) |
|
29 |
IMPL (simple_strncmp, 0) |
|
30 |
IMPL (strncmp, 1) |
|
31 |
||
32 |
int
|
|
33 |
simple_strncmp (const char *s1, const char *s2, size_t n) |
|
34 |
{
|
|
35 |
int ret = 0; |
|
36 |
||
37 |
while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0 |
|
38 |
&& *s1++); |
|
39 |
return ret; |
|
40 |
}
|
|
41 |
||
42 |
int
|
|
43 |
stupid_strncmp (const char *s1, const char *s2, size_t n) |
|
44 |
{
|
|
45 |
size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1; |
|
46 |
int ret = 0; |
|
47 |
||
48 |
n = ns1 < n ? ns1 : n; |
|
49 |
n = ns2 < n ? ns2 : n; |
|
50 |
while (n-- && (ret = *(unsigned char *) s1++ - * (unsigned char *) s2++) == 0); |
|
51 |
return ret; |
|
52 |
}
|
|
53 |
||
54 |
static int |
|
55 |
check_result (impl_t *impl, const char *s1, const char *s2, size_t n, |
|
56 |
int exp_result) |
|
57 |
{
|
|
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)) |
|
62 |
{
|
|
63 |
error (0, 0, "Wrong result in function %s %d %d", impl->name, |
|
64 |
result, exp_result); |
|
65 |
ret = 1; |
|
66 |
return -1; |
|
67 |
}
|
|
68 |
||
69 |
return 0; |
|
70 |
}
|
|
71 |
||
72 |
static void |
|
73 |
do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n, |
|
74 |
int exp_result) |
|
75 |
{
|
|
76 |
if (check_result (impl, s1, s2, n, exp_result) < 0) |
|
77 |
return; |
|
78 |
||
79 |
if (HP_TIMING_AVAIL) |
|
80 |
{
|
|
81 |
hp_timing_t start __attribute ((unused)); |
|
82 |
hp_timing_t stop __attribute ((unused)); |
|
83 |
hp_timing_t best_time = ~ (hp_timing_t) 0; |
|
84 |
size_t i; |
|
85 |
||
86 |
for (i = 0; i < 32; ++i) |
|
87 |
{
|
|
88 |
HP_TIMING_NOW (start); |
|
89 |
CALL (impl, s1, s2, n); |
|
90 |
HP_TIMING_NOW (stop); |
|
91 |
HP_TIMING_BEST (best_time, start, stop); |
|
92 |
}
|
|
93 |
||
94 |
printf ("\t%zd", (size_t) best_time); |
|
95 |
}
|
|
96 |
}
|
|
97 |
||
98 |
static void |
|
99 |
do_test_limit (size_t align1, size_t align2, size_t len, size_t n, int max_char, |
|
100 |
int exp_result) |
|
101 |
{
|
|
102 |
size_t i, align_n; |
|
103 |
char *s1, *s2; |
|
104 |
||
105 |
if (n == 0) |
|
106 |
{
|
|
107 |
s1 = (char*)(buf1 + page_size); |
|
108 |
s2 = (char*)(buf2 + page_size); |
|
109 |
if (HP_TIMING_AVAIL) |
|
110 |
printf ("Length %4zd/%4zd:", len, n); |
|
111 |
||
112 |
FOR_EACH_IMPL (impl, 0) |
|
113 |
do_one_test (impl, s1, s2, n, 0); |
|
114 |
||
115 |
if (HP_TIMING_AVAIL) |
|
116 |
putchar ('\n'); |
|
117 |
||
118 |
return; |
|
119 |
}
|
|
120 |
||
121 |
align1 &= 15; |
|
122 |
align2 &= 15; |
|
123 |
align_n = (page_size - n) & 15; |
|
124 |
||
125 |
s1 = (char*)(buf1 + page_size - n); |
|
126 |
s2 = (char*)(buf2 + page_size - n); |
|
127 |
||
128 |
if (align1 < align_n) |
|
129 |
s1 -= (align_n - align1); |
|
130 |
||
131 |
if (align2 < align_n) |
|
132 |
s2 -= (align_n - align2); |
|
133 |
||
134 |
for (i = 0; i < n; i++) |
|
135 |
s1[i] = s2[i] = 1 + 23 * i % max_char; |
|
136 |
||
137 |
if (len < n) |
|
138 |
{
|
|
139 |
s1[len] = 0; |
|
140 |
s2[len] = 0; |
|
141 |
if (exp_result < 0) |
|
142 |
s2[len] = 32; |
|
143 |
else if (exp_result > 0) |
|
144 |
s1[len] = 64; |
|
145 |
}
|
|
146 |
||
147 |
if (HP_TIMING_AVAIL) |
|
148 |
printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2); |
|
149 |
||
150 |
FOR_EACH_IMPL (impl, 0) |
|
151 |
do_one_test (impl, s1, s2, n, exp_result); |
|
152 |
||
153 |
if (HP_TIMING_AVAIL) |
|
154 |
putchar ('\n'); |
|
155 |
}
|
|
156 |
||
157 |
static void |
|
158 |
do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char, |
|
159 |
int exp_result) |
|
160 |
{
|
|
161 |
size_t i; |
|
162 |
char *s1, *s2; |
|
163 |
||
164 |
if (n == 0) |
|
165 |
return; |
|
166 |
||
167 |
align1 &= 7; |
|
168 |
if (align1 + n + 1 >= page_size) |
|
169 |
return; |
|
170 |
||
171 |
align2 &= 7; |
|
172 |
if (align2 + n + 1 >= page_size) |
|
173 |
return; |
|
174 |
||
175 |
s1 = (char*)(buf1 + align1); |
|
176 |
s2 = (char*)(buf2 + align2); |
|
177 |
||
178 |
for (i = 0; i < n; i++) |
|
179 |
s1[i] = s2[i] = 1 + 23 * i % max_char; |
|
180 |
||
181 |
s1[n] = 24 + exp_result; |
|
182 |
s2[n] = 23; |
|
183 |
s1[len] = 0; |
|
184 |
s2[len] = 0; |
|
185 |
if (exp_result < 0) |
|
186 |
s2[len] = 32; |
|
187 |
else if (exp_result > 0) |
|
188 |
s1[len] = 64; |
|
189 |
if (len >= n) |
|
190 |
s2[n - 1] -= exp_result; |
|
191 |
||
192 |
if (HP_TIMING_AVAIL) |
|
193 |
printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2); |
|
194 |
||
195 |
FOR_EACH_IMPL (impl, 0) |
|
196 |
do_one_test (impl, (char*)s1, (char*)s2, n, exp_result); |
|
197 |
||
198 |
if (HP_TIMING_AVAIL) |
|
199 |
putchar ('\n'); |
|
200 |
}
|
|
201 |
||
202 |
static void |
|
203 |
do_page_test (size_t offset1, size_t offset2, char *s2) |
|
204 |
{
|
|
205 |
char *s1; |
|
206 |
int exp_result; |
|
207 |
||
208 |
if (offset1 >= page_size || offset2 >= page_size) |
|
209 |
return; |
|
210 |
||
211 |
s1 = (char *) (buf1 + offset1); |
|
212 |
s2 += offset2; |
|
213 |
||
214 |
exp_result= *s1; |
|
215 |
||
216 |
FOR_EACH_IMPL (impl, 0) |
|
217 |
{
|
|
218 |
check_result (impl, s1, s2, page_size, -exp_result); |
|
219 |
check_result (impl, s2, s1, page_size, exp_result); |
|
220 |
}
|
|
221 |
}
|
|
222 |
||
223 |
static void |
|
224 |
do_random_tests (void) |
|
225 |
{
|
|
226 |
size_t i, j, n, align1, align2, pos, len1, len2, size; |
|
227 |
int result; |
|
228 |
long r; |
|
229 |
unsigned char *p1 = buf1 + page_size - 512; |
|
230 |
unsigned char *p2 = buf2 + page_size - 512; |
|
231 |
||
232 |
for (n = 0; n < ITERATIONS; n++) |
|
233 |
{
|
|
234 |
align1 = random () & 31; |
|
235 |
if (random () & 1) |
|
236 |
align2 = random () & 31; |
|
237 |
else
|
|
238 |
align2 = align1 + (random () & 24); |
|
239 |
pos = random () & 511; |
|
240 |
size = random () & 511; |
|
241 |
j = align1 > align2 ? align1 : align2; |
|
242 |
if (pos + j >= 511) |
|
243 |
pos = 510 - j - (random () & 7); |
|
244 |
len1 = random () & 511; |
|
245 |
if (pos >= len1 && (random () & 1)) |
|
246 |
len1 = pos + (random () & 7); |
|
247 |
if (len1 + j >= 512) |
|
248 |
len1 = 511 - j - (random () & 7); |
|
249 |
if (pos >= len1) |
|
250 |
len2 = len1; |
|
251 |
else
|
|
252 |
len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0); |
|
253 |
j = (pos > len2 ? pos : len2) + align1 + 64; |
|
254 |
if (j > 512) |
|
255 |
j = 512; |
|
256 |
for (i = 0; i < j; ++i) |
|
257 |
{
|
|
258 |
p1[i] = random () & 255; |
|
259 |
if (i < len1 + align1 && !p1[i]) |
|
260 |
{
|
|
261 |
p1[i] = random () & 255; |
|
262 |
if (!p1[i]) |
|
263 |
p1[i] = 1 + (random () & 127); |
|
264 |
}
|
|
265 |
}
|
|
266 |
for (i = 0; i < j; ++i) |
|
267 |
{
|
|
268 |
p2[i] = random () & 255; |
|
269 |
if (i < len2 + align2 && !p2[i]) |
|
270 |
{
|
|
271 |
p2[i] = random () & 255; |
|
272 |
if (!p2[i]) |
|
273 |
p2[i] = 1 + (random () & 127); |
|
274 |
}
|
|
275 |
}
|
|
276 |
||
277 |
result = 0; |
|
278 |
memcpy (p2 + align2, p1 + align1, pos); |
|
279 |
if (pos < len1) |
|
280 |
{
|
|
281 |
if (p2[align2 + pos] == p1[align1 + pos]) |
|
282 |
{
|
|
283 |
p2[align2 + pos] = random () & 255; |
|
284 |
if (p2[align2 + pos] == p1[align1 + pos]) |
|
285 |
p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127); |
|
286 |
}
|
|
287 |
||
288 |
if (pos < size) |
|
289 |
{
|
|
290 |
if (p1[align1 + pos] < p2[align2 + pos]) |
|
291 |
result = -1; |
|
292 |
else
|
|
293 |
result = 1; |
|
294 |
}
|
|
295 |
}
|
|
296 |
p1[len1 + align1] = 0; |
|
297 |
p2[len2 + align2] = 0; |
|
298 |
||
299 |
FOR_EACH_IMPL (impl, 1) |
|
300 |
{
|
|
301 |
r = CALL (impl, (char*)(p1 + align1), (char*)(p2 + align2), size); |
|
302 |
/* Test whether on 64-bit architectures where ABI requires
|
|
303 |
callee to promote has the promotion been done. */
|
|
304 |
asm ("" : "=g" (r) : "0" (r)); |
|
305 |
if ((r == 0 && result) |
|
306 |
|| (r < 0 && result >= 0) |
|
307 |
|| (r > 0 && result <= 0)) |
|
308 |
{
|
|
309 |
error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p", |
|
310 |
n, impl->name, align1, align2, len1, len2, pos, size, r, result, p1, p2); |
|
311 |
ret = 1; |
|
312 |
}
|
|
313 |
}
|
|
314 |
}
|
|
315 |
}
|
|
316 |
||
317 |
static void |
|
318 |
check1 (void) |
|
319 |
{
|
|
320 |
char *s1 = (char *)(buf1 + 0xb2c); |
|
321 |
char *s2 = (char *)(buf1 + 0xfd8); |
|
322 |
size_t i; |
|
323 |
int exp_result; |
|
324 |
||
325 |
strcpy(s1, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"); |
|
326 |
strcpy(s2, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"); |
|
327 |
||
328 |
for (i = 0; i < 80; i++) |
|
329 |
{
|
|
330 |
exp_result = simple_strncmp (s1, s2, i); |
|
331 |
FOR_EACH_IMPL (impl, 0) |
|
332 |
check_result (impl, s1, s2, i, exp_result); |
|
333 |
}
|
|
334 |
}
|
|
335 |
||
336 |
static void |
|
337 |
check2 (void) |
|
338 |
{
|
|
339 |
size_t i; |
|
340 |
char *s1, *s2; |
|
341 |
||
342 |
s1 = (char *) buf1; |
|
343 |
for (i = 0; i < page_size - 1; i++) |
|
344 |
s1[i] = 23; |
|
345 |
s1[i] = 0; |
|
346 |
||
347 |
s2 = strdup (s1); |
|
348 |
||
349 |
for (i = 0; i < 64; ++i) |
|
350 |
do_page_test (3990 + i, 2635, s2); |
|
351 |
||
352 |
free (s2); |
|
353 |
}
|
|
354 |
||
355 |
int
|
|
356 |
test_main (void) |
|
357 |
{
|
|
358 |
size_t i; |
|
359 |
||
360 |
test_init (); |
|
361 |
||
362 |
check1 (); |
|
363 |
check2 (); |
|
364 |
||
365 |
printf ("%23s", ""); |
|
366 |
FOR_EACH_IMPL (impl, 0) |
|
367 |
printf ("\t%s", impl->name); |
|
368 |
putchar ('\n'); |
|
369 |
||
370 |
for (i =0; i < 16; ++i) |
|
371 |
{
|
|
372 |
do_test (0, 0, 8, i, 127, 0); |
|
373 |
do_test (0, 0, 8, i, 127, -1); |
|
374 |
do_test (0, 0, 8, i, 127, 1); |
|
375 |
do_test (i, i, 8, i, 127, 0); |
|
376 |
do_test (i, i, 8, i, 127, 1); |
|
377 |
do_test (i, i, 8, i, 127, -1); |
|
378 |
do_test (i, 2 * i, 8, i, 127, 0); |
|
379 |
do_test (2 * i, i, 8, i, 127, 1); |
|
380 |
do_test (i, 3 * i, 8, i, 127, -1); |
|
381 |
do_test (0, 0, 8, i, 255, 0); |
|
382 |
do_test (0, 0, 8, i, 255, -1); |
|
383 |
do_test (0, 0, 8, i, 255, 1); |
|
384 |
do_test (i, i, 8, i, 255, 0); |
|
385 |
do_test (i, i, 8, i, 255, 1); |
|
386 |
do_test (i, i, 8, i, 255, -1); |
|
387 |
do_test (i, 2 * i, 8, i, 255, 0); |
|
388 |
do_test (2 * i, i, 8, i, 255, 1); |
|
389 |
do_test (i, 3 * i, 8, i, 255, -1); |
|
390 |
}
|
|
391 |
||
392 |
for (i = 1; i < 8; ++i) |
|
393 |
{
|
|
394 |
do_test (0, 0, 8 << i, 16 << i, 127, 0); |
|
395 |
do_test (0, 0, 8 << i, 16 << i, 127, 1); |
|
396 |
do_test (0, 0, 8 << i, 16 << i, 127, -1); |
|
397 |
do_test (0, 0, 8 << i, 16 << i, 255, 0); |
|
398 |
do_test (0, 0, 8 << i, 16 << i, 255, 1); |
|
399 |
do_test (0, 0, 8 << i, 16 << i, 255, -1); |
|
400 |
do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 0); |
|
401 |
do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 1); |
|
402 |
do_test (2 * i, i, 8 << i, 16 << i, 255, 0); |
|
403 |
do_test (2 * i, i, 8 << i, 16 << i, 255, 1); |
|
404 |
}
|
|
405 |
||
406 |
do_test_limit (0, 0, 0, 0, 127, 0); |
|
407 |
do_test_limit (4, 0, 21, 20, 127, 0); |
|
408 |
do_test_limit (0, 4, 21, 20, 127, 0); |
|
409 |
do_test_limit (8, 0, 25, 24, 127, 0); |
|
410 |
do_test_limit (0, 8, 25, 24, 127, 0); |
|
411 |
||
412 |
for (i = 0; i < 8; ++i) |
|
413 |
{
|
|
414 |
do_test_limit (0, 0, 17 - i, 16 - i, 127, 0); |
|
415 |
do_test_limit (0, 0, 17 - i, 16 - i, 255, 0); |
|
416 |
do_test_limit (0, 0, 15 - i, 16 - i, 127, 0); |
|
417 |
do_test_limit (0, 0, 15 - i, 16 - i, 127, 1); |
|
418 |
do_test_limit (0, 0, 15 - i, 16 - i, 127, -1); |
|
419 |
do_test_limit (0, 0, 15 - i, 16 - i, 255, 0); |
|
420 |
do_test_limit (0, 0, 15 - i, 16 - i, 255, 1); |
|
421 |
do_test_limit (0, 0, 15 - i, 16 - i, 255, -1); |
|
422 |
}
|
|
423 |
||
424 |
do_random_tests (); |
|
425 |
return ret; |
|
426 |
}
|
|
427 |
||
428 |
#include "test-skeleton.c" |