Fix strchr test
[platform/upstream/glibc.git] / string / test-strchr.c
1 /* Test and measure STRCHR functions.
2    Copyright (C) 1999, 2002, 2003, 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 wcschr 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 #ifndef WIDE
26 # define STRCHR strchr
27 # define STRLEN strlen
28 # define CHAR char
29 # define BIG_CHAR CHAR_MAX
30 # define MIDDLE_CHAR 127
31 # define SMALL_CHAR 23
32 # define UCHAR unsigned char
33 #else
34 # include <wchar.h>
35 # define STRCHR wcschr
36 # define STRLEN wcslen
37 # define CHAR wchar_t
38 # define BIG_CHAR WCHAR_MAX
39 # define MIDDLE_CHAR 1121
40 # define SMALL_CHAR 851
41 # define UCHAR wchar_t
42 #endif
43
44 typedef CHAR *(*proto_t) (const CHAR *, int);
45
46 CHAR *
47 simple_STRCHR (const CHAR *s, int c)
48 {
49   for (; *s != (CHAR) c; ++s)
50     if (*s == '\0')
51       return NULL;
52   return (CHAR *) s;
53 }
54
55 CHAR *
56 stupid_STRCHR (const CHAR *s, int c)
57 {
58   size_t n = STRLEN (s) + 1;
59
60   while (n--)
61     if (*s++ == (CHAR) c)
62       return (CHAR *) s - 1;
63   return NULL;
64 }
65
66 IMPL (stupid_STRCHR, 0)
67 IMPL (simple_STRCHR, 0)
68 IMPL (STRCHR, 1)
69
70 static void
71 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
72 {
73   CHAR *res = CALL (impl, s, c);
74   if (res != exp_res)
75     {
76       error (0, 0, "Wrong result in function %s %p %p", impl->name,
77              res, exp_res);
78       ret = 1;
79       return;
80     }
81
82   if (HP_TIMING_AVAIL)
83     {
84       hp_timing_t start __attribute ((unused));
85       hp_timing_t stop __attribute ((unused));
86       hp_timing_t best_time = ~ (hp_timing_t) 0;
87       size_t i;
88
89       for (i = 0; i < 32; ++i)
90         {
91           HP_TIMING_NOW (start);
92           CALL (impl, s, c);
93           HP_TIMING_NOW (stop);
94           HP_TIMING_BEST (best_time, start, stop);
95         }
96
97       printf ("\t%zd", (size_t) best_time);
98     }
99 }
100
101 static void
102 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
103 /* For wcschr: align here means align not in bytes,
104    but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
105    len for wcschr here isn't in bytes but it's number of wchar_t symbols.  */
106 {
107   size_t i;
108   CHAR *result;
109   CHAR *buf = (CHAR *) buf1;
110   align &= 15;
111   if ((align + len) * sizeof (CHAR) >= page_size)
112     return;
113
114   for (i = 0; i < len; ++i)
115     {
116       buf[align + i] = 32 + 23 * i % max_char;
117       if (buf[align + i] == seek_char)
118         buf[align + i] = seek_char + 1;
119       else if (buf[align + i] == 0)
120         buf[align + i] = 1;
121     }
122   buf[align + len] = 0;
123
124   if (pos < len)
125     {
126       buf[align + pos] = seek_char;
127       result = buf + align + pos;
128     }
129   else if (seek_char == 0)
130     result = buf + align + len;
131   else
132     result = NULL;
133
134   if (HP_TIMING_AVAIL)
135     printf ("Length %4zd, alignment in bytes %2zd:",
136             pos, align * sizeof (CHAR));
137
138   FOR_EACH_IMPL (impl, 0)
139     do_one_test (impl, buf + align, seek_char, result);
140
141   if (HP_TIMING_AVAIL)
142     putchar ('\n');
143 }
144
145 static void
146 do_random_tests (void)
147 {
148   size_t i, j, n, align, pos, len;
149   int seek_char;
150   CHAR *result;
151   UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
152
153   for (n = 0; n < ITERATIONS; n++)
154     {
155       /* For wcschr: align here means align not in bytes, but in wchar_ts,
156          in bytes it will equal to align * (sizeof (wchar_t)).  */
157       align = random () & 15;
158       pos = random () & 511;
159       seek_char = random () & 255;
160       if (pos + align >= 511)
161         pos = 510 - align - (random () & 7);
162       /* len for wcschr here isn't in bytes but it's number of wchar_t
163          symbols.  */
164       len = random () & 511;
165       if ((pos == len && seek_char)
166           || (pos > len && (random () & 1)))
167         len = pos + 1 + (random () & 7);
168       if (len + align >= 512)
169         len = 511 - align - (random () & 7);
170       if (pos == len && seek_char)
171         len = pos + 1;
172       j = (pos > len ? pos : len) + align + 64;
173       if (j > 512)
174         j = 512;
175
176       for (i = 0; i < j; i++)
177         {
178           if (i == pos + align)
179             p[i] = seek_char;
180           else if (i == len + align)
181             p[i] = 0;
182           else
183             {
184               p[i] = random () & 255;
185               if (i < pos + align && p[i] == seek_char)
186                 p[i] = seek_char + 13;
187               if (i < len + align && !p[i])
188                 {
189                   p[i] = seek_char - 13;
190                   if (!p[i])
191                     p[i] = 140;
192                 }
193             }
194         }
195
196       if (pos <= len)
197         result = (CHAR *) (p + pos + align);
198       else if (seek_char == 0)
199         result = (CHAR *) (p + len + align);
200       else
201         result = NULL;
202
203       FOR_EACH_IMPL (impl, 1)
204         if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
205           {
206             error (0, 0, "Iteration %zd - wrong result in function \
207                    %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
208                    n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
209                    CALL (impl, (CHAR *) (p + align), seek_char), result, p);
210             ret = 1;
211           }
212     }
213 }
214
215 int
216 test_main (void)
217 {
218   size_t i;
219
220   test_init ();
221
222   printf ("%20s", "");
223   FOR_EACH_IMPL (impl, 0)
224     printf ("\t%s", impl->name);
225   putchar ('\n');
226
227   for (i = 1; i < 8; ++i)
228     {
229       do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
230       do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
231     }
232
233   for (i = 1; i < 8; ++i)
234     {
235       do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
236       do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
237     }
238
239   for (i = 0; i < 32; ++i)
240     {
241       do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
242       do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
243     }
244
245   for (i = 1; i < 8; ++i)
246     {
247       do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
248       do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
249     }
250
251   for (i = 1; i < 8; ++i)
252     {
253       do_test (i, 64, 256, 0, MIDDLE_CHAR);
254       do_test (i, 64, 256, 0, BIG_CHAR);
255     }
256
257   for (i = 0; i < 32; ++i)
258     {
259       do_test (0, i, i + 1, 0, MIDDLE_CHAR);
260       do_test (0, i, i + 1, 0, BIG_CHAR);
261     }
262
263   do_random_tests ();
264   return ret;
265 }
266
267 #include "../test-skeleton.c"