alpha: Fix all users of SYSCALL_ERROR_HANDLER
[glibc.git] / string / test-strchr.c
blobe544aa7158d9a3934617978db367a0d85b5fc4f8
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 void
83 do_one_test (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;
94 if (HP_TIMING_AVAIL)
96 hp_timing_t start __attribute ((unused));
97 hp_timing_t stop __attribute ((unused));
98 hp_timing_t best_time = ~ (hp_timing_t) 0;
99 size_t i;
101 for (i = 0; i < 32; ++i)
103 HP_TIMING_NOW (start);
104 CALL (impl, s, c);
105 HP_TIMING_NOW (stop);
106 HP_TIMING_BEST (best_time, start, stop);
109 printf ("\t%zd", (size_t) best_time);
113 static void
114 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
115 /* For wcschr: align here means align not in bytes,
116 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
117 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
119 size_t i;
120 CHAR *result;
121 CHAR *buf = (CHAR *) buf1;
122 align &= 15;
123 if ((align + len) * sizeof (CHAR) >= page_size)
124 return;
126 for (i = 0; i < len; ++i)
128 buf[align + i] = 32 + 23 * i % max_char;
129 if (buf[align + i] == seek_char)
130 buf[align + i] = seek_char + 1;
131 else if (buf[align + i] == 0)
132 buf[align + i] = 1;
134 buf[align + len] = 0;
136 if (pos < len)
138 buf[align + pos] = seek_char;
139 result = buf + align + pos;
141 else if (seek_char == 0)
142 result = buf + align + len;
143 else
144 result = NULLRET (buf + align + len);
146 if (HP_TIMING_AVAIL)
147 printf ("Length %4zd, alignment in bytes %2zd:",
148 pos, align * sizeof (CHAR));
150 FOR_EACH_IMPL (impl, 0)
151 do_one_test (impl, buf + align, seek_char, result);
153 if (HP_TIMING_AVAIL)
154 putchar ('\n');
157 static void
158 do_random_tests (void)
160 size_t i, j, n, align, pos, len;
161 int seek_char;
162 CHAR *result;
163 UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
165 for (n = 0; n < ITERATIONS; n++)
167 /* For wcschr: align here means align not in bytes, but in wchar_ts,
168 in bytes it will equal to align * (sizeof (wchar_t)). */
169 align = random () & 15;
170 pos = random () & 511;
171 seek_char = random () & 255;
172 if (pos + align >= 511)
173 pos = 510 - align - (random () & 7);
174 /* len for wcschr here isn't in bytes but it's number of wchar_t
175 symbols. */
176 len = random () & 511;
177 if ((pos == len && seek_char)
178 || (pos > len && (random () & 1)))
179 len = pos + 1 + (random () & 7);
180 if (len + align >= 512)
181 len = 511 - align - (random () & 7);
182 if (pos == len && seek_char)
183 len = pos + 1;
184 j = (pos > len ? pos : len) + align + 64;
185 if (j > 512)
186 j = 512;
188 for (i = 0; i < j; i++)
190 if (i == pos + align)
191 p[i] = seek_char;
192 else if (i == len + align)
193 p[i] = 0;
194 else
196 p[i] = random () & 255;
197 if (i < pos + align && p[i] == seek_char)
198 p[i] = seek_char + 13;
199 if (i < len + align && !p[i])
201 p[i] = seek_char - 13;
202 if (!p[i])
203 p[i] = 140;
208 if (pos <= len)
209 result = (CHAR *) (p + pos + align);
210 else if (seek_char == 0)
211 result = (CHAR *) (p + len + align);
212 else
213 result = NULLRET ((CHAR *) (p + len + align));
215 FOR_EACH_IMPL (impl, 1)
216 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
218 error (0, 0, "Iteration %zd - wrong result in function \
219 %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
220 n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
221 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
222 ret = 1;
228 test_main (void)
230 size_t i;
232 test_init ();
234 printf ("%20s", "");
235 FOR_EACH_IMPL (impl, 0)
236 printf ("\t%s", impl->name);
237 putchar ('\n');
239 for (i = 1; i < 8; ++i)
241 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
242 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
245 for (i = 1; i < 8; ++i)
247 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
248 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
251 for (i = 0; i < 32; ++i)
253 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
254 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
257 for (i = 1; i < 8; ++i)
259 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
260 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
263 for (i = 1; i < 8; ++i)
265 do_test (i, 64, 256, 0, MIDDLE_CHAR);
266 do_test (i, 64, 256, 0, BIG_CHAR);
269 for (i = 0; i < 32; ++i)
271 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
272 do_test (0, i, i + 1, 0, BIG_CHAR);
275 do_random_tests ();
276 return ret;
279 #include "../test-skeleton.c"