1 /* Test and measure strcmp and wcscmp functions.
2 Copyright (C) 1999-2020 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 <https://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 do_test (size_t align1
, size_t align2
, size_t len
, int max_char
,
178 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
182 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
185 /* Put them close to the end of page. */
186 i
= align1
+ CHARBYTES
* (len
+ 2);
187 s1
= (CHAR
*) (buf1
+ ((page_size
- i
) / 16 * 16) + align1
);
188 i
= align2
+ CHARBYTES
* (len
+ 2);
189 s2
= (CHAR
*) (buf2
+ ((page_size
- i
) / 16 * 16) + align2
);
191 for (i
= 0; i
< len
; i
++)
192 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% max_char
;
194 s1
[len
] = s2
[len
] = 0;
196 s2
[len
+ 1] = 24 + exp_result
;
197 s2
[len
- 1] -= exp_result
;
199 FOR_EACH_IMPL (impl
, 0)
200 do_one_test (impl
, s1
, s2
, exp_result
);
204 do_random_tests (void)
206 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
);
207 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
);
209 for (size_t n
= 0; n
< ITERATIONS
; n
++)
211 /* for wcscmp case align1 and align2 mean here alignment
212 in wchar_t symbols, it equal 4*k alignment in bytes, we
213 don't check other alignments like for example
214 p1 = (wchar_t *)(buf1 + 1)
215 because it's wrong using of wchar_t type. */
216 size_t align1
= random () & 31;
219 align2
= random () & 31;
221 align2
= align1
+ (random () & 24);
222 size_t pos
= random () & 511;
223 size_t j
= align1
> align2
? align1
: align2
;
225 pos
= 510 - j
- (random () & 7);
226 size_t len1
= random () & 511;
227 if (pos
>= len1
&& (random () & 1))
228 len1
= pos
+ (random () & 7);
230 len1
= 511 - j
- (random () & 7);
235 len2
= len1
+ (len1
!= 511 - j
? random () % (511 - j
- len1
) : 0);
236 j
= (pos
> len2
? pos
: len2
) + align1
+ 64;
239 for (size_t i
= 0; i
< j
; ++i
)
241 p1
[i
] = random () & 255;
242 if (i
< len1
+ align1
&& !p1
[i
])
244 p1
[i
] = random () & 255;
246 p1
[i
] = 1 + (random () & 127);
249 for (size_t i
= 0; i
< j
; ++i
)
251 p2
[i
] = random () & 255;
252 if (i
< len2
+ align2
&& !p2
[i
])
254 p2
[i
] = random () & 255;
256 p2
[i
] = 1 + (random () & 127);
261 MEMCPY (p2
+ align2
, p1
+ align1
, pos
);
264 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
266 p2
[align2
+ pos
] = random () & 255;
267 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
268 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
271 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
276 p1
[len1
+ align1
] = 0;
277 p2
[len2
+ align2
] = 0;
279 FOR_EACH_IMPL (impl
, 1)
281 int r
= CALL (impl
, (CHAR
*) (p1
+ align1
), (CHAR
*) (p2
+ align2
));
282 /* Test whether on 64-bit architectures where ABI requires
283 callee to promote has the promotion been done. */
284 asm ("" : "=g" (r
) : "0" (r
));
285 if ((r
== 0 && result
)
286 || (r
< 0 && result
>= 0)
287 || (r
> 0 && result
<= 0))
289 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",
290 n
, impl
->name
, (size_t) (p1
+ align1
) & 63, (size_t) (p1
+ align2
) & 63, len1
, len2
, pos
, r
, result
, p1
, p2
);
300 CHAR
*s1
= (CHAR
*) (buf1
+ 0xb2c);
301 CHAR
*s2
= (CHAR
*) (buf1
+ 0xfd8);
303 STRCPY(s1
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
304 STRCPY(s2
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
306 /* Check correct working for negatives values */
317 /* Check possible overflow bug, actual more for wcscmp */
322 size_t l1
= STRLEN (s1
);
323 size_t l2
= STRLEN (s2
);
325 for (size_t i1
= 0; i1
< l1
; i1
++)
326 for (size_t i2
= 0; i2
< l2
; i2
++)
328 int exp_result
= SIMPLE_STRCMP (s1
+ i1
, s2
+ i2
);
329 FOR_EACH_IMPL (impl
, 0)
330 check_result (impl
, s1
+ i1
, s2
+ i2
, exp_result
);
333 /* Test cases where there are multiple zero bytes after the first. */
335 for (size_t i
= 0; i
< 16 + 1; i
++)
341 for (size_t i
= 0; i
< 16; i
++)
345 for (int val
= 0x01; val
< 0x100; val
++)
347 for (size_t j
= 0; j
< i
; j
++)
355 exp_result
= SIMPLE_STRCMP (s1
, s2
);
356 FOR_EACH_IMPL (impl
, 0)
357 check_result (impl
, s1
, s2
, exp_result
);
372 FOR_EACH_IMPL (impl
, 0)
373 printf ("\t%s", impl
->name
);
376 for (i
= 1; i
< 32; ++i
)
378 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 0);
379 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 1);
380 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, -1);
383 for (i
= 1; i
< 10 + CHARBYTESLOG
; ++i
)
385 do_test (0, 0, 2 << i
, MIDCHAR
, 0);
386 do_test (0, 0, 2 << i
, LARGECHAR
, 0);
387 do_test (0, 0, 2 << i
, MIDCHAR
, 1);
388 do_test (0, 0, 2 << i
, LARGECHAR
, 1);
389 do_test (0, 0, 2 << i
, MIDCHAR
, -1);
390 do_test (0, 0, 2 << i
, LARGECHAR
, -1);
391 do_test (0, CHARBYTES
* i
, 2 << i
, MIDCHAR
, 1);
392 do_test (CHARBYTES
* i
, CHARBYTES
* (i
+ 1), 2 << i
, LARGECHAR
, 1);
395 for (i
= 1; i
< 8; ++i
)
397 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 0);
398 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 0);
399 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 1);
400 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 1);
401 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, -1);
402 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, -1);
409 #include <support/test-driver.c>