1/* Test STRCHR functions.
2 Copyright (C) 1999-2024 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# else
39# define STRCHR strchr
40# endif /* !USE_FOR_STRCHRNUL */
41# define STRLEN strlen
42# define CHAR char
43# define BIG_CHAR CHAR_MAX
44# define MIDDLE_CHAR 127
45# define SMALL_CHAR 23
46# define UCHAR unsigned char
47# define L(s) s
48#else
49# include <wchar.h>
50# ifdef USE_FOR_STRCHRNUL
51# define STRCHR wcschrnul
52# else
53# define STRCHR wcschr
54# endif /* !USE_FOR_STRCHRNUL */
55# define STRLEN wcslen
56# define CHAR wchar_t
57# define BIG_CHAR WCHAR_MAX
58# define MIDDLE_CHAR 1121
59# define SMALL_CHAR 851
60# define UCHAR wchar_t
61# define L(s) L ## s
62#endif /* WIDE */
63
64#ifdef USE_FOR_STRCHRNUL
65# define NULLRET(endptr) endptr
66#else
67# define NULLRET(endptr) NULL
68#endif /* !USE_FOR_STRCHRNUL */
69
70
71typedef CHAR *(*proto_t) (const CHAR *, int);
72
73IMPL (STRCHR, 1)
74
75/* Also check the generic implementation. */
76#undef STRCHR
77#undef weak_alias
78#define weak_alias(a, b)
79#undef libc_hidden_builtin_def
80#define libc_hidden_builtin_def(a)
81#undef libc_hidden_def
82#define libc_hidden_def(a)
83#undef libc_hidden_weak
84#define libc_hidden_weak(a)
85#ifndef WIDE
86# define STRCHRNUL __strchrnul_default
87# include "string/strchrnul.c"
88# ifndef USE_FOR_STRCHRNUL
89# define STRCHR __strchr_default
90# include "string/strchr.c"
91# define STRCHR_DEFAULT STRCHR
92# else
93# define STRCHR_DEFAULT STRCHRNUL
94# endif
95#else
96# ifndef USE_FOR_STRCHRNUL
97# define WCSCHR __wcschr_default
98# include "wcsmbs/wcschr.c"
99# define STRCHR_DEFAULT WCSCHR
100# else
101# define WCSCHRNUL __wcschrnul_default
102# include "wcsmbs/wcschrnul.c"
103# define STRCHR_DEFAULT WCSCHRNUL
104# endif
105#endif
106IMPL (STRCHR_DEFAULT, 1)
107
108static int
109check_result (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
110{
111 CHAR *res = CALL (impl, s, c);
112 if (res != exp_res)
113 {
114 error (status: 0, errnum: 0, format: "Wrong result in function %s(%p) %#x %p %p", impl->name, s,
115 c, res, exp_res);
116 ret = 1;
117 return -1;
118 }
119 return 0;
120}
121
122static int
123do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
124{
125 return check_result (impl, s, c, exp_res);
126}
127
128static void
129do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
130/* For wcschr: align here means align not in bytes,
131 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
132 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
133{
134 size_t i;
135 CHAR *result;
136 CHAR *buf = (CHAR *) buf1;
137 align &= 127;
138 if ((align + len) * sizeof (CHAR) >= page_size)
139 return;
140
141 for (i = 0; i < len; ++i)
142 {
143 buf[align + i] = 32 + 23 * i % max_char;
144 if (buf[align + i] == seek_char)
145 buf[align + i] = seek_char + 1;
146 else if (buf[align + i] == 0)
147 buf[align + i] = 1;
148 }
149 buf[align + len] = 0;
150
151 if (pos < len)
152 {
153 buf[align + pos] = seek_char;
154 result = buf + align + pos;
155 }
156 else if (seek_char == 0)
157 result = buf + align + len;
158 else
159 result = NULLRET (buf + align + len);
160
161 FOR_EACH_IMPL (impl, 0)
162 {
163 if (do_one_test (impl, s: buf + align, c: seek_char, exp_res: result) != 0)
164 {
165 error (status: 0, errnum: 0,
166 format: "\tAlign=%zu, Pos=%zu, Len=%zu, seek=%d, max_char=%d, "
167 "Buf=%p, Res=%p",
168 align, pos, len, seek_char, max_char, buf, result);
169 }
170 }
171}
172
173static void
174do_random_tests (void)
175{
176 size_t i, j, n, align, pos, len;
177 int seek_char;
178 CHAR *result;
179 UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
180
181 for (n = 0; n < ITERATIONS; n++)
182 {
183 /* For wcschr: align here means align not in bytes, but in wchar_ts,
184 in bytes it will equal to align * (sizeof (wchar_t)). */
185 align = random () & 15;
186 pos = random () & 511;
187 seek_char = random () & 255;
188 if (pos + align >= 511)
189 pos = 510 - align - (random () & 7);
190 /* len for wcschr here isn't in bytes but it's number of wchar_t
191 symbols. */
192 len = random () & 511;
193 if ((pos == len && seek_char)
194 || (pos > len && (random () & 1)))
195 len = pos + 1 + (random () & 7);
196 if (len + align >= 512)
197 len = 511 - align - (random () & 7);
198 if (pos == len && seek_char)
199 len = pos + 1;
200 j = (pos > len ? pos : len) + align + 64;
201 if (j > 512)
202 j = 512;
203
204 for (i = 0; i < j; i++)
205 {
206 if (i == pos + align)
207 p[i] = seek_char;
208 else if (i == len + align)
209 p[i] = 0;
210 else
211 {
212 p[i] = random () & 255;
213 if (i < pos + align && p[i] == seek_char)
214 p[i] = seek_char + 13;
215 if (i < len + align && !p[i])
216 {
217 p[i] = seek_char - 13;
218 if (!p[i])
219 p[i] = 140;
220 }
221 }
222 }
223
224 if (pos <= len)
225 result = (CHAR *) (p + pos + align);
226 else if (seek_char == 0)
227 result = (CHAR *) (p + len + align);
228 else
229 result = NULLRET ((CHAR *) (p + len + align));
230
231 FOR_EACH_IMPL (impl, 1)
232 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
233 {
234 error (status: 0, errnum: 0, format: "Iteration %zd - wrong result in function \
235 %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
236 n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
237 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
238 ret = 1;
239 }
240 }
241}
242
243static void
244check1 (void)
245{
246 CHAR s[] __attribute__((aligned(16))) = L ("\xff");
247 CHAR c = L ('\xfe');
248#ifndef USE_FOR_STRCHRNUL
249 CHAR *exp_result = NULL;
250#else
251 CHAR *exp_result = s + STRLEN (s);
252#endif
253
254 FOR_EACH_IMPL (impl, 0)
255 check_result (impl, s, c, exp_res: exp_result);
256}
257
258static void
259check2 (void)
260{
261 CHAR *s = (CHAR *) (buf1 + getpagesize () - 4 * sizeof (CHAR));
262 CHAR *s_begin = (CHAR *) (buf1 + getpagesize () - 64);
263#ifndef USE_FOR_STRCHRNUL
264 CHAR *exp_result = NULL;
265#else
266 CHAR *exp_result = s + 1;
267#endif
268 CHAR val = 0x12;
269 for (; s_begin != s; ++s_begin)
270 *s_begin = val;
271
272 s[0] = val + 1;
273 s[1] = 0;
274 s[2] = val + 1;
275 s[3] = val + 1;
276
277 {
278 FOR_EACH_IMPL (impl, 0)
279 check_result (impl, s, c: val, exp_res: exp_result);
280 }
281 s[3] = val;
282 {
283 FOR_EACH_IMPL (impl, 0)
284 check_result (impl, s, c: val, exp_res: exp_result);
285 }
286 exp_result = s;
287 s[0] = val;
288 {
289 FOR_EACH_IMPL (impl, 0)
290 check_result (impl, s, c: val, exp_res: exp_result);
291 }
292
293 s[3] = val + 1;
294 {
295 FOR_EACH_IMPL (impl, 0)
296 check_result (impl, s, c: val, exp_res: exp_result);
297 }
298
299 s[0] = val + 1;
300 s[1] = val + 1;
301 s[2] = val + 1;
302 s[3] = val + 1;
303 s[4] = val;
304 exp_result = s + 4;
305 {
306 FOR_EACH_IMPL (impl, 0)
307 check_result (impl, s, c: val, exp_res: exp_result);
308 }
309 s[4] = 0;
310#ifndef USE_FOR_STRCHRNUL
311 exp_result = NULL;
312#else
313 exp_result = s + 4;
314#endif
315 {
316 FOR_EACH_IMPL (impl, 0)
317 check_result (impl, s, c: val, exp_res: exp_result);
318 }
319}
320
321int
322test_main (void)
323{
324 size_t i;
325
326 test_init ();
327
328 check1 ();
329 check2 ();
330 printf (format: "%20s", "");
331 FOR_EACH_IMPL (impl, 0)
332 printf (format: "\t%s", impl->name);
333 putchar (c: '\n');
334
335 for (i = 1; i < 8; ++i)
336 {
337 do_test (align: 0, pos: 16 << i, len: 2048, SMALL_CHAR, MIDDLE_CHAR);
338 do_test (align: i, pos: 16 << i, len: 2048, SMALL_CHAR, MIDDLE_CHAR);
339 }
340
341 for (i = 1; i < 8; ++i)
342 {
343 do_test (align: 0, pos: 16 << i, len: 4096, SMALL_CHAR, MIDDLE_CHAR);
344 do_test (align: i, pos: 16 << i, len: 4096, SMALL_CHAR, MIDDLE_CHAR);
345 }
346
347 for (i = 1; i < 8; ++i)
348 {
349 do_test (align: i, pos: 64, len: 256, SMALL_CHAR, MIDDLE_CHAR);
350 do_test (align: i, pos: 64, len: 256, SMALL_CHAR, BIG_CHAR);
351 }
352
353 for (i = 0; i < 8; ++i)
354 {
355 do_test (align: 16 * i, pos: 256, len: 512, SMALL_CHAR, MIDDLE_CHAR);
356 do_test (align: 16 * i, pos: 256, len: 512, SMALL_CHAR, BIG_CHAR);
357 }
358
359 for (i = 0; i < 32; ++i)
360 {
361 do_test (align: 0, pos: i, len: i + 1, SMALL_CHAR, MIDDLE_CHAR);
362 do_test (align: 0, pos: i, len: i + 1, SMALL_CHAR, BIG_CHAR);
363 }
364
365 for (i = 1; i < 8; ++i)
366 {
367 do_test (align: 0, pos: 16 << i, len: 2048, seek_char: 0, MIDDLE_CHAR);
368 do_test (align: i, pos: 16 << i, len: 2048, seek_char: 0, MIDDLE_CHAR);
369 }
370
371 for (i = 1; i < 8; ++i)
372 {
373 do_test (align: 0, pos: 16 << i, len: 4096, seek_char: 0, MIDDLE_CHAR);
374 do_test (align: i, pos: 16 << i, len: 4096, seek_char: 0, MIDDLE_CHAR);
375 }
376
377 for (i = 1; i < 8; ++i)
378 {
379 do_test (align: i, pos: 64, len: 256, seek_char: 0, MIDDLE_CHAR);
380 do_test (align: i, pos: 64, len: 256, seek_char: 0, BIG_CHAR);
381 }
382
383 for (i = 0; i < 8; ++i)
384 {
385 do_test (align: 16 * i, pos: 256, len: 512, seek_char: 0, MIDDLE_CHAR);
386 do_test (align: 16 * i, pos: 256, len: 512, seek_char: 0, BIG_CHAR);
387 }
388
389 for (i = 0; i < 32; ++i)
390 {
391 do_test (align: 0, pos: i, len: i + 1, seek_char: 0, MIDDLE_CHAR);
392 do_test (align: 0, pos: i, len: i + 1, seek_char: 0, BIG_CHAR);
393 }
394
395 do_random_tests ();
396 return ret;
397}
398
399#include <support/test-driver.c>
400

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of glibc/string/test-strchr.c