Add a testase for BZ #14602
[glibc.git] / string / test-strchr.c
blob161ac458c184340e627bef6c85b09c488ecf3888
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
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 #ifndef WIDE
25 # ifdef USE_FOR_STRCHRNUL
26 # define STRCHR strchrnul
27 # define stupid_STRCHR stupid_STRCHRNUL
28 # define simple_STRCHR simple_STRCHRNUL
29 # else
30 # define STRCHR strchr
31 # endif
32 # define STRLEN strlen
33 # define CHAR char
34 # define BIG_CHAR CHAR_MAX
35 # define MIDDLE_CHAR 127
36 # define SMALL_CHAR 23
37 # define UCHAR unsigned char
38 #else
39 # include <wchar.h>
40 # define STRCHR wcschr
41 # define STRLEN wcslen
42 # define CHAR wchar_t
43 # define BIG_CHAR WCHAR_MAX
44 # define MIDDLE_CHAR 1121
45 # define SMALL_CHAR 851
46 # define UCHAR wchar_t
47 #endif
49 #ifdef USE_FOR_STRCHRNUL
50 # define NULLRET(endptr) endptr
51 #else
52 # define NULLRET(endptr) NULL
53 #endif
56 typedef CHAR *(*proto_t) (const CHAR *, int);
58 CHAR *
59 simple_STRCHR (const CHAR *s, int c)
61 for (; *s != (CHAR) c; ++s)
62 if (*s == '\0')
63 return NULLRET ((CHAR *) s);
64 return (CHAR *) s;
67 CHAR *
68 stupid_STRCHR (const CHAR *s, int c)
70 size_t n = STRLEN (s) + 1;
72 while (n--)
73 if (*s++ == (CHAR) c)
74 return (CHAR *) s - 1;
75 return NULLRET ((CHAR *) s - 1);
78 IMPL (stupid_STRCHR, 0)
79 IMPL (simple_STRCHR, 0)
80 IMPL (STRCHR, 1)
82 static int
83 check_result (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
85 CHAR *res = CALL (impl, s, c);
86 if (res != exp_res)
88 error (0, 0, "Wrong result in function %s %#x %p %p", impl->name,
89 c, res, exp_res);
90 ret = 1;
91 return -1;
93 return 0;
96 static void
97 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
99 if (check_result (impl, s, c, exp_res) < 0)
100 return;
102 if (HP_TIMING_AVAIL)
104 hp_timing_t start __attribute ((unused));
105 hp_timing_t stop __attribute ((unused));
106 hp_timing_t best_time = ~ (hp_timing_t) 0;
107 size_t i;
109 for (i = 0; i < 32; ++i)
111 HP_TIMING_NOW (start);
112 CALL (impl, s, c);
113 HP_TIMING_NOW (stop);
114 HP_TIMING_BEST (best_time, start, stop);
117 printf ("\t%zd", (size_t) best_time);
121 static void
122 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
123 /* For wcschr: align here means align not in bytes,
124 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
125 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
127 size_t i;
128 CHAR *result;
129 CHAR *buf = (CHAR *) buf1;
130 align &= 15;
131 if ((align + len) * sizeof (CHAR) >= page_size)
132 return;
134 for (i = 0; i < len; ++i)
136 buf[align + i] = 32 + 23 * i % max_char;
137 if (buf[align + i] == seek_char)
138 buf[align + i] = seek_char + 1;
139 else if (buf[align + i] == 0)
140 buf[align + i] = 1;
142 buf[align + len] = 0;
144 if (pos < len)
146 buf[align + pos] = seek_char;
147 result = buf + align + pos;
149 else if (seek_char == 0)
150 result = buf + align + len;
151 else
152 result = NULLRET (buf + align + len);
154 if (HP_TIMING_AVAIL)
155 printf ("Length %4zd, alignment in bytes %2zd:",
156 pos, align * sizeof (CHAR));
158 FOR_EACH_IMPL (impl, 0)
159 do_one_test (impl, buf + align, seek_char, result);
161 if (HP_TIMING_AVAIL)
162 putchar ('\n');
165 static void
166 do_random_tests (void)
168 size_t i, j, n, align, pos, len;
169 int seek_char;
170 CHAR *result;
171 UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
173 for (n = 0; n < ITERATIONS; n++)
175 /* For wcschr: align here means align not in bytes, but in wchar_ts,
176 in bytes it will equal to align * (sizeof (wchar_t)). */
177 align = random () & 15;
178 pos = random () & 511;
179 seek_char = random () & 255;
180 if (pos + align >= 511)
181 pos = 510 - align - (random () & 7);
182 /* len for wcschr here isn't in bytes but it's number of wchar_t
183 symbols. */
184 len = random () & 511;
185 if ((pos == len && seek_char)
186 || (pos > len && (random () & 1)))
187 len = pos + 1 + (random () & 7);
188 if (len + align >= 512)
189 len = 511 - align - (random () & 7);
190 if (pos == len && seek_char)
191 len = pos + 1;
192 j = (pos > len ? pos : len) + align + 64;
193 if (j > 512)
194 j = 512;
196 for (i = 0; i < j; i++)
198 if (i == pos + align)
199 p[i] = seek_char;
200 else if (i == len + align)
201 p[i] = 0;
202 else
204 p[i] = random () & 255;
205 if (i < pos + align && p[i] == seek_char)
206 p[i] = seek_char + 13;
207 if (i < len + align && !p[i])
209 p[i] = seek_char - 13;
210 if (!p[i])
211 p[i] = 140;
216 if (pos <= len)
217 result = (CHAR *) (p + pos + align);
218 else if (seek_char == 0)
219 result = (CHAR *) (p + len + align);
220 else
221 result = NULLRET ((CHAR *) (p + len + align));
223 FOR_EACH_IMPL (impl, 1)
224 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
226 error (0, 0, "Iteration %zd - wrong result in function \
227 %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
228 n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
229 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
230 ret = 1;
235 static void
236 check1 (void)
238 char s[] __attribute__((aligned(16))) = "\xff";
239 char c = '\xfe';
240 char *exp_result = stupid_STRCHR (s, c);
242 FOR_EACH_IMPL (impl, 0)
243 check_result (impl, s, c, exp_result);
247 test_main (void)
249 size_t i;
251 test_init ();
253 check1 ();
255 printf ("%20s", "");
256 FOR_EACH_IMPL (impl, 0)
257 printf ("\t%s", impl->name);
258 putchar ('\n');
260 for (i = 1; i < 8; ++i)
262 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
263 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
266 for (i = 1; i < 8; ++i)
268 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
269 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
272 for (i = 0; i < 32; ++i)
274 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
275 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
278 for (i = 1; i < 8; ++i)
280 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
281 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
284 for (i = 1; i < 8; ++i)
286 do_test (i, 64, 256, 0, MIDDLE_CHAR);
287 do_test (i, 64, 256, 0, BIG_CHAR);
290 for (i = 0; i < 32; ++i)
292 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
293 do_test (0, i, i + 1, 0, BIG_CHAR);
296 do_random_tests ();
297 return ret;
300 #include "../test-skeleton.c"