1 /* Test and measure STRCMP functions.
2 Copyright (C) 1999, 2002, 2003, 2005, 2011 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, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include "test-string.h"
26 # include <inttypes.h>
29 # define L(str) L##str
30 # define STRCMP wcscmp
31 # define STRCPY wcscpy
32 # define STRLEN wcslen
33 # define MEMCPY wmemcpy
34 # define SIMPLE_STRCMP simple_wcscmp
35 # define STUPID_STRCMP stupid_wcscmp
37 # define UCHAR uint32_t
39 # define CHARBYTESLOG 2
40 # define CHARALIGN __alignof__ (CHAR)
41 # define MIDCHAR 0x7fffffff
42 # define LARGECHAR 0xfffffffe
45 # define STRCMP strcmp
46 # define STRCPY strcpy
47 # define STRLEN strlen
48 # define MEMCPY memcpy
49 # define SIMPLE_STRCMP simple_strcmp
50 # define STUPID_STRCMP stupid_strcmp
52 # define UCHAR unsigned char
54 # define CHARBYTESLOG 0
57 # define LARGECHAR 0xfe
59 typedef int (*proto_t
) (const CHAR
*, const CHAR
*);
62 SIMPLE_STRCMP (const CHAR
*s1
, const CHAR
*s2
)
66 while ((ret
= *(UCHAR
*) s1
- *(UCHAR
*) s2
++) == 0 && *s1
++);
71 STUPID_STRCMP (const CHAR
*s1
, const CHAR
*s2
)
73 size_t ns1
= STRLEN (s1
) + 1;
74 size_t ns2
= STRLEN (s2
) + 1;
75 size_t n
= ns1
< ns2
? ns1
: ns2
;
79 if ((ret
= *(UCHAR
*) s1
++ - *(UCHAR
*) s2
++) != 0)
84 IMPL (STUPID_STRCMP
, 1)
85 IMPL (SIMPLE_STRCMP
, 1)
89 check_result (impl_t
*impl
,
90 const CHAR
*s1
, const CHAR
*s2
,
93 int result
= CALL (impl
, s1
, s2
);
94 if ((exp_result
== 0 && result
!= 0)
95 || (exp_result
< 0 && result
>= 0)
96 || (exp_result
> 0 && result
<= 0))
98 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
108 do_one_test (impl_t
*impl
,
109 const CHAR
*s1
, const CHAR
*s2
,
112 if (check_result (impl
, s1
, s2
, exp_result
) < 0)
117 hp_timing_t start
__attribute ((unused
));
118 hp_timing_t stop
__attribute ((unused
));
119 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
122 for (i
= 0; i
< 32; ++i
)
124 HP_TIMING_NOW (start
);
126 HP_TIMING_NOW (stop
);
127 HP_TIMING_BEST (best_time
, start
, stop
);
130 printf ("\t%zd", (size_t) best_time
);
135 do_test (size_t align1
, size_t align2
, size_t len
, int max_char
,
146 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
150 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
153 /* Put them close to the end of page. */
154 i
= align1
+ CHARBYTES
* (len
+ 2);
155 s1
= (CHAR
*) (buf1
+ ((page_size
- i
) / 16 * 16) + align1
);
156 i
= align2
+ CHARBYTES
* (len
+ 2);
157 s2
= (CHAR
*) (buf2
+ ((page_size
- i
) / 16 * 16) + align2
);
159 for (i
= 0; i
< len
; i
++)
160 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% max_char
;
162 s1
[len
] = s2
[len
] = 0;
164 s2
[len
+ 1] = 24 + exp_result
;
165 s2
[len
- 1] -= exp_result
;
168 printf ("Length %4zd, alignment %2zd/%2zd:", len
, align1
, align2
);
170 FOR_EACH_IMPL (impl
, 0)
171 do_one_test (impl
, s1
, s2
, exp_result
);
178 do_random_tests (void)
180 for (size_t a
= 0; a
< CHARBYTES
; a
+= CHARALIGN
)
181 for (size_t b
= 0; b
< CHARBYTES
; b
+= CHARALIGN
)
183 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
- a
);
184 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
- b
);
186 for (size_t n
= 0; n
< ITERATIONS
; n
++)
188 size_t align1
= random () & 31;
191 align2
= random () & 31;
193 align2
= align1
+ (random () & 24);
194 size_t pos
= random () & 511;
195 size_t j
= align1
> align2
? align1
: align2
;
197 pos
= 510 - j
- (random () & 7);
198 size_t len1
= random () & 511;
199 if (pos
>= len1
&& (random () & 1))
200 len1
= pos
+ (random () & 7);
202 len1
= 511 - j
- (random () & 7);
207 len2
= len1
+ (len1
!= 511 - j
? random () % (511 - j
- len1
) : 0);
208 j
= (pos
> len2
? pos
: len2
) + align1
+ 64;
211 for (size_t i
= 0; i
< j
; ++i
)
213 p1
[i
] = random () & 255;
214 if (i
< len1
+ align1
&& !p1
[i
])
216 p1
[i
] = random () & 255;
218 p1
[i
] = 1 + (random () & 127);
221 for (size_t i
= 0; i
< j
; ++i
)
223 p2
[i
] = random () & 255;
224 if (i
< len2
+ align2
&& !p2
[i
])
226 p2
[i
] = random () & 255;
228 p2
[i
] = 1 + (random () & 127);
233 MEMCPY ((CHAR
*) (p2
+ align2
), (CHAR
*) (p1
+ align1
), pos
);
236 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
238 p2
[align2
+ pos
] = random () & 255;
239 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
240 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
243 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
248 p1
[len1
+ align1
] = 0;
249 p2
[len2
+ align2
] = 0;
251 FOR_EACH_IMPL (impl
, 1)
253 int r
= CALL (impl
, (CHAR
*) (p1
+ align1
), (CHAR
*) (p2
+ align2
));
254 /* Test whether on 64-bit architectures where ABI requires
255 callee to promote has the promotion been done. */
256 asm ("" : "=g" (r
) : "0" (r
));
257 if ((r
== 0 && result
)
258 || (r
< 0 && result
>= 0)
259 || (r
> 0 && result
<= 0))
261 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %d != %d, p1 %p p2 %p",
262 n
, impl
->name
, (size_t) (p1
+ align1
) & 63, (size_t) (p1
+ align2
) & 63, len1
, len2
, pos
, r
, result
, p1
, p2
);
273 CHAR
*s1
= (CHAR
*) (buf1
+ 0xb2c);
274 CHAR
*s2
= (CHAR
*) (buf1
+ 0xfd8);
276 STRCPY(s1
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
277 STRCPY(s2
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
279 size_t l1
= STRLEN (s1
);
280 size_t l2
= STRLEN (s2
);
281 for (size_t i1
= 0; i1
< l1
; i1
++)
282 for (size_t i2
= 0; i2
< l2
; i2
++)
284 int exp_result
= SIMPLE_STRCMP (s1
+ i1
, s2
+ i2
);
285 FOR_EACH_IMPL (impl
, 0)
286 check_result (impl
, s1
+ i1
, s2
+ i2
, exp_result
);
300 FOR_EACH_IMPL (impl
, 0)
301 printf ("\t%s", impl
->name
);
304 for (i
= 1; i
< 32; ++i
)
306 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 0);
307 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 1);
308 do_test (CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, -1);
311 for (i
= 1; i
< 10 + CHARBYTESLOG
; ++i
)
313 do_test (0, 0, 2 << i
, MIDCHAR
, 0);
314 do_test (0, 0, 2 << i
, LARGECHAR
, 0);
315 do_test (0, 0, 2 << i
, MIDCHAR
, 1);
316 do_test (0, 0, 2 << i
, LARGECHAR
, 1);
317 do_test (0, 0, 2 << i
, MIDCHAR
, -1);
318 do_test (0, 0, 2 << i
, LARGECHAR
, -1);
319 do_test (0, CHARBYTES
* i
, 2 << i
, MIDCHAR
, 1);
320 do_test (CHARBYTES
* i
, CHARBYTES
* (i
+ 1), 2 << i
, LARGECHAR
, 1);
323 for (i
= 1; i
< 8; ++i
)
325 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 0);
326 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 0);
327 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 1);
328 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 1);
329 do_test (CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, -1);
330 do_test (2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, -1);
337 #include "../test-skeleton.c"