1 /* Test STRCHR functions.
2 Copyright (C) 1999-2023 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
38 # define simple_STRCHR simple_STRCHRNUL
40 # define STRCHR strchr
41 # endif /* !USE_FOR_STRCHRNUL */
42 # define STRLEN strlen
44 # define BIG_CHAR CHAR_MAX
45 # define MIDDLE_CHAR 127
46 # define SMALL_CHAR 23
47 # define UCHAR unsigned char
51 # ifdef USE_FOR_STRCHRNUL
52 # define STRCHR wcschrnul
53 # define simple_STRCHR simple_WCSCHRNUL
55 # define STRCHR wcschr
56 # endif /* !USE_FOR_STRCHRNUL */
57 # define STRLEN wcslen
59 # define BIG_CHAR WCHAR_MAX
60 # define MIDDLE_CHAR 1121
61 # define SMALL_CHAR 851
62 # define UCHAR wchar_t
66 #ifdef USE_FOR_STRCHRNUL
67 # define NULLRET(endptr) endptr
69 # define NULLRET(endptr) NULL
70 #endif /* !USE_FOR_STRCHRNUL */
73 typedef CHAR
*(*proto_t
) (const CHAR
*, int);
75 /* Naive implementation to verify results. */
77 simple_STRCHR (const CHAR
*s
, int c
)
79 size_t n
= STRLEN (s
) + 1;
83 return (CHAR
*) s
- 1;
84 return NULLRET ((CHAR
*) s
- 1);
90 check_result (impl_t
*impl
, const CHAR
*s
, int c
, const CHAR
*exp_res
)
92 CHAR
*res
= CALL (impl
, s
, c
);
95 error (0, 0, "Wrong result in function %s %#x %p %p", impl
->name
,
104 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, const CHAR
*exp_res
)
106 if (check_result (impl
, s
, c
, exp_res
) < 0)
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. */
118 CHAR
*buf
= (CHAR
*) buf1
;
120 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
123 for (i
= 0; i
< len
; ++i
)
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)
131 buf
[align
+ len
] = 0;
135 buf
[align
+ pos
] = seek_char
;
136 result
= buf
+ align
+ pos
;
138 else if (seek_char
== 0)
139 result
= buf
+ align
+ len
;
141 result
= NULLRET (buf
+ align
+ len
);
143 FOR_EACH_IMPL (impl
, 0)
144 do_one_test (impl
, buf
+ align
, seek_char
, result
);
148 do_random_tests (void)
150 size_t i
, j
, n
, align
, pos
, len
;
153 UCHAR
*p
= (UCHAR
*) (buf1
+ page_size
- 512 * sizeof (CHAR
));
155 for (n
= 0; n
< ITERATIONS
; n
++)
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
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
)
174 j
= (pos
> len
? pos
: len
) + align
+ 64;
178 for (i
= 0; i
< j
; i
++)
180 if (i
== pos
+ align
)
182 else if (i
== len
+ align
)
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
])
191 p
[i
] = seek_char
- 13;
199 result
= (CHAR
*) (p
+ pos
+ align
);
200 else if (seek_char
== 0)
201 result
= (CHAR
*) (p
+ len
+ align
);
203 result
= NULLRET ((CHAR
*) (p
+ len
+ align
));
205 FOR_EACH_IMPL (impl
, 1)
206 if (CALL (impl
, (CHAR
*) (p
+ align
), seek_char
) != result
)
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
);
220 CHAR s
[] __attribute__((aligned(16))) = L ("\xff");
222 CHAR
*exp_result
= simple_STRCHR (s
, c
);
224 FOR_EACH_IMPL (impl
, 0)
225 check_result (impl
, s
, c
, exp_result
);
238 FOR_EACH_IMPL (impl
, 0)
239 printf ("\t%s", impl
->name
);
242 for (i
= 1; i
< 8; ++i
)
244 do_test (0, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
245 do_test (i
, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
248 for (i
= 1; i
< 8; ++i
)
250 do_test (0, 16 << i
, 4096, SMALL_CHAR
, MIDDLE_CHAR
);
251 do_test (i
, 16 << i
, 4096, SMALL_CHAR
, MIDDLE_CHAR
);
254 for (i
= 1; i
< 8; ++i
)
256 do_test (i
, 64, 256, SMALL_CHAR
, MIDDLE_CHAR
);
257 do_test (i
, 64, 256, SMALL_CHAR
, BIG_CHAR
);
260 for (i
= 0; i
< 8; ++i
)
262 do_test (16 * i
, 256, 512, SMALL_CHAR
, MIDDLE_CHAR
);
263 do_test (16 * i
, 256, 512, SMALL_CHAR
, BIG_CHAR
);
266 for (i
= 0; i
< 32; ++i
)
268 do_test (0, i
, i
+ 1, SMALL_CHAR
, MIDDLE_CHAR
);
269 do_test (0, i
, i
+ 1, SMALL_CHAR
, BIG_CHAR
);
272 for (i
= 1; i
< 8; ++i
)
274 do_test (0, 16 << i
, 2048, 0, MIDDLE_CHAR
);
275 do_test (i
, 16 << i
, 2048, 0, MIDDLE_CHAR
);
278 for (i
= 1; i
< 8; ++i
)
280 do_test (0, 16 << i
, 4096, 0, MIDDLE_CHAR
);
281 do_test (i
, 16 << i
, 4096, 0, MIDDLE_CHAR
);
284 for (i
= 1; i
< 8; ++i
)
286 do_test (i
, 64, 256, 0, MIDDLE_CHAR
);
287 do_test (i
, 64, 256, 0, BIG_CHAR
);
290 for (i
= 0; i
< 8; ++i
)
292 do_test (16 * i
, 256, 512, 0, MIDDLE_CHAR
);
293 do_test (16 * i
, 256, 512, 0, BIG_CHAR
);
296 for (i
= 0; i
< 32; ++i
)
298 do_test (0, i
, i
+ 1, 0, MIDDLE_CHAR
);
299 do_test (0, i
, i
+ 1, 0, BIG_CHAR
);
306 #include <support/test-driver.c>