Include atomic.h in generic lowlevellock.c.
[glibc.git] / string / test-strcmp.c
blobd9b69fa830b12020bd007d70559ce75ee55a56fc
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/>. */
21 #define TEST_MAIN
22 #ifdef WIDE
23 # define TEST_NAME "wcscmp"
24 #else
25 # define TEST_NAME "strcmp"
26 #endif
27 #include "test-string.h"
29 #ifdef WIDE
30 # include <wchar.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
39 # define CHAR wchar_t
40 # define UCHAR wchar_t
41 # define CHARBYTES 4
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 */
52 int
53 simple_wcscmp (const wchar_t *s1, const wchar_t *s2)
55 wchar_t c1, c2;
58 c1 = *s1++;
59 c2 = *s2++;
60 if (c2 == L'\0')
61 return c1 - c2;
63 while (c1 == c2);
65 return c1 < c2 ? -1 : 1;
68 int
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;
74 int ret = 0;
76 wchar_t c1, c2;
78 while (n--) {
79 c1 = *s1++;
80 c2 = *s2++;
81 if ((ret = c1 < c2 ? -1 : c1 == c2 ? 0 : 1) != 0)
82 break;
84 return ret;
87 #else
88 # include <limits.h>
90 # define L(str) str
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
97 # define CHAR char
98 # define UCHAR unsigned char
99 # define CHARBYTES 1
100 # define CHARBYTESLOG 0
101 # define CHARALIGN 1
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)
111 int ret;
113 while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++);
114 return ret;
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;
123 int ret = 0;
125 while (n--)
126 if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0)
127 break;
128 return ret;
130 #endif
132 typedef int (*proto_t) (const CHAR *, const CHAR *);
134 IMPL (STUPID_STRCMP, 1)
135 IMPL (SIMPLE_STRCMP, 1)
136 IMPL (STRCMP, 1)
138 static int
139 check_result (impl_t *impl,
140 const CHAR *s1, const CHAR *s2,
141 int exp_result)
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,
149 result, exp_result);
150 ret = 1;
151 return -1;
154 return 0;
157 static void
158 do_one_test (impl_t *impl,
159 const CHAR *s1, const CHAR *s2,
160 int exp_result)
162 if (check_result (impl, s1, s2, exp_result) < 0)
163 return;
165 if (HP_TIMING_AVAIL)
167 hp_timing_t start __attribute ((unused));
168 hp_timing_t stop __attribute ((unused));
169 hp_timing_t best_time = ~ (hp_timing_t) 0;
170 size_t i;
172 for (i = 0; i < 32; ++i)
174 HP_TIMING_NOW (start);
175 CALL (impl, s1, s2);
176 HP_TIMING_NOW (stop);
177 HP_TIMING_BEST (best_time, start, stop);
180 printf ("\t%zd", (size_t) best_time);
184 static void
185 do_test (size_t align1, size_t align2, size_t len, int max_char,
186 int exp_result)
188 size_t i;
190 CHAR *s1, *s2;
192 if (len == 0)
193 return;
195 align1 &= 63;
196 if (align1 + (len + 1) * CHARBYTES >= page_size)
197 return;
199 align2 &= 63;
200 if (align2 + (len + 1) * CHARBYTES >= page_size)
201 return;
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;
213 s1[len + 1] = 23;
214 s2[len + 1] = 24 + exp_result;
215 s2[len - 1] -= exp_result;
217 if (HP_TIMING_AVAIL)
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);
223 if (HP_TIMING_AVAIL)
224 putchar ('\n');
227 static void
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;
241 size_t align2;
242 if (random () & 1)
243 align2 = random () & 31;
244 else
245 align2 = align1 + (random () & 24);
246 size_t pos = random () & 511;
247 size_t j = align1 > align2 ? align1 : align2;
248 if (pos + j >= 511)
249 pos = 510 - j - (random () & 7);
250 size_t len1 = random () & 511;
251 if (pos >= len1 && (random () & 1))
252 len1 = pos + (random () & 7);
253 if (len1 + j >= 512)
254 len1 = 511 - j - (random () & 7);
255 size_t len2;
256 if (pos >= len1)
257 len2 = len1;
258 else
259 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
260 j = (pos > len2 ? pos : len2) + align1 + 64;
261 if (j > 512)
262 j = 512;
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;
269 if (!p1[i])
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;
279 if (!p2[i])
280 p2[i] = 1 + (random () & 127);
284 int result = 0;
285 MEMCPY (p2 + align2, p1 + align1, pos);
286 if (pos < len1)
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])
296 result = -1;
297 else
298 result = 1;
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);
315 ret = 1;
321 static void
322 check (void)
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 */
332 s1[0] = 1;
333 s2[0] = 1;
334 s1[1] = 1;
335 s2[1] = 1;
336 s1[2] = -1;
337 s2[2] = 3;
338 s1[3] = 0;
339 s2[3] = -1;
341 /* Check possible overflow bug, actual more for wcscmp */
343 s1[7] = CHAR__MIN;
344 s2[7] = CHAR__MAX;
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);
360 test_main (void)
362 size_t i;
364 test_init ();
365 check();
367 printf ("%23s", "");
368 FOR_EACH_IMPL (impl, 0)
369 printf ("\t%s", impl->name);
370 putchar ('\n');
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);
401 do_random_tests ();
402 return ret;
405 #include "../test-skeleton.c"