]> git.ipfire.org Git - thirdparty/glibc.git/blame - string/test-strchr.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / string / test-strchr.c
CommitLineData
d23d4ef1 1/* Test STRCHR functions.
04277e02 2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
58ef9ef7
RM
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
2d09f82f 5 Added wcschr support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, 2011
58ef9ef7
RM
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
59ba27a6
PE
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
58ef9ef7
RM
20
21#define TEST_MAIN
69f07e5f
L
22#ifndef WIDE
23# ifdef USE_FOR_STRCHRNUL
24# define TEST_NAME "strchrnul"
25# else
26# define TEST_NAME "strchr"
d23d4ef1 27# endif /* !USE_FOR_STRCHRNUL */
69f07e5f 28#else
d23d4ef1
SL
29# ifdef USE_FOR_STRCHRNUL
30# define TEST_NAME "wcschrnul"
31# else
32# define TEST_NAME "wcschr"
33# endif /* !USE_FOR_STRCHRNUL */
34#endif /* WIDE */
58ef9ef7
RM
35#include "test-string.h"
36
2d09f82f 37#ifndef WIDE
51d91b18
UD
38# ifdef USE_FOR_STRCHRNUL
39# define STRCHR strchrnul
40# define stupid_STRCHR stupid_STRCHRNUL
41# define simple_STRCHR simple_STRCHRNUL
42# else
43# define STRCHR strchr
d23d4ef1 44# endif /* !USE_FOR_STRCHRNUL */
2d09f82f
LD
45# define STRLEN strlen
46# define CHAR char
47# define BIG_CHAR CHAR_MAX
48# define MIDDLE_CHAR 127
49# define SMALL_CHAR 23
50# define UCHAR unsigned char
3b20fd5c 51# define L(s) s
2d09f82f
LD
52#else
53# include <wchar.h>
d23d4ef1
SL
54# ifdef USE_FOR_STRCHRNUL
55# define STRCHR wcschrnul
56# define stupid_STRCHR stupid_WCSCHRNUL
57# define simple_STRCHR simple_WCSCHRNUL
58# else
59# define STRCHR wcschr
60# endif /* !USE_FOR_STRCHRNUL */
2d09f82f
LD
61# define STRLEN wcslen
62# define CHAR wchar_t
63# define BIG_CHAR WCHAR_MAX
64# define MIDDLE_CHAR 1121
65# define SMALL_CHAR 851
66# define UCHAR wchar_t
3b20fd5c 67# define L(s) L ## s
d23d4ef1 68#endif /* WIDE */
2d09f82f 69
51d91b18
UD
70#ifdef USE_FOR_STRCHRNUL
71# define NULLRET(endptr) endptr
72#else
73# define NULLRET(endptr) NULL
d23d4ef1 74#endif /* !USE_FOR_STRCHRNUL */
51d91b18
UD
75
76
2d09f82f
LD
77typedef CHAR *(*proto_t) (const CHAR *, int);
78
79CHAR *
80simple_STRCHR (const CHAR *s, int c)
58ef9ef7 81{
2d09f82f 82 for (; *s != (CHAR) c; ++s)
58ef9ef7 83 if (*s == '\0')
51d91b18 84 return NULLRET ((CHAR *) s);
2d09f82f 85 return (CHAR *) s;
58ef9ef7
RM
86}
87
2d09f82f
LD
88CHAR *
89stupid_STRCHR (const CHAR *s, int c)
e8c1660f 90{
2d09f82f 91 size_t n = STRLEN (s) + 1;
e8c1660f
RM
92
93 while (n--)
2d09f82f
LD
94 if (*s++ == (CHAR) c)
95 return (CHAR *) s - 1;
51d91b18 96 return NULLRET ((CHAR *) s - 1);
e8c1660f
RM
97}
98
37822576
AS
99IMPL (stupid_STRCHR, 0)
100IMPL (simple_STRCHR, 0)
2d09f82f
LD
101IMPL (STRCHR, 1)
102
03759f47
L
103static int
104check_result (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
58ef9ef7 105{
2d09f82f 106 CHAR *res = CALL (impl, s, c);
58ef9ef7
RM
107 if (res != exp_res)
108 {
51d91b18
UD
109 error (0, 0, "Wrong result in function %s %#x %p %p", impl->name,
110 c, res, exp_res);
58ef9ef7 111 ret = 1;
03759f47 112 return -1;
58ef9ef7 113 }
03759f47
L
114 return 0;
115}
116
117static void
118do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
119{
120 if (check_result (impl, s, c, exp_res) < 0)
121 return;
58ef9ef7
RM
122}
123
124static void
125do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
37822576
AS
126/* For wcschr: align here means align not in bytes,
127 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
128 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
58ef9ef7
RM
129{
130 size_t i;
2d09f82f 131 CHAR *result;
37822576 132 CHAR *buf = (CHAR *) buf1;
2d09f82f 133 align &= 15;
37822576 134 if ((align + len) * sizeof (CHAR) >= page_size)
58ef9ef7 135 return;
c9df3df9 136
58ef9ef7
RM
137 for (i = 0; i < len; ++i)
138 {
2d09f82f
LD
139 buf[align + i] = 32 + 23 * i % max_char;
140 if (buf[align + i] == seek_char)
afb05e81 141 buf[align + i] = seek_char + 1;
37822576
AS
142 else if (buf[align + i] == 0)
143 buf[align + i] = 1;
58ef9ef7 144 }
2d09f82f 145 buf[align + len] = 0;
58ef9ef7
RM
146
147 if (pos < len)
148 {
2d09f82f
LD
149 buf[align + pos] = seek_char;
150 result = buf + align + pos;
58ef9ef7
RM
151 }
152 else if (seek_char == 0)
2d09f82f 153 result = buf + align + len;
58ef9ef7 154 else
51d91b18 155 result = NULLRET (buf + align + len);
58ef9ef7 156
58ef9ef7 157 FOR_EACH_IMPL (impl, 0)
2d09f82f 158 do_one_test (impl, buf + align, seek_char, result);
58ef9ef7
RM
159}
160
161static void
162do_random_tests (void)
163{
164 size_t i, j, n, align, pos, len;
165 int seek_char;
2d09f82f
LD
166 CHAR *result;
167 UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
58ef9ef7
RM
168
169 for (n = 0; n < ITERATIONS; n++)
170 {
37822576
AS
171 /* For wcschr: align here means align not in bytes, but in wchar_ts,
172 in bytes it will equal to align * (sizeof (wchar_t)). */
58ef9ef7
RM
173 align = random () & 15;
174 pos = random () & 511;
e8c1660f 175 seek_char = random () & 255;
58ef9ef7
RM
176 if (pos + align >= 511)
177 pos = 510 - align - (random () & 7);
37822576
AS
178 /* len for wcschr here isn't in bytes but it's number of wchar_t
179 symbols. */
58ef9ef7 180 len = random () & 511;
e8c1660f
RM
181 if ((pos == len && seek_char)
182 || (pos > len && (random () & 1)))
183 len = pos + 1 + (random () & 7);
58ef9ef7 184 if (len + align >= 512)
afb05e81 185 len = 511 - align - (random () & 7);
e8c1660f
RM
186 if (pos == len && seek_char)
187 len = pos + 1;
188 j = (pos > len ? pos : len) + align + 64;
58ef9ef7 189 if (j > 512)
afb05e81 190 j = 512;
58ef9ef7
RM
191
192 for (i = 0; i < j; i++)
193 {
194 if (i == pos + align)
195 p[i] = seek_char;
196 else if (i == len + align)
197 p[i] = 0;
198 else
199 {
200 p[i] = random () & 255;
201 if (i < pos + align && p[i] == seek_char)
202 p[i] = seek_char + 13;
203 if (i < len + align && !p[i])
204 {
205 p[i] = seek_char - 13;
206 if (!p[i])
207 p[i] = 140;
208 }
209 }
210 }
211
212 if (pos <= len)
2d09f82f 213 result = (CHAR *) (p + pos + align);
58ef9ef7 214 else if (seek_char == 0)
2d09f82f 215 result = (CHAR *) (p + len + align);
58ef9ef7 216 else
51d91b18 217 result = NULLRET ((CHAR *) (p + len + align));
58ef9ef7
RM
218
219 FOR_EACH_IMPL (impl, 1)
2d09f82f 220 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
58ef9ef7 221 {
2d09f82f 222 error (0, 0, "Iteration %zd - wrong result in function \
afb05e81 223 %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
2d09f82f
LD
224 n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
225 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
58ef9ef7
RM
226 ret = 1;
227 }
228 }
229}
230
03759f47
L
231static void
232check1 (void)
233{
3b20fd5c
JM
234 CHAR s[] __attribute__((aligned(16))) = L ("\xff");
235 CHAR c = L ('\xfe');
236 CHAR *exp_result = stupid_STRCHR (s, c);
03759f47
L
237
238 FOR_EACH_IMPL (impl, 0)
239 check_result (impl, s, c, exp_result);
240}
241
58ef9ef7
RM
242int
243test_main (void)
244{
245 size_t i;
246
247 test_init ();
248
03759f47
L
249 check1 ();
250
58ef9ef7
RM
251 printf ("%20s", "");
252 FOR_EACH_IMPL (impl, 0)
253 printf ("\t%s", impl->name);
254 putchar ('\n');
255
256 for (i = 1; i < 8; ++i)
257 {
2d09f82f
LD
258 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
259 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
58ef9ef7
RM
260 }
261
262 for (i = 1; i < 8; ++i)
263 {
2d09f82f
LD
264 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
265 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
58ef9ef7
RM
266 }
267
268 for (i = 0; i < 32; ++i)
269 {
2d09f82f
LD
270 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
271 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
58ef9ef7
RM
272 }
273
274 for (i = 1; i < 8; ++i)
275 {
2d09f82f
LD
276 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
277 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
58ef9ef7
RM
278 }
279
280 for (i = 1; i < 8; ++i)
281 {
2d09f82f
LD
282 do_test (i, 64, 256, 0, MIDDLE_CHAR);
283 do_test (i, 64, 256, 0, BIG_CHAR);
58ef9ef7
RM
284 }
285
286 for (i = 0; i < 32; ++i)
287 {
2d09f82f
LD
288 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
289 do_test (0, i, i + 1, 0, BIG_CHAR);
58ef9ef7
RM
290 }
291
292 do_random_tests ();
293 return ret;
294}
295
fb82116f 296#include <support/test-driver.c>