1 /* Test STRCHR functions.
2 Copyright (C) 1999-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 # ifdef USE_FOR_STRCHRNUL
22 # define TEST_NAME "strchrnul"
24 # define TEST_NAME "strchr"
25 # endif /* !USE_FOR_STRCHRNUL */
27 # ifdef USE_FOR_STRCHRNUL
28 # define TEST_NAME "wcschrnul"
30 # define TEST_NAME "wcschr"
31 # endif /* !USE_FOR_STRCHRNUL */
33 #include "test-string.h"
36 # ifdef USE_FOR_STRCHRNUL
37 # define STRCHR strchrnul
39 # define STRCHR strchr
40 # endif /* !USE_FOR_STRCHRNUL */
41 # define STRLEN strlen
43 # define BIG_CHAR CHAR_MAX
44 # define MIDDLE_CHAR 127
45 # define SMALL_CHAR 23
46 # define UCHAR unsigned char
50 # ifdef USE_FOR_STRCHRNUL
51 # define STRCHR wcschrnul
53 # define STRCHR wcschr
54 # endif /* !USE_FOR_STRCHRNUL */
55 # define STRLEN wcslen
57 # define BIG_CHAR WCHAR_MAX
58 # define MIDDLE_CHAR 1121
59 # define SMALL_CHAR 851
60 # define UCHAR wchar_t
64 #ifdef USE_FOR_STRCHRNUL
65 # define NULLRET(endptr) endptr
67 # define NULLRET(endptr) NULL
68 #endif /* !USE_FOR_STRCHRNUL */
71 typedef CHAR
*(*proto_t
) (const CHAR
*, int);
75 /* Also check the generic implementation. */
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)
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
93 # define STRCHR_DEFAULT STRCHRNUL
96 # ifndef USE_FOR_STRCHRNUL
97 # define WCSCHR __wcschr_default
98 # include "wcsmbs/wcschr.c"
99 # define STRCHR_DEFAULT WCSCHR
101 # define WCSCHRNUL __wcschrnul_default
102 # include "wcsmbs/wcschrnul.c"
103 # define STRCHR_DEFAULT WCSCHRNUL
106 IMPL (STRCHR_DEFAULT
, 1)
109 check_result (impl_t
*impl
, const CHAR
*s
, int c
, const CHAR
*exp_res
)
111 CHAR
*res
= CALL (impl
, s
, c
);
114 error (0, 0, "Wrong result in function %s(%p) %#x %p %p", impl
->name
, s
,
123 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, const CHAR
*exp_res
)
125 return check_result (impl
, s
, c
, exp_res
);
129 do_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. */
136 CHAR
*buf
= (CHAR
*) buf1
;
138 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
141 for (i
= 0; i
< len
; ++i
)
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)
149 buf
[align
+ len
] = 0;
153 buf
[align
+ pos
] = seek_char
;
154 result
= buf
+ align
+ pos
;
156 else if (seek_char
== 0)
157 result
= buf
+ align
+ len
;
159 result
= NULLRET (buf
+ align
+ len
);
161 FOR_EACH_IMPL (impl
, 0)
163 if (do_one_test (impl
, buf
+ align
, seek_char
, result
) != 0)
166 "\tAlign=%zu, Pos=%zu, Len=%zu, seek=%d, max_char=%d, "
168 align
, pos
, len
, seek_char
, max_char
, buf
, result
);
174 do_random_tests (void)
176 size_t i
, j
, n
, align
, pos
, len
;
179 UCHAR
*p
= (UCHAR
*) (buf1
+ page_size
- 512 * sizeof (CHAR
));
181 for (n
= 0; n
< ITERATIONS
; n
++)
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
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
)
200 j
= (pos
> len
? pos
: len
) + align
+ 64;
204 for (i
= 0; i
< j
; i
++)
206 if (i
== pos
+ align
)
208 else if (i
== len
+ align
)
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
])
217 p
[i
] = seek_char
- 13;
225 result
= (CHAR
*) (p
+ pos
+ align
);
226 else if (seek_char
== 0)
227 result
= (CHAR
*) (p
+ len
+ align
);
229 result
= NULLRET ((CHAR
*) (p
+ len
+ align
));
231 FOR_EACH_IMPL (impl
, 1)
232 if (CALL (impl
, (CHAR
*) (p
+ align
), seek_char
) != result
)
234 error (0, 0, "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
);
246 CHAR s
[] __attribute__((aligned(16))) = L ("\xff");
248 #ifndef USE_FOR_STRCHRNUL
249 CHAR
*exp_result
= NULL
;
251 CHAR
*exp_result
= s
+ STRLEN (s
);
254 FOR_EACH_IMPL (impl
, 0)
255 check_result (impl
, s
, c
, exp_result
);
268 FOR_EACH_IMPL (impl
, 0)
269 printf ("\t%s", impl
->name
);
272 for (i
= 1; i
< 8; ++i
)
274 do_test (0, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
275 do_test (i
, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
278 for (i
= 1; i
< 8; ++i
)
280 do_test (0, 16 << i
, 4096, SMALL_CHAR
, MIDDLE_CHAR
);
281 do_test (i
, 16 << i
, 4096, SMALL_CHAR
, MIDDLE_CHAR
);
284 for (i
= 1; i
< 8; ++i
)
286 do_test (i
, 64, 256, SMALL_CHAR
, MIDDLE_CHAR
);
287 do_test (i
, 64, 256, SMALL_CHAR
, BIG_CHAR
);
290 for (i
= 0; i
< 8; ++i
)
292 do_test (16 * i
, 256, 512, SMALL_CHAR
, MIDDLE_CHAR
);
293 do_test (16 * i
, 256, 512, SMALL_CHAR
, BIG_CHAR
);
296 for (i
= 0; i
< 32; ++i
)
298 do_test (0, i
, i
+ 1, SMALL_CHAR
, MIDDLE_CHAR
);
299 do_test (0, i
, i
+ 1, SMALL_CHAR
, BIG_CHAR
);
302 for (i
= 1; i
< 8; ++i
)
304 do_test (0, 16 << i
, 2048, 0, MIDDLE_CHAR
);
305 do_test (i
, 16 << i
, 2048, 0, MIDDLE_CHAR
);
308 for (i
= 1; i
< 8; ++i
)
310 do_test (0, 16 << i
, 4096, 0, MIDDLE_CHAR
);
311 do_test (i
, 16 << i
, 4096, 0, MIDDLE_CHAR
);
314 for (i
= 1; i
< 8; ++i
)
316 do_test (i
, 64, 256, 0, MIDDLE_CHAR
);
317 do_test (i
, 64, 256, 0, BIG_CHAR
);
320 for (i
= 0; i
< 8; ++i
)
322 do_test (16 * i
, 256, 512, 0, MIDDLE_CHAR
);
323 do_test (16 * i
, 256, 512, 0, BIG_CHAR
);
326 for (i
= 0; i
< 32; ++i
)
328 do_test (0, i
, i
+ 1, 0, MIDDLE_CHAR
);
329 do_test (0, i
, i
+ 1, 0, BIG_CHAR
);
336 #include <support/test-driver.c>