2012-03-29 Jeff Law <law@redhat.com>
[glibc.git] / string / test-strcmp.c
blob85df6dcd48719be0fe70f18fab08e1bcb238bd0c
1 /* Test and measure strcmp and wcscmp 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, see
19 <http://www.gnu.org/licenses/>. */
21 #define TEST_MAIN
22 #include "test-string.h"
24 #ifdef WIDE
25 # include <wchar.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
34 # define CHAR wchar_t
35 # define UCHAR wchar_t
36 # define CHARBYTES 4
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 */
47 int
48 simple_wcscmp (const wchar_t *s1, const wchar_t *s2)
50 wchar_t c1, c2;
53 c1 = *s1++;
54 c2 = *s2++;
55 if (c2 == L'\0')
56 return c1 - c2;
58 while (c1 == c2);
60 return c1 < c2 ? -1 : 1;
63 int
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;
69 int ret = 0;
71 wchar_t c1, c2;
73 while (n--) {
74 c1 = *s1++;
75 c2 = *s2++;
76 if ((ret = c1 < c2 ? -1 : c1 == c2 ? 0 : 1) != 0)
77 break;
79 return ret;
82 #else
83 # define L(str) str
84 # define STRCMP strcmp
85 # define STRCPY strcpy
86 # define STRLEN strlen
87 # define MEMCPY memcpy
88 # define SIMPLE_STRCMP simple_strcmp
89 # define STUPID_STRCMP stupid_strcmp
90 # define CHAR char
91 # define UCHAR unsigned char
92 # define CHARBYTES 1
93 # define CHARBYTESLOG 0
94 # define CHARALIGN 1
95 # define MIDCHAR 0x7f
96 # define LARGECHAR 0xfe
97 # define CHAR__MAX CHAR_MAX
98 # define CHAR__MIN CHAR_MIN
100 /* Strcmp uses unsigned semantics for comparison. */
102 simple_strcmp (const char *s1, const char *s2)
104 int ret;
106 while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++);
107 return ret;
111 stupid_strcmp (const char *s1, const char *s2)
113 size_t ns1 = strlen (s1) + 1;
114 size_t ns2 = strlen (s2) + 1;
115 size_t n = ns1 < ns2 ? ns1 : ns2;
116 int ret = 0;
118 while (n--)
119 if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0)
120 break;
121 return ret;
123 #endif
125 typedef int (*proto_t) (const CHAR *, const CHAR *);
127 IMPL (STUPID_STRCMP, 1)
128 IMPL (SIMPLE_STRCMP, 1)
129 IMPL (STRCMP, 1)
131 static int
132 check_result (impl_t *impl,
133 const CHAR *s1, const CHAR *s2,
134 int exp_result)
136 int result = CALL (impl, s1, s2);
137 if ((exp_result == 0 && result != 0)
138 || (exp_result < 0 && result >= 0)
139 || (exp_result > 0 && result <= 0))
141 error (0, 0, "Wrong result in function %s %d %d", impl->name,
142 result, exp_result);
143 ret = 1;
144 return -1;
147 return 0;
150 static void
151 do_one_test (impl_t *impl,
152 const CHAR *s1, const CHAR *s2,
153 int exp_result)
155 if (check_result (impl, s1, s2, exp_result) < 0)
156 return;
158 if (HP_TIMING_AVAIL)
160 hp_timing_t start __attribute ((unused));
161 hp_timing_t stop __attribute ((unused));
162 hp_timing_t best_time = ~ (hp_timing_t) 0;
163 size_t i;
165 for (i = 0; i < 32; ++i)
167 HP_TIMING_NOW (start);
168 CALL (impl, s1, s2);
169 HP_TIMING_NOW (stop);
170 HP_TIMING_BEST (best_time, start, stop);
173 printf ("\t%zd", (size_t) best_time);
177 static void
178 do_test (size_t align1, size_t align2, size_t len, int max_char,
179 int exp_result)
181 size_t i;
183 CHAR *s1, *s2;
185 if (len == 0)
186 return;
188 align1 &= 63;
189 if (align1 + (len + 1) * CHARBYTES >= page_size)
190 return;
192 align2 &= 63;
193 if (align2 + (len + 1) * CHARBYTES >= page_size)
194 return;
196 /* Put them close to the end of page. */
197 i = align1 + CHARBYTES * (len + 2);
198 s1 = (CHAR *) (buf1 + ((page_size - i) / 16 * 16) + align1);
199 i = align2 + CHARBYTES * (len + 2);
200 s2 = (CHAR *) (buf2 + ((page_size - i) / 16 * 16) + align2);
202 for (i = 0; i < len; i++)
203 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
205 s1[len] = s2[len] = 0;
206 s1[len + 1] = 23;
207 s2[len + 1] = 24 + exp_result;
208 s2[len - 1] -= exp_result;
210 if (HP_TIMING_AVAIL)
211 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
213 FOR_EACH_IMPL (impl, 0)
214 do_one_test (impl, s1, s2, exp_result);
216 if (HP_TIMING_AVAIL)
217 putchar ('\n');
220 static void
221 do_random_tests (void)
223 UCHAR *p1 = (UCHAR *) (buf1 + page_size - 512 * CHARBYTES);
224 UCHAR *p2 = (UCHAR *) (buf2 + page_size - 512 * CHARBYTES);
226 for (size_t n = 0; n < ITERATIONS; n++)
228 /* for wcscmp case align1 and align2 mean here alignment
229 in wchar_t symbols, it equal 4*k alignment in bytes, we
230 don't check other alignments like for example
231 p1 = (wchar_t *)(buf1 + 1)
232 because it's wrong using of wchar_t type. */
233 size_t align1 = random () & 31;
234 size_t align2;
235 if (random () & 1)
236 align2 = random () & 31;
237 else
238 align2 = align1 + (random () & 24);
239 size_t pos = random () & 511;
240 size_t j = align1 > align2 ? align1 : align2;
241 if (pos + j >= 511)
242 pos = 510 - j - (random () & 7);
243 size_t len1 = random () & 511;
244 if (pos >= len1 && (random () & 1))
245 len1 = pos + (random () & 7);
246 if (len1 + j >= 512)
247 len1 = 511 - j - (random () & 7);
248 size_t len2;
249 if (pos >= len1)
250 len2 = len1;
251 else
252 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
253 j = (pos > len2 ? pos : len2) + align1 + 64;
254 if (j > 512)
255 j = 512;
256 for (size_t i = 0; i < j; ++i)
258 p1[i] = random () & 255;
259 if (i < len1 + align1 && !p1[i])
261 p1[i] = random () & 255;
262 if (!p1[i])
263 p1[i] = 1 + (random () & 127);
266 for (size_t i = 0; i < j; ++i)
268 p2[i] = random () & 255;
269 if (i < len2 + align2 && !p2[i])
271 p2[i] = random () & 255;
272 if (!p2[i])
273 p2[i] = 1 + (random () & 127);
277 int result = 0;
278 MEMCPY (p2 + align2, p1 + align1, pos);
279 if (pos < len1)
281 if (p2[align2 + pos] == p1[align1 + pos])
283 p2[align2 + pos] = random () & 255;
284 if (p2[align2 + pos] == p1[align1 + pos])
285 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
288 if (p1[align1 + pos] < p2[align2 + pos])
289 result = -1;
290 else
291 result = 1;
293 p1[len1 + align1] = 0;
294 p2[len2 + align2] = 0;
296 FOR_EACH_IMPL (impl, 1)
298 int r = CALL (impl, (CHAR *) (p1 + align1), (CHAR *) (p2 + align2));
299 /* Test whether on 64-bit architectures where ABI requires
300 callee to promote has the promotion been done. */
301 asm ("" : "=g" (r) : "0" (r));
302 if ((r == 0 && result)
303 || (r < 0 && result >= 0)
304 || (r > 0 && result <= 0))
306 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",
307 n, impl->name, (size_t) (p1 + align1) & 63, (size_t) (p1 + align2) & 63, len1, len2, pos, r, result, p1, p2);
308 ret = 1;
314 static void
315 check (void)
317 CHAR *s1 = (CHAR *) (buf1 + 0xb2c);
318 CHAR *s2 = (CHAR *) (buf1 + 0xfd8);
320 STRCPY(s1, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
321 STRCPY(s2, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
323 /* Check correct working for negatives values */
325 s1[0] = 1;
326 s2[0] = 1;
327 s1[1] = 1;
328 s2[1] = 1;
329 s1[2] = -1;
330 s2[2] = 3;
331 s1[3] = 0;
332 s2[3] = -1;
334 /* Check possible overflow bug, actual more for wcscmp */
336 s1[7] = CHAR__MIN;
337 s2[7] = CHAR__MAX;
339 size_t l1 = STRLEN (s1);
340 size_t l2 = STRLEN (s2);
342 for (size_t i1 = 0; i1 < l1; i1++)
343 for (size_t i2 = 0; i2 < l2; i2++)
345 int exp_result = SIMPLE_STRCMP (s1 + i1, s2 + i2);
346 FOR_EACH_IMPL (impl, 0)
347 check_result (impl, s1 + i1, s2 + i2, exp_result);
353 test_main (void)
355 size_t i;
357 test_init ();
358 check();
360 printf ("%23s", "");
361 FOR_EACH_IMPL (impl, 0)
362 printf ("\t%s", impl->name);
363 putchar ('\n');
365 for (i = 1; i < 32; ++i)
367 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 0);
368 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 1);
369 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, -1);
372 for (i = 1; i < 10 + CHARBYTESLOG; ++i)
374 do_test (0, 0, 2 << i, MIDCHAR, 0);
375 do_test (0, 0, 2 << i, LARGECHAR, 0);
376 do_test (0, 0, 2 << i, MIDCHAR, 1);
377 do_test (0, 0, 2 << i, LARGECHAR, 1);
378 do_test (0, 0, 2 << i, MIDCHAR, -1);
379 do_test (0, 0, 2 << i, LARGECHAR, -1);
380 do_test (0, CHARBYTES * i, 2 << i, MIDCHAR, 1);
381 do_test (CHARBYTES * i, CHARBYTES * (i + 1), 2 << i, LARGECHAR, 1);
384 for (i = 1; i < 8; ++i)
386 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 0);
387 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 0);
388 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 1);
389 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 1);
390 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, -1);
391 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, -1);
394 do_random_tests ();
395 return ret;
398 #include "../test-skeleton.c"