1 /* Test STRCHR functions.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #define TEST_MAIN
20 #ifndef WIDE
21 # ifdef USE_FOR_STRCHRNUL
22 # define TEST_NAME "strchrnul"
23 # else
24 # define TEST_NAME "strchr"
25 # endif /* !USE_FOR_STRCHRNUL */
26 #else
27 # ifdef USE_FOR_STRCHRNUL
28 # define TEST_NAME "wcschrnul"
29 # else
30 # define TEST_NAME "wcschr"
31 # endif /* !USE_FOR_STRCHRNUL */
32 #endif /* WIDE */
33 #include "test-string.h"
34
35 #ifndef WIDE
36 # ifdef USE_FOR_STRCHRNUL
37 # define STRCHR strchrnul
38 # define simple_STRCHR simple_STRCHRNUL
39 # else
40 # define STRCHR strchr
41 # endif /* !USE_FOR_STRCHRNUL */
42 # define STRLEN strlen
43 # define CHAR char
44 # define BIG_CHAR CHAR_MAX
45 # define MIDDLE_CHAR 127
46 # define SMALL_CHAR 23
47 # define UCHAR unsigned char
48 # define L(s) s
49 #else
50 # include <wchar.h>
51 # ifdef USE_FOR_STRCHRNUL
52 # define STRCHR wcschrnul
53 # define simple_STRCHR simple_WCSCHRNUL
54 # else
55 # define STRCHR wcschr
56 # endif /* !USE_FOR_STRCHRNUL */
57 # define STRLEN wcslen
58 # define CHAR wchar_t
59 # define BIG_CHAR WCHAR_MAX
60 # define MIDDLE_CHAR 1121
61 # define SMALL_CHAR 851
62 # define UCHAR wchar_t
63 # define L(s) L ## s
64 #endif /* WIDE */
65
66 #ifdef USE_FOR_STRCHRNUL
67 # define NULLRET(endptr) endptr
68 #else
69 # define NULLRET(endptr) NULL
70 #endif /* !USE_FOR_STRCHRNUL */
71
72
73 typedef CHAR *(*proto_t) (const CHAR *, int);
74
75 /* Naive implementation to verify results. */
76 CHAR *
simple_STRCHR(const CHAR * s,int c)77 simple_STRCHR (const CHAR *s, int c)
78 {
79 size_t n = STRLEN (s) + 1;
80
81 while (n--)
82 if (*s++ == (CHAR) c)
83 return (CHAR *) s - 1;
84 return NULLRET ((CHAR *) s - 1);
85 }
86
87 IMPL (STRCHR, 1)
88
89 static int
check_result(impl_t * impl,const CHAR * s,int c,const CHAR * exp_res)90 check_result (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
91 {
92 CHAR *res = CALL (impl, s, c);
93 if (res != exp_res)
94 {
95 error (0, 0, "Wrong result in function %s %#x %p %p", impl->name,
96 c, res, exp_res);
97 ret = 1;
98 return -1;
99 }
100 return 0;
101 }
102
103 static void
do_one_test(impl_t * impl,const CHAR * s,int c,const CHAR * exp_res)104 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
105 {
106 if (check_result (impl, s, c, exp_res) < 0)
107 return;
108 }
109
110 static void
do_test(size_t align,size_t pos,size_t len,int seek_char,int max_char)111 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
112 /* For wcschr: align here means align not in bytes,
113 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
114 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
115 {
116 size_t i;
117 CHAR *result;
118 CHAR *buf = (CHAR *) buf1;
119 align &= 127;
120 if ((align + len) * sizeof (CHAR) >= page_size)
121 return;
122
123 for (i = 0; i < len; ++i)
124 {
125 buf[align + i] = 32 + 23 * i % max_char;
126 if (buf[align + i] == seek_char)
127 buf[align + i] = seek_char + 1;
128 else if (buf[align + i] == 0)
129 buf[align + i] = 1;
130 }
131 buf[align + len] = 0;
132
133 if (pos < len)
134 {
135 buf[align + pos] = seek_char;
136 result = buf + align + pos;
137 }
138 else if (seek_char == 0)
139 result = buf + align + len;
140 else
141 result = NULLRET (buf + align + len);
142
143 FOR_EACH_IMPL (impl, 0)
144 do_one_test (impl, buf + align, seek_char, result);
145 }
146
147 static void
do_random_tests(void)148 do_random_tests (void)
149 {
150 size_t i, j, n, align, pos, len;
151 int seek_char;
152 CHAR *result;
153 UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
154
155 for (n = 0; n < ITERATIONS; n++)
156 {
157 /* For wcschr: align here means align not in bytes, but in wchar_ts,
158 in bytes it will equal to align * (sizeof (wchar_t)). */
159 align = random () & 15;
160 pos = random () & 511;
161 seek_char = random () & 255;
162 if (pos + align >= 511)
163 pos = 510 - align - (random () & 7);
164 /* len for wcschr here isn't in bytes but it's number of wchar_t
165 symbols. */
166 len = random () & 511;
167 if ((pos == len && seek_char)
168 || (pos > len && (random () & 1)))
169 len = pos + 1 + (random () & 7);
170 if (len + align >= 512)
171 len = 511 - align - (random () & 7);
172 if (pos == len && seek_char)
173 len = pos + 1;
174 j = (pos > len ? pos : len) + align + 64;
175 if (j > 512)
176 j = 512;
177
178 for (i = 0; i < j; i++)
179 {
180 if (i == pos + align)
181 p[i] = seek_char;
182 else if (i == len + align)
183 p[i] = 0;
184 else
185 {
186 p[i] = random () & 255;
187 if (i < pos + align && p[i] == seek_char)
188 p[i] = seek_char + 13;
189 if (i < len + align && !p[i])
190 {
191 p[i] = seek_char - 13;
192 if (!p[i])
193 p[i] = 140;
194 }
195 }
196 }
197
198 if (pos <= len)
199 result = (CHAR *) (p + pos + align);
200 else if (seek_char == 0)
201 result = (CHAR *) (p + len + align);
202 else
203 result = NULLRET ((CHAR *) (p + len + align));
204
205 FOR_EACH_IMPL (impl, 1)
206 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
207 {
208 error (0, 0, "Iteration %zd - wrong result in function \
209 %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
210 n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
211 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
212 ret = 1;
213 }
214 }
215 }
216
217 static void
check1(void)218 check1 (void)
219 {
220 CHAR s[] __attribute__((aligned(16))) = L ("\xff");
221 CHAR c = L ('\xfe');
222 CHAR *exp_result = simple_STRCHR (s, c);
223
224 FOR_EACH_IMPL (impl, 0)
225 check_result (impl, s, c, exp_result);
226 }
227
228 int
test_main(void)229 test_main (void)
230 {
231 size_t i;
232
233 test_init ();
234
235 check1 ();
236
237 printf ("%20s", "");
238 FOR_EACH_IMPL (impl, 0)
239 printf ("\t%s", impl->name);
240 putchar ('\n');
241
242 for (i = 1; i < 8; ++i)
243 {
244 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
245 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
246 }
247
248 for (i = 1; i < 8; ++i)
249 {
250 do_test (0, 16 << i, 4096, SMALL_CHAR, MIDDLE_CHAR);
251 do_test (i, 16 << i, 4096, SMALL_CHAR, MIDDLE_CHAR);
252 }
253
254 for (i = 1; i < 8; ++i)
255 {
256 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
257 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
258 }
259
260 for (i = 0; i < 8; ++i)
261 {
262 do_test (16 * i, 256, 512, SMALL_CHAR, MIDDLE_CHAR);
263 do_test (16 * i, 256, 512, SMALL_CHAR, BIG_CHAR);
264 }
265
266 for (i = 0; i < 32; ++i)
267 {
268 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
269 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
270 }
271
272 for (i = 1; i < 8; ++i)
273 {
274 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
275 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
276 }
277
278 for (i = 1; i < 8; ++i)
279 {
280 do_test (0, 16 << i, 4096, 0, MIDDLE_CHAR);
281 do_test (i, 16 << i, 4096, 0, MIDDLE_CHAR);
282 }
283
284 for (i = 1; i < 8; ++i)
285 {
286 do_test (i, 64, 256, 0, MIDDLE_CHAR);
287 do_test (i, 64, 256, 0, BIG_CHAR);
288 }
289
290 for (i = 0; i < 8; ++i)
291 {
292 do_test (16 * i, 256, 512, 0, MIDDLE_CHAR);
293 do_test (16 * i, 256, 512, 0, BIG_CHAR);
294 }
295
296 for (i = 0; i < 32; ++i)
297 {
298 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
299 do_test (0, i, i + 1, 0, BIG_CHAR);
300 }
301
302 do_random_tests ();
303 return ret;
304 }
305
306 #include <support/test-driver.c>
307