1 /* Test memchr 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 # define TEST_NAME "memchr"
23 # define TEST_NAME "wmemchr"
26 #include "test-string.h"
30 # define MEMCHR memchr
32 # define UCHAR unsigned char
33 # define BIG_CHAR CHAR_MAX
34 # define SMALL_CHAR 127
37 # define MEMCHR wmemchr
39 # define UCHAR wchar_t
40 # define BIG_CHAR WCHAR_MAX
41 # define SMALL_CHAR 1273
44 typedef CHAR
*(*proto_t
) (const CHAR
*, int, size_t);
48 /* Also check the generic implementation. */
51 #define weak_alias(a, b)
52 #undef libc_hidden_builtin_def
53 #define libc_hidden_builtin_def(a)
54 #undef libc_hidden_def
55 #define libc_hidden_def(a)
56 #undef libc_hidden_weak
57 #define libc_hidden_weak(a)
59 # define MEMCHR __memchr_default
60 # include "string/memchr.c"
61 # define MEMCHR_DEFAULT MEMCHR
63 # define WMEMCHR __wmemchr_default
64 # include "wcsmbs/wmemchr.c"
65 # define MEMCHR_DEFAULT WMEMCHR
67 IMPL (MEMCHR_DEFAULT
, 1)
70 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, size_t n
, CHAR
*exp_res
)
72 CHAR
*res
= CALL (impl
, s
, c
, n
);
75 error (0, 0, "Wrong result in function %s (%p, %d, %zu) -> %p != %p",
76 impl
->name
, s
, c
, n
, res
, exp_res
);
83 do_test (size_t align
, size_t pos
, size_t len
, size_t n
, int seek_char
)
88 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
91 CHAR
*buf
= (CHAR
*) (buf1
);
93 for (i
= 0; i
< len
; ++i
)
95 buf
[align
+ i
] = 1 + 23 * i
% SMALL_CHAR
;
96 if (buf
[align
+ i
] == seek_char
)
97 buf
[align
+ i
] = seek_char
+ 1;
101 if (pos
< MIN(n
, len
))
103 buf
[align
+ pos
] = seek_char
;
104 buf
[align
+ len
] = -seek_char
;
105 result
= (CHAR
*) (buf
+ align
+ pos
);
110 buf
[align
+ len
] = seek_char
;
113 FOR_EACH_IMPL (impl
, 0)
114 do_one_test (impl
, (CHAR
*) (buf
+ align
), seek_char
, n
, result
);
118 do_overflow_tests (void)
121 const size_t one
= 1;
122 uintptr_t buf_addr
= (uintptr_t) buf1
;
124 for (i
= 0; i
< 750; ++i
)
126 do_test (0, i
, 751, SIZE_MAX
- i
, BIG_CHAR
);
127 do_test (0, i
, 751, i
- buf_addr
, BIG_CHAR
);
128 do_test (0, i
, 751, -buf_addr
- i
, BIG_CHAR
);
129 do_test (0, i
, 751, SIZE_MAX
- buf_addr
- i
, BIG_CHAR
);
130 do_test (0, i
, 751, SIZE_MAX
- buf_addr
+ i
, BIG_CHAR
);
133 for (j
= 8 * sizeof(size_t) - 1; j
; --j
)
136 do_test (0, i
, 751, len
- i
, BIG_CHAR
);
137 do_test (0, i
, 751, len
+ i
, BIG_CHAR
);
138 do_test (0, i
, 751, len
- buf_addr
- i
, BIG_CHAR
);
139 do_test (0, i
, 751, len
- buf_addr
+ i
, BIG_CHAR
);
141 do_test (0, i
, 751, ~len
- i
, BIG_CHAR
);
142 do_test (0, i
, 751, ~len
+ i
, BIG_CHAR
);
143 do_test (0, i
, 751, ~len
- buf_addr
- i
, BIG_CHAR
);
144 do_test (0, i
, 751, ~len
- buf_addr
+ i
, BIG_CHAR
);
150 do_random_tests (void)
152 size_t i
, j
, n
, align
, pos
, len
;
155 UCHAR
*p
= (UCHAR
*) (buf1
+ page_size
) - 512;
157 for (n
= 0; n
< ITERATIONS
; n
++)
159 align
= random () & 15;
160 pos
= random () & 511;
161 if (pos
+ align
>= 512)
162 pos
= 511 - align
- (random () & 7);
163 len
= random () & 511;
165 len
= pos
+ (random () & 7);
166 if (len
+ align
>= 512)
167 len
= 512 - align
- (random () & 7);
168 seek_char
= random () & BIG_CHAR
;
169 j
= len
+ align
+ 64;
173 for (i
= 0; i
< j
; i
++)
175 if (i
== pos
+ align
)
179 p
[i
] = random () & BIG_CHAR
;
180 if (i
< pos
+ align
&& p
[i
] == seek_char
)
181 p
[i
] = seek_char
+ 13;
187 size_t r
= random ();
189 len
= ~(uintptr_t) (p
+ align
) - ((r
>> 5) & 31);
190 result
= (CHAR
*) (p
+ pos
+ align
);
195 FOR_EACH_IMPL (impl
, 1)
196 if (CALL (impl
, (CHAR
*) (p
+ align
), seek_char
, len
) != result
)
198 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
199 n
, impl
->name
, align
, seek_char
, len
, pos
,
200 CALL (impl
, (CHAR
*) (p
+ align
), seek_char
, len
),
215 FOR_EACH_IMPL (impl
, 0)
216 printf ("\t%s", impl
->name
);
219 for (i
= 1; i
< 8; ++i
)
222 do_test (i
, i
, 0, 0, 23);
223 do_test (i
, i
, 0, 0, 0);
225 do_test (0, 16 << i
, 2048, 2048, 23);
226 do_test (i
, 64, 256, 256, 23);
227 do_test (0, 16 << i
, 2048, 2048, 0);
228 do_test (i
, 64, 256, 256, 0);
230 /* Check for large input sizes and for these cases we need to
231 make sure the byte is within the size range (that's why
232 7 << i must be smaller than 2048). */
233 do_test (0, 7 << i
, 2048, SIZE_MAX
, 23);
234 do_test (0, 2048 - i
, 2048, SIZE_MAX
, 23);
235 do_test (i
, 64, 256, SIZE_MAX
, 23);
236 do_test (0, 7 << i
, 2048, SIZE_MAX
, 0);
237 do_test (0, 2048 - i
, 2048, SIZE_MAX
, 0);
238 do_test (i
, 64, 256, SIZE_MAX
, 0);
241 for (i
= 1; i
< 64; ++i
)
243 for (j
= 1; j
< 64; j
++)
245 do_test (0, 64 - j
, 64, SIZE_MAX
, 23);
246 do_test (i
, 64 - j
, 64, SIZE_MAX
, 23);
250 for (i
= 1; i
< 32; ++i
)
252 do_test (0, i
, i
+ 1, i
+ 1, 23);
253 do_test (0, i
, i
+ 1, i
+ 1, 0);
256 /* BZ#21182 - wrong overflow calculation for i686 implementation
257 with address near end of the page. */
258 for (i
= 2; i
< 16; ++i
)
260 /* page_size is in fact getpagesize() * 2. */
261 do_test (page_size
/ 2 - i
, i
, i
, 1, 0x9B);
262 do_test (page_size
/ 2 - i
, i
- 1, i
- 1, 1, 0x9B);
263 do_test (page_size
/ 2 - (i
* 4), i
+ 128, i
+ 128, i
, 0x9B);
267 do_overflow_tests ();
271 #include <support/test-driver.c>