Fix strchr test
[platform/upstream/glibc.git] / string / test-strcmp.c
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.
6
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.
11
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.
16
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
20    02111-1307 USA.  */
21
22 #define TEST_MAIN
23 #include "test-string.h"
24
25 #ifdef WIDE
26 # include <wchar.h>
27
28 # define L(str) L##str
29 # define STRCMP wcscmp
30 # define STRCPY wcscpy
31 # define STRLEN wcslen
32 # define MEMCPY wmemcpy
33 # define SIMPLE_STRCMP simple_wcscmp
34 # define STUPID_STRCMP stupid_wcscmp
35 # define CHAR wchar_t
36 # define UCHAR wchar_t
37 # define CHARBYTES 4
38 # define CHARBYTESLOG 2
39 # define CHARALIGN __alignof__ (CHAR)
40 # define MIDCHAR 0x7fffffff
41 # define LARGECHAR 0xfffffffe
42 # define CHAR__MAX WCHAR_MAX
43 # define CHAR__MIN WCHAR_MIN
44
45 /* Wcscmp uses signed semantics for comparison, not unsigned */
46 /* Avoid using substraction since possible overflow */
47
48 int
49 simple_wcscmp (const wchar_t *s1, const wchar_t *s2)
50 {
51   wchar_t c1, c2;
52   do
53     {
54       c1 = *s1++;
55       c2 = *s2++;
56       if (c2 == L'\0')
57       return c1 - c2;
58     }
59   while (c1 == c2);
60
61   return c1 < c2 ? -1 : 1;
62 }
63
64 int
65 stupid_wcscmp (const wchar_t *s1, const wchar_t *s2)
66 {
67   size_t ns1 = wcslen (s1) + 1;
68   size_t ns2 = wcslen (s2) + 1;
69   size_t n = ns1 < ns2 ? ns1 : ns2;
70   int ret = 0;
71
72   wchar_t c1, c2;
73
74   while (n--) {
75     c1 = *s1++;
76     c2 = *s2++;
77     if ((ret = c1 < c2 ? -1 : c1 == c2 ? 0 : 1) != 0)
78       break;
79   }
80   return ret;
81 }
82
83 #else
84 # define L(str) str
85 # define STRCMP strcmp
86 # define STRCPY strcpy
87 # define STRLEN strlen
88 # define MEMCPY memcpy
89 # define SIMPLE_STRCMP simple_strcmp
90 # define STUPID_STRCMP stupid_strcmp
91 # define CHAR char
92 # define UCHAR unsigned char
93 # define CHARBYTES 1
94 # define CHARBYTESLOG 0
95 # define CHARALIGN 1
96 # define MIDCHAR 0x7f
97 # define LARGECHAR 0xfe
98 # define CHAR__MAX CHAR_MAX
99 # define CHAR__MIN CHAR_MIN
100
101 /* Strcmp uses unsigned semantics for comparison. */
102 int
103 simple_strcmp (const char *s1, const char *s2)
104 {
105   int ret;
106
107   while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++);
108   return ret;
109 }
110
111 int
112 stupid_strcmp (const char *s1, const char *s2)
113 {
114   size_t ns1 = strlen (s1) + 1;
115   size_t ns2 = strlen (s2) + 1;
116   size_t n = ns1 < ns2 ? ns1 : ns2;
117   int ret = 0;
118
119   while (n--)
120     if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0)
121       break;
122   return ret;
123 }
124 #endif
125
126 typedef int (*proto_t) (const CHAR *, const CHAR *);
127
128 IMPL (STUPID_STRCMP, 1)
129 IMPL (SIMPLE_STRCMP, 1)
130 IMPL (STRCMP, 1)
131
132 static int
133 check_result (impl_t *impl,
134              const CHAR *s1, const CHAR *s2,
135              int exp_result)
136 {
137   int result = CALL (impl, s1, s2);
138   if ((exp_result == 0 && result != 0)
139       || (exp_result < 0 && result >= 0)
140       || (exp_result > 0 && result <= 0))
141     {
142       error (0, 0, "Wrong result in function %s %d %d", impl->name,
143              result, exp_result);
144       ret = 1;
145       return -1;
146     }
147
148   return 0;
149 }
150
151 static void
152 do_one_test (impl_t *impl,
153              const CHAR *s1, const CHAR *s2,
154              int exp_result)
155 {
156   if (check_result (impl, s1, s2, exp_result) < 0)
157     return;
158
159   if (HP_TIMING_AVAIL)
160     {
161       hp_timing_t start __attribute ((unused));
162       hp_timing_t stop __attribute ((unused));
163       hp_timing_t best_time = ~ (hp_timing_t) 0;
164       size_t i;
165
166       for (i = 0; i < 32; ++i)
167         {
168           HP_TIMING_NOW (start);
169           CALL (impl, s1, s2);
170           HP_TIMING_NOW (stop);
171           HP_TIMING_BEST (best_time, start, stop);
172         }
173
174       printf ("\t%zd", (size_t) best_time);
175     }
176 }
177
178 static void
179 do_test (size_t align1, size_t align2, size_t len, int max_char,
180          int exp_result)
181 {
182   size_t i;
183
184   CHAR *s1, *s2;
185
186   if (len == 0)
187     return;
188
189   align1 &= 63;
190   if (align1 + (len + 1) * CHARBYTES >= page_size)
191     return;
192
193   align2 &= 63;
194   if (align2 + (len + 1) * CHARBYTES >= page_size)
195     return;
196
197   /* Put them close to the end of page.  */
198   i = align1 + CHARBYTES * (len + 2);
199   s1 = (CHAR *) (buf1 + ((page_size - i) / 16 * 16) + align1);
200   i = align2 + CHARBYTES * (len + 2);
201   s2 = (CHAR *) (buf2 + ((page_size - i) / 16 * 16)  + align2);
202
203   for (i = 0; i < len; i++)
204     s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
205
206   s1[len] = s2[len] = 0;
207   s1[len + 1] = 23;
208   s2[len + 1] = 24 + exp_result;
209   s2[len - 1] -= exp_result;
210
211   if (HP_TIMING_AVAIL)
212     printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
213
214   FOR_EACH_IMPL (impl, 0)
215     do_one_test (impl, s1, s2, exp_result);
216
217   if (HP_TIMING_AVAIL)
218     putchar ('\n');
219 }
220
221 static void
222 do_random_tests (void)
223 {
224   for (size_t a = 0; a < CHARBYTES; a += CHARALIGN)
225     for (size_t b = 0; b < CHARBYTES; b += CHARALIGN)
226       {
227         UCHAR *p1 = (UCHAR *) (buf1 + page_size - 512 * CHARBYTES - a);
228         UCHAR *p2 = (UCHAR *) (buf2 + page_size - 512 * CHARBYTES - b);
229
230         for (size_t n = 0; n < ITERATIONS; n++)
231           {
232             size_t align1 = random () & 31;
233             size_t align2;
234             if (random () & 1)
235               align2 = random () & 31;
236             else
237               align2 = align1 + (random () & 24);
238             size_t pos = random () & 511;
239             size_t j = align1 > align2 ? align1 : align2;
240             if (pos + j >= 511)
241               pos = 510 - j - (random () & 7);
242             size_t len1 = random () & 511;
243             if (pos >= len1 && (random () & 1))
244               len1 = pos + (random () & 7);
245             if (len1 + j >= 512)
246               len1 = 511 - j - (random () & 7);
247             size_t len2;
248             if (pos >= len1)
249               len2 = len1;
250             else
251               len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
252             j = (pos > len2 ? pos : len2) + align1 + 64;
253             if (j > 512)
254               j = 512;
255             for (size_t i = 0; i < j; ++i)
256               {
257                 p1[i] = random () & 255;
258                 if (i < len1 + align1 && !p1[i])
259                   {
260                     p1[i] = random () & 255;
261                     if (!p1[i])
262                       p1[i] = 1 + (random () & 127);
263                   }
264               }
265             for (size_t i = 0; i < j; ++i)
266               {
267                 p2[i] = random () & 255;
268                 if (i < len2 + align2 && !p2[i])
269                   {
270                     p2[i] = random () & 255;
271                     if (!p2[i])
272                       p2[i] = 1 + (random () & 127);
273                   }
274               }
275
276             int result = 0;
277             MEMCPY ((CHAR *) (p2 + align2), (CHAR *) (p1 + align1), pos);
278             if (pos < len1)
279               {
280                 if (p2[align2 + pos] == p1[align1 + pos])
281                   {
282                     p2[align2 + pos] = random () & 255;
283                     if (p2[align2 + pos] == p1[align1 + pos])
284                       p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
285                   }
286
287                 if (p1[align1 + pos] < p2[align2 + pos])
288                   result = -1;
289                 else
290                   result = 1;
291               }
292             p1[len1 + align1] = 0;
293             p2[len2 + align2] = 0;
294
295             FOR_EACH_IMPL (impl, 1)
296               {
297                 int r = CALL (impl, (CHAR *) (p1 + align1), (CHAR *) (p2 + align2));
298                 /* Test whether on 64-bit architectures where ABI requires
299                    callee to promote has the promotion been done.  */
300                 asm ("" : "=g" (r) : "0" (r));
301                 if ((r == 0 && result)
302                     || (r < 0 && result >= 0)
303                     || (r > 0 && result <= 0))
304                   {
305                     error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %d != %d, p1 %p p2 %p",
306                            n, impl->name, (size_t) (p1 + align1) & 63, (size_t) (p1 + align2) & 63, len1, len2, pos, r, result, p1, p2);
307                     ret = 1;
308                   }
309               }
310           }
311       }
312 }
313
314 static void
315 check (void)
316 {
317   CHAR *s1 = (CHAR *) (buf1 + 0xb2c);
318   CHAR *s2 = (CHAR *) (buf1 + 0xfd8);
319
320   STRCPY(s1, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
321   STRCPY(s2, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
322
323   /* Check correct working for negatives values */
324
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;
333
334   /* Check possible overflow bug, actual more for wcscmp */
335
336   s1[7] = CHAR__MIN;
337   s2[7] = CHAR__MAX;
338
339   size_t l1 = STRLEN (s1);
340   size_t l2 = STRLEN (s2);
341
342   for (size_t i1 = 0; i1 < l1; i1++)
343     for (size_t i2 = 0; i2 < l2; i2++)
344       {
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);
348       }
349 }
350
351
352 int
353 test_main (void)
354 {
355   size_t i;
356
357   test_init ();
358   check();
359
360   printf ("%23s", "");
361   FOR_EACH_IMPL (impl, 0)
362     printf ("\t%s", impl->name);
363   putchar ('\n');
364
365   for (i = 1; i < 32; ++i)
366     {
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);
370     }
371
372   for (i = 1; i < 10 + CHARBYTESLOG; ++i)
373     {
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);
382     }
383
384   for (i = 1; i < 8; ++i)
385     {
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);
392     }
393
394   do_random_tests ();
395   return ret;
396 }
397
398 #include "../test-skeleton.c"