1 /* Test and measure strcmp and wcscmp 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 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/>. */
22 #include "test-string.h"
27 # define L(str) L##str
28 # define STRCMP wcscmp
29 # define STRCPY wcscpy
30 # define STRLEN wcslen
31 # define MEMCPY wmemcpy
32 # define SIMPLE_STRCMP simple_wcscmp
33 # define STUPID_STRCMP stupid_wcscmp
35 # define UCHAR wchar_t
37 # define CHARBYTESLOG 2
38 # define CHARALIGN __alignof__ (CHAR)
39 # define MIDCHAR 0x7fffffff
40 # define LARGECHAR 0xfffffffe
41 # define CHAR__MAX WCHAR_MAX
42 # define CHAR__MIN WCHAR_MIN
44 /* Wcscmp uses signed semantics for comparison, not unsigned */
45 /* Avoid using substraction since possible overflow */
48 simple_wcscmp (const wchar_t *s1
, const wchar_t *s2
)
60 return c1
< c2
? -1 : 1;
64 stupid_wcscmp (const wchar_t *s1
, const wchar_t *s2
)
66 size_t ns1
= wcslen (s1
) + 1;
67 size_t ns2
= wcslen (s2
) + 1;
68 size_t n
= ns1
< ns2
? ns1
: ns2
;
76 if ((ret
= c1
< c2
? -1 : c1
== c2
? 0 : 1) != 0)
86 # define STRCMP strcmp
87 # define STRCPY strcpy
88 # define STRLEN strlen
89 # define MEMCPY memcpy
90 # define SIMPLE_STRCMP simple_strcmp
91 # define STUPID_STRCMP stupid_strcmp
93 # define UCHAR unsigned char
95 # define CHARBYTESLOG 0
98 # define LARGECHAR 0xfe
99 # define CHAR__MAX CHAR_MAX
100 # define CHAR__MIN CHAR_MIN
102 /* Strcmp uses unsigned semantics for comparison. */
104 simple_strcmp (const char *s1
, const char *s2
)
108 while ((ret
= *(unsigned char *) s1
- *(unsigned char*) s2
++) == 0 && *s1
++);
113 stupid_strcmp (const char *s1
, const char *s2
)
115 size_t ns1
= strlen (s1
) + 1;
116 size_t ns2
= strlen (s2
) + 1;
117 size_t n
= ns1
< ns2
? ns1
: ns2
;
121 if ((ret
= *(unsigned char *) s1
++ - *(unsigned char *) s2
++) != 0)
127 typedef int (*proto_t
) (const CHAR
*, const CHAR
*);
129 IMPL (STUPID_STRCMP
, 1)
130 IMPL (SIMPLE_STRCMP
, 1)
134 check_result (impl_t
*impl
,
135 const CHAR
*s1
, const CHAR
*s2
,
138 int result
= CALL (impl
, s1
, s2
);
139 if ((exp_result
== 0 && result
!= 0)
140 || (exp_result
< 0 && result
>= 0)
141 || (exp_result
> 0 && result
<= 0))
143 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
153 do_one_test (impl_t
*impl
,
154 const CHAR
*s1
, const CHAR
*s2
,
157 if (check_result (impl
, s1
, s2
, exp_result
) < 0)
162 hp_timing_t start
__attribute ((unused
));
163 hp_timing_t stop
__attribute ((unused
));
164 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
167 for (i
= 0; i
< 32; ++i
)
169 HP_TIMING_NOW (start
);
171 HP_TIMING_NOW (stop
);
172 HP_TIMING_BEST (best_time
, start
, stop
);
175 printf ("\t%zd", (size_t) best_time
);
180 do_test (size_t align1
, size_t align2
, size_t len
, int max_char
,
191 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
195 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
198 /* Put them close to the end of page. */
199 i
= align1
+ CHARBYTES
* (len
+ 2);
200 s1
= (CHAR
*) (buf1
+ ((page_size
- i
) / 16 * 16) + align1
);
201 i
= align2
+ CHARBYTES
* (len
+ 2);
202 s2
= (CHAR
*) (buf2
+ ((page_size
- i
) / 16 * 16) + align2
);
204 for (i
= 0; i
< len
; i
++)
205 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% max_char
;
207 s1
[len
] = s2
[len
] = 0;
209 s2
[len
+ 1] = 24 + exp_result
;
210 s2
[len
- 1] -= exp_result
;
213 printf ("Length %4zd, alignment %2zd/%2zd:", len
, align1
, align2
);
215 FOR_EACH_IMPL (impl
, 0)
216 do_one_test (impl
, s1
, s2
, exp_result
);
223 do_random_tests (void)
225 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
);
226 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
);
228 for (size_t n
= 0; n
< ITERATIONS
; n
++)
230 /* for wcscmp case align1 and align2 mean here alignment
231 in wchar_t symbols, it equal 4*k alignment in bytes, we
232 don't check other alignments like for example
233 p1 = (wchar_t *)(buf1 + 1)
234 because it's wrong using of wchar_t type. */
235 size_t align1
= random () & 31;
238 align2
= random () & 31;
240 align2
= align1
+ (random () & 24);
241 size_t pos
= random () & 511;
242 size_t j
= align1
> align2
? align1
: align2
;
244 pos
= 510 - j
- (random () & 7);
245 size_t len1
= random () & 511;
246 if (pos
>= len1
&& (random () & 1))
247 len1
= pos
+ (random () & 7);
249 len1
= 511 - j
- (random () & 7);
254 len2
= len1
+ (len1
!= 511 - j
? random () % (511 - j
- len1
) : 0);
255 j
= (pos
> len2
? pos
: len2
) + align1
+ 64;
258 for (size_t i
= 0; i
< j
; ++i
)
260 p1
[i
] = random () & 255;
261 if (i
< len1
+ align1
&& !p1
[i
])
263 p1
[i
] = random () & 255;
265 p1
[i
] = 1 + (random () & 127);
268 for (size_t i
= 0; i
< j
; ++i
)
270 p2
[i
] = random () & 255;
271 if (i
< len2
+ align2
&& !p2
[i
])
273 p2
[i
] = random () & 255;
275 p2
[i
] = 1 + (random () & 127);
280 MEMCPY (p2
+ align2
, p1
+ align1
, pos
);
283 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
285 p2
[align2
+ pos
] = random () & 255;
286 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
287 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
290 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
295 p1
[len1
+ align1
] = 0;
296 p2
[len2
+ align2
] = 0;
298 FOR_EACH_IMPL (impl
, 1)
300 int r
= CALL (impl
, (CHAR
*) (p1
+ align1
), (CHAR
*) (p2
+ align2
));
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 (align in bytes: %zd, align in bytes: %zd, len1: %zd, len2: %zd, pos: %zd) %d != %d, p1 %p p2 %p",
309 n
, impl
->name
, (size_t) (p1
+ align1
) & 63, (size_t) (p1
+ align2
) & 63, len1
, len2
, pos
, r
, result
, p1
, p2
);
319 CHAR
*s1
= (CHAR
*) (buf1
+ 0xb2c);
320 CHAR
*s2
= (CHAR
*) (buf1
+ 0xfd8);
322 STRCPY(s1
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
323 STRCPY(s2
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
325 /* Check correct working for negatives values */
336 /* Check possible overflow bug, actual more for wcscmp */
341 size_t l1
= STRLEN (s1
);
342 size_t l2
= STRLEN (s2
);
344 for (size_t i1
= 0; i1
< l1
; i1
++)
345 for (size_t i2
= 0; i2
< l2
; i2
++)
347 int exp_result
= SIMPLE_STRCMP (s1
+ i1
, s2
+ i2
);
348 FOR_EACH_IMPL (impl
, 0)
349 check_result (impl
, s1
+ i1
, s2
+ i2
, exp_result
);
363 FOR_EACH_IMPL (impl
, 0)
364 printf ("\t%s", impl
->name
);
367 for (i
= 1; i
< 32; ++i
)
369 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 0);
370 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 1);
371 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, -1);
374 for (i
= 1; i
< 10 + CHARBYTESLOG
; ++i
)
376 do_test (0, 0, 2 << i
, MIDCHAR
, 0);
377 do_test (0, 0, 2 << i
, LARGECHAR
, 0);
378 do_test (0, 0, 2 << i
, MIDCHAR
, 1);
379 do_test (0, 0, 2 << i
, LARGECHAR
, 1);
380 do_test (0, 0, 2 << i
, MIDCHAR
, -1);
381 do_test (0, 0, 2 << i
, LARGECHAR
, -1);
382 do_test (0, CHARBYTES
* i
, 2 << i
, MIDCHAR
, 1);
383 do_test (CHARBYTES
* i
, CHARBYTES
* (i
+ 1), 2 << i
, LARGECHAR
, 1);
386 for (i
= 1; i
< 8; ++i
)
388 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 0);
389 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 0);
390 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 1);
391 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 1);
392 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, -1);
393 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, -1);
400 #include "../test-skeleton.c"