1 /* Test and measure strcmp and wcscmp functions.
2 Copyright (C) 1999-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5 Added wcscmp support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, 2011.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
23 # define TEST_NAME "wcscmp"
25 # define TEST_NAME "strcmp"
27 #include "test-string.h"
32 # define L(str) L##str
33 # define STRCMP wcscmp
34 # define STRCPY wcscpy
35 # define STRLEN wcslen
36 # define MEMCPY wmemcpy
37 # define SIMPLE_STRCMP simple_wcscmp
38 # define STUPID_STRCMP stupid_wcscmp
40 # define UCHAR wchar_t
42 # define CHARBYTESLOG 2
43 # define CHARALIGN __alignof__ (CHAR)
44 # define MIDCHAR 0x7fffffff
45 # define LARGECHAR 0xfffffffe
46 # define CHAR__MAX WCHAR_MAX
47 # define CHAR__MIN WCHAR_MIN
49 /* Wcscmp uses signed semantics for comparison, not unsigned */
50 /* Avoid using substraction since possible overflow */
53 simple_wcscmp (const wchar_t *s1
, const wchar_t *s2
)
65 return c1
< c2
? -1 : 1;
69 stupid_wcscmp (const wchar_t *s1
, const wchar_t *s2
)
71 size_t ns1
= wcslen (s1
) + 1;
72 size_t ns2
= wcslen (s2
) + 1;
73 size_t n
= ns1
< ns2
? ns1
: ns2
;
81 if ((ret
= c1
< c2
? -1 : c1
== c2
? 0 : 1) != 0)
91 # define STRCMP strcmp
92 # define STRCPY strcpy
93 # define STRLEN strlen
94 # define MEMCPY memcpy
95 # define SIMPLE_STRCMP simple_strcmp
96 # define STUPID_STRCMP stupid_strcmp
98 # define UCHAR unsigned char
100 # define CHARBYTESLOG 0
102 # define MIDCHAR 0x7f
103 # define LARGECHAR 0xfe
104 # define CHAR__MAX CHAR_MAX
105 # define CHAR__MIN CHAR_MIN
107 /* Strcmp uses unsigned semantics for comparison. */
109 simple_strcmp (const char *s1
, const char *s2
)
113 while ((ret
= *(unsigned char *) s1
- *(unsigned char*) s2
++) == 0 && *s1
++);
118 stupid_strcmp (const char *s1
, const char *s2
)
120 size_t ns1
= strlen (s1
) + 1;
121 size_t ns2
= strlen (s2
) + 1;
122 size_t n
= ns1
< ns2
? ns1
: ns2
;
126 if ((ret
= *(unsigned char *) s1
++ - *(unsigned char *) s2
++) != 0)
132 typedef int (*proto_t
) (const CHAR
*, const CHAR
*);
134 IMPL (STUPID_STRCMP
, 1)
135 IMPL (SIMPLE_STRCMP
, 1)
139 check_result (impl_t
*impl
,
140 const CHAR
*s1
, const CHAR
*s2
,
143 int result
= CALL (impl
, s1
, s2
);
144 if ((exp_result
== 0 && result
!= 0)
145 || (exp_result
< 0 && result
>= 0)
146 || (exp_result
> 0 && result
<= 0))
148 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
158 do_one_test (impl_t
*impl
,
159 const CHAR
*s1
, const CHAR
*s2
,
162 if (check_result (impl
, s1
, s2
, exp_result
) < 0)
167 hp_timing_t start
__attribute ((unused
));
168 hp_timing_t stop
__attribute ((unused
));
169 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
172 for (i
= 0; i
< 32; ++i
)
174 HP_TIMING_NOW (start
);
176 HP_TIMING_NOW (stop
);
177 HP_TIMING_BEST (best_time
, start
, stop
);
180 printf ("\t%zd", (size_t) best_time
);
185 do_test (size_t align1
, size_t align2
, size_t len
, int max_char
,
196 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
200 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
203 /* Put them close to the end of page. */
204 i
= align1
+ CHARBYTES
* (len
+ 2);
205 s1
= (CHAR
*) (buf1
+ ((page_size
- i
) / 16 * 16) + align1
);
206 i
= align2
+ CHARBYTES
* (len
+ 2);
207 s2
= (CHAR
*) (buf2
+ ((page_size
- i
) / 16 * 16) + align2
);
209 for (i
= 0; i
< len
; i
++)
210 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% max_char
;
212 s1
[len
] = s2
[len
] = 0;
214 s2
[len
+ 1] = 24 + exp_result
;
215 s2
[len
- 1] -= exp_result
;
218 printf ("Length %4zd, alignment %2zd/%2zd:", len
, align1
, align2
);
220 FOR_EACH_IMPL (impl
, 0)
221 do_one_test (impl
, s1
, s2
, exp_result
);
228 do_random_tests (void)
230 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
);
231 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
);
233 for (size_t n
= 0; n
< ITERATIONS
; n
++)
235 /* for wcscmp case align1 and align2 mean here alignment
236 in wchar_t symbols, it equal 4*k alignment in bytes, we
237 don't check other alignments like for example
238 p1 = (wchar_t *)(buf1 + 1)
239 because it's wrong using of wchar_t type. */
240 size_t align1
= random () & 31;
243 align2
= random () & 31;
245 align2
= align1
+ (random () & 24);
246 size_t pos
= random () & 511;
247 size_t j
= align1
> align2
? align1
: align2
;
249 pos
= 510 - j
- (random () & 7);
250 size_t len1
= random () & 511;
251 if (pos
>= len1
&& (random () & 1))
252 len1
= pos
+ (random () & 7);
254 len1
= 511 - j
- (random () & 7);
259 len2
= len1
+ (len1
!= 511 - j
? random () % (511 - j
- len1
) : 0);
260 j
= (pos
> len2
? pos
: len2
) + align1
+ 64;
263 for (size_t i
= 0; i
< j
; ++i
)
265 p1
[i
] = random () & 255;
266 if (i
< len1
+ align1
&& !p1
[i
])
268 p1
[i
] = random () & 255;
270 p1
[i
] = 1 + (random () & 127);
273 for (size_t i
= 0; i
< j
; ++i
)
275 p2
[i
] = random () & 255;
276 if (i
< len2
+ align2
&& !p2
[i
])
278 p2
[i
] = random () & 255;
280 p2
[i
] = 1 + (random () & 127);
285 MEMCPY (p2
+ align2
, p1
+ align1
, pos
);
288 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
290 p2
[align2
+ pos
] = random () & 255;
291 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
292 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
295 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
300 p1
[len1
+ align1
] = 0;
301 p2
[len2
+ align2
] = 0;
303 FOR_EACH_IMPL (impl
, 1)
305 int r
= CALL (impl
, (CHAR
*) (p1
+ align1
), (CHAR
*) (p2
+ align2
));
306 /* Test whether on 64-bit architectures where ABI requires
307 callee to promote has the promotion been done. */
308 asm ("" : "=g" (r
) : "0" (r
));
309 if ((r
== 0 && result
)
310 || (r
< 0 && result
>= 0)
311 || (r
> 0 && result
<= 0))
313 error (0, 0, "Iteration %zd - wrong result in function %s (align in bytes: %zd, align in bytes: %zd, len1: %zd, len2: %zd, pos: %zd) %d != %d, p1 %p p2 %p",
314 n
, impl
->name
, (size_t) (p1
+ align1
) & 63, (size_t) (p1
+ align2
) & 63, len1
, len2
, pos
, r
, result
, p1
, p2
);
324 CHAR
*s1
= (CHAR
*) (buf1
+ 0xb2c);
325 CHAR
*s2
= (CHAR
*) (buf1
+ 0xfd8);
327 STRCPY(s1
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
328 STRCPY(s2
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
330 /* Check correct working for negatives values */
341 /* Check possible overflow bug, actual more for wcscmp */
346 size_t l1
= STRLEN (s1
);
347 size_t l2
= STRLEN (s2
);
349 for (size_t i1
= 0; i1
< l1
; i1
++)
350 for (size_t i2
= 0; i2
< l2
; i2
++)
352 int exp_result
= SIMPLE_STRCMP (s1
+ i1
, s2
+ i2
);
353 FOR_EACH_IMPL (impl
, 0)
354 check_result (impl
, s1
+ i1
, s2
+ i2
, exp_result
);
368 FOR_EACH_IMPL (impl
, 0)
369 printf ("\t%s", impl
->name
);
372 for (i
= 1; i
< 32; ++i
)
374 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 0);
375 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 1);
376 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, -1);
379 for (i
= 1; i
< 10 + CHARBYTESLOG
; ++i
)
381 do_test (0, 0, 2 << i
, MIDCHAR
, 0);
382 do_test (0, 0, 2 << i
, LARGECHAR
, 0);
383 do_test (0, 0, 2 << i
, MIDCHAR
, 1);
384 do_test (0, 0, 2 << i
, LARGECHAR
, 1);
385 do_test (0, 0, 2 << i
, MIDCHAR
, -1);
386 do_test (0, 0, 2 << i
, LARGECHAR
, -1);
387 do_test (0, CHARBYTES
* i
, 2 << i
, MIDCHAR
, 1);
388 do_test (CHARBYTES
* i
, CHARBYTES
* (i
+ 1), 2 << i
, LARGECHAR
, 1);
391 for (i
= 1; i
< 8; ++i
)
393 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 0);
394 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 0);
395 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 1);
396 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 1);
397 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, -1);
398 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, -1);
405 #include "../test-skeleton.c"