Fix handling of tail bytes of buffer in SSE2/SSSE3 x86-64 version strn{,case}cmp
[platform/upstream/glibc.git] / string / test-strcasecmp.c
1 /* Test and measure strcasecmp functions.
2    Copyright (C) 1999, 2002, 2003, 2005, 2010 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <ctype.h>
22 #define TEST_MAIN
23 #include "test-string.h"
24
25 typedef int (*proto_t) (const char *, const char *);
26 static int simple_strcasecmp (const char *, const char *);
27 static int stupid_strcasecmp (const char *, const char *);
28
29 IMPL (stupid_strcasecmp, 0)
30 IMPL (simple_strcasecmp, 0)
31 IMPL (strcasecmp, 1)
32
33 static int
34 simple_strcasecmp (const char *s1, const char *s2)
35 {
36   int ret;
37
38   while ((ret = ((unsigned char) tolower (*s1)
39                  - (unsigned char) tolower (*s2))) == 0
40          && *s1++)
41     ++s2;
42   return ret;
43 }
44
45 static int
46 stupid_strcasecmp (const char *s1, const char *s2)
47 {
48   size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1;
49   size_t n = ns1 < ns2 ? ns1 : ns2;
50   int ret = 0;
51
52   while (n--)
53     {
54       if ((ret = ((unsigned char) tolower (*s1)
55                   - (unsigned char) tolower (*s2))) != 0)
56         break;
57       ++s1;
58       ++s2;
59     }
60   return ret;
61 }
62
63 static void
64 do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
65 {
66   int result = CALL (impl, s1, s2);
67   if ((exp_result == 0 && result != 0)
68       || (exp_result < 0 && result >= 0)
69       || (exp_result > 0 && result <= 0))
70     {
71       error (0, 0, "Wrong result in function %s %d %d", impl->name,
72              result, exp_result);
73       ret = 1;
74       return;
75     }
76
77   if (HP_TIMING_AVAIL)
78     {
79       hp_timing_t start __attribute ((unused));
80       hp_timing_t stop __attribute ((unused));
81       hp_timing_t best_time = ~ (hp_timing_t) 0;
82       size_t i;
83
84       for (i = 0; i < 32; ++i)
85         {
86           HP_TIMING_NOW (start);
87           CALL (impl, s1, s2);
88           HP_TIMING_NOW (stop);
89           HP_TIMING_BEST (best_time, start, stop);
90         }
91
92       printf ("\t%zd", (size_t) best_time);
93     }
94 }
95
96 static void
97 do_test (size_t align1, size_t align2, size_t len, int max_char,
98          int exp_result)
99 {
100   size_t i;
101   char *s1, *s2;
102
103   if (len == 0)
104     return;
105
106   align1 &= 7;
107   if (align1 + len + 1 >= page_size)
108     return;
109
110   align2 &= 7;
111   if (align2 + len + 1 >= page_size)
112     return;
113
114   s1 = (char *) (buf1 + align1);
115   s2 = (char *) (buf2 + align2);
116
117   for (i = 0; i < len; i++)
118     {
119       s1[i] = toupper (1 + 23 * i % max_char);
120       s2[i] = tolower (s1[i]);
121     }
122
123   s1[len] = s2[len] = 0;
124   s1[len + 1] = 23;
125   s2[len + 1] = 24 + exp_result;
126   if ((s2[len - 1] == 'z' && exp_result == -1)
127       || (s2[len - 1] == 'a' && exp_result == 1))
128     s1[len - 1] += exp_result;
129   else
130     s2[len - 1] -= exp_result;
131
132   if (HP_TIMING_AVAIL)
133     printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
134
135   FOR_EACH_IMPL (impl, 0)
136     do_one_test (impl, s1, s2, exp_result);
137
138   if (HP_TIMING_AVAIL)
139     putchar ('\n');
140 }
141
142 static void
143 do_random_tests (void)
144 {
145   size_t i, j, n, align1, align2, pos, len1, len2;
146   int result;
147   long r;
148   unsigned char *p1 = buf1 + page_size - 512;
149   unsigned char *p2 = buf2 + page_size - 512;
150
151   for (n = 0; n < ITERATIONS; n++)
152     {
153       align1 = random () & 31;
154       if (random () & 1)
155         align2 = random () & 31;
156       else
157         align2 = align1 + (random () & 24);
158       pos = random () & 511;
159       j = align1 > align2 ? align1 : align2;
160       if (pos + j >= 511)
161         pos = 510 - j - (random () & 7);
162       len1 = random () & 511;
163       if (pos >= len1 && (random () & 1))
164         len1 = pos + (random () & 7);
165       if (len1 + j >= 512)
166         len1 = 511 - j - (random () & 7);
167       if (pos >= len1)
168         len2 = len1;
169       else
170         len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
171       j = (pos > len2 ? pos : len2) + align1 + 64;
172       if (j > 512)
173         j = 512;
174       for (i = 0; i < j; ++i)
175         {
176           p1[i] = tolower (random () & 255);
177           if (i < len1 + align1 && !p1[i])
178             {
179               p1[i] = tolower (random () & 255);
180               if (!p1[i])
181                 p1[i] = tolower (1 + (random () & 127));
182             }
183         }
184       for (i = 0; i < j; ++i)
185         {
186           p2[i] = toupper (random () & 255);
187           if (i < len2 + align2 && !p2[i])
188             {
189               p2[i] = toupper (random () & 255);
190               if (!p2[i])
191                 toupper (p2[i] = 1 + (random () & 127));
192             }
193         }
194
195       result = 0;
196       memcpy (p2 + align2, p1 + align1, pos);
197       if (pos < len1)
198         {
199           if (tolower (p2[align2 + pos]) == p1[align1 + pos])
200             {
201               p2[align2 + pos] = toupper (random () & 255);
202               if (tolower (p2[align2 + pos]) == p1[align1 + pos])
203                 p2[align2 + pos] = toupper (p1[align1 + pos]
204                                             + 3 + (random () & 127));
205             }
206
207           if (p1[align1 + pos] < tolower (p2[align2 + pos]))
208             result = -1;
209           else
210             result = 1;
211         }
212       p1[len1 + align1] = 0;
213       p2[len2 + align2] = 0;
214
215       FOR_EACH_IMPL (impl, 1)
216         {
217           r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2));
218           /* Test whether on 64-bit architectures where ABI requires
219              callee to promote has the promotion been done.  */
220           asm ("" : "=g" (r) : "0" (r));
221           if ((r == 0 && result)
222               || (r < 0 && result >= 0)
223               || (r > 0 && result <= 0))
224             {
225               error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
226                      n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
227               ret = 1;
228             }
229         }
230     }
231 }
232
233 int
234 test_main (void)
235 {
236   size_t i;
237
238   test_init ();
239
240   printf ("%23s", "");
241   FOR_EACH_IMPL (impl, 0)
242     printf ("\t%s", impl->name);
243   putchar ('\n');
244
245   for (i = 1; i < 16; ++i)
246     {
247       do_test (i, i, i, 127, 0);
248       do_test (i, i, i, 127, 1);
249       do_test (i, i, i, 127, -1);
250     }
251
252   for (i = 1; i < 10; ++i)
253     {
254       do_test (0, 0, 2 << i, 127, 0);
255       do_test (0, 0, 2 << i, 254, 0);
256       do_test (0, 0, 2 << i, 127, 1);
257       do_test (0, 0, 2 << i, 254, 1);
258       do_test (0, 0, 2 << i, 127, -1);
259       do_test (0, 0, 2 << i, 254, -1);
260     }
261
262   for (i = 1; i < 8; ++i)
263     {
264       do_test (i, 2 * i, 8 << i, 127, 0);
265       do_test (2 * i, i, 8 << i, 254, 0);
266       do_test (i, 2 * i, 8 << i, 127, 1);
267       do_test (2 * i, i, 8 << i, 254, 1);
268       do_test (i, 2 * i, 8 << i, 127, -1);
269       do_test (2 * i, i, 8 << i, 254, -1);
270     }
271
272   do_random_tests ();
273   return ret;
274 }
275
276 #include "../test-skeleton.c"