1 /* Test memchr functions.
2 Copyright (C) 1999-2022 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 SIMPLE_MEMCHR simple_memchr
34 # define BIG_CHAR CHAR_MAX
35 # define SMALL_CHAR 127
38 # define MEMCHR wmemchr
40 # define UCHAR wchar_t
41 # define SIMPLE_MEMCHR simple_wmemchr
42 # define BIG_CHAR WCHAR_MAX
43 # define SMALL_CHAR 1273
46 typedef CHAR
*(*proto_t
) (const CHAR
*, int, size_t);
50 /* Naive implementation to verify results. */
52 SIMPLE_MEMCHR (const CHAR
*s
, int c
, size_t n
)
56 return (CHAR
*) s
- 1;
61 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, size_t n
, CHAR
*exp_res
)
63 CHAR
*res
= CALL (impl
, s
, c
, n
);
66 error (0, 0, "Wrong result in function %s (%p, %d, %zu) -> %p != %p",
67 impl
->name
, s
, c
, n
, res
, exp_res
);
74 do_test (size_t align
, size_t pos
, size_t len
, size_t n
, int seek_char
)
79 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
82 CHAR
*buf
= (CHAR
*) (buf1
);
84 for (i
= 0; i
< len
; ++i
)
86 buf
[align
+ i
] = 1 + 23 * i
% SMALL_CHAR
;
87 if (buf
[align
+ i
] == seek_char
)
88 buf
[align
+ i
] = seek_char
+ 1;
92 if (pos
< MIN(n
, len
))
94 buf
[align
+ pos
] = seek_char
;
95 buf
[align
+ len
] = -seek_char
;
96 result
= (CHAR
*) (buf
+ align
+ pos
);
101 buf
[align
+ len
] = seek_char
;
104 FOR_EACH_IMPL (impl
, 0)
105 do_one_test (impl
, (CHAR
*) (buf
+ align
), seek_char
, n
, result
);
109 do_overflow_tests (void)
112 const size_t one
= 1;
113 uintptr_t buf_addr
= (uintptr_t) buf1
;
115 for (i
= 0; i
< 750; ++i
)
117 do_test (0, i
, 751, SIZE_MAX
- i
, BIG_CHAR
);
118 do_test (0, i
, 751, i
- buf_addr
, BIG_CHAR
);
119 do_test (0, i
, 751, -buf_addr
- i
, BIG_CHAR
);
120 do_test (0, i
, 751, SIZE_MAX
- buf_addr
- i
, BIG_CHAR
);
121 do_test (0, i
, 751, SIZE_MAX
- buf_addr
+ i
, BIG_CHAR
);
124 for (j
= 8 * sizeof(size_t) - 1; j
; --j
)
127 do_test (0, i
, 751, len
- i
, BIG_CHAR
);
128 do_test (0, i
, 751, len
+ i
, BIG_CHAR
);
129 do_test (0, i
, 751, len
- buf_addr
- i
, BIG_CHAR
);
130 do_test (0, i
, 751, len
- buf_addr
+ i
, BIG_CHAR
);
132 do_test (0, i
, 751, ~len
- i
, BIG_CHAR
);
133 do_test (0, i
, 751, ~len
+ i
, BIG_CHAR
);
134 do_test (0, i
, 751, ~len
- buf_addr
- i
, BIG_CHAR
);
135 do_test (0, i
, 751, ~len
- buf_addr
+ i
, BIG_CHAR
);
141 do_random_tests (void)
143 size_t i
, j
, n
, align
, pos
, len
;
146 UCHAR
*p
= (UCHAR
*) (buf1
+ page_size
) - 512;
148 for (n
= 0; n
< ITERATIONS
; n
++)
150 align
= random () & 15;
151 pos
= random () & 511;
152 if (pos
+ align
>= 512)
153 pos
= 511 - align
- (random () & 7);
154 len
= random () & 511;
156 len
= pos
+ (random () & 7);
157 if (len
+ align
>= 512)
158 len
= 512 - align
- (random () & 7);
159 seek_char
= random () & BIG_CHAR
;
160 j
= len
+ align
+ 64;
164 for (i
= 0; i
< j
; i
++)
166 if (i
== pos
+ align
)
170 p
[i
] = random () & BIG_CHAR
;
171 if (i
< pos
+ align
&& p
[i
] == seek_char
)
172 p
[i
] = seek_char
+ 13;
178 size_t r
= random ();
180 len
= ~(uintptr_t) (p
+ align
) - ((r
>> 5) & 31);
181 result
= (CHAR
*) (p
+ pos
+ align
);
186 FOR_EACH_IMPL (impl
, 1)
187 if (CALL (impl
, (CHAR
*) (p
+ align
), seek_char
, len
) != result
)
189 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
190 n
, impl
->name
, align
, seek_char
, len
, pos
,
191 CALL (impl
, (CHAR
*) (p
+ align
), seek_char
, len
),
206 FOR_EACH_IMPL (impl
, 0)
207 printf ("\t%s", impl
->name
);
210 for (i
= 1; i
< 8; ++i
)
213 do_test (i
, i
, 0, 0, 23);
214 do_test (i
, i
, 0, 0, 0);
216 do_test (0, 16 << i
, 2048, 2048, 23);
217 do_test (i
, 64, 256, 256, 23);
218 do_test (0, 16 << i
, 2048, 2048, 0);
219 do_test (i
, 64, 256, 256, 0);
221 /* Check for large input sizes and for these cases we need to
222 make sure the byte is within the size range (that's why
223 7 << i must be smaller than 2048). */
224 do_test (0, 7 << i
, 2048, SIZE_MAX
, 23);
225 do_test (0, 2048 - i
, 2048, SIZE_MAX
, 23);
226 do_test (i
, 64, 256, SIZE_MAX
, 23);
227 do_test (0, 7 << i
, 2048, SIZE_MAX
, 0);
228 do_test (0, 2048 - i
, 2048, SIZE_MAX
, 0);
229 do_test (i
, 64, 256, SIZE_MAX
, 0);
232 for (i
= 1; i
< 64; ++i
)
234 for (j
= 1; j
< 64; j
++)
236 do_test (0, 64 - j
, 64, SIZE_MAX
, 23);
237 do_test (i
, 64 - j
, 64, SIZE_MAX
, 23);
241 for (i
= 1; i
< 32; ++i
)
243 do_test (0, i
, i
+ 1, i
+ 1, 23);
244 do_test (0, i
, i
+ 1, i
+ 1, 0);
247 /* BZ#21182 - wrong overflow calculation for i686 implementation
248 with address near end of the page. */
249 for (i
= 2; i
< 16; ++i
)
251 /* page_size is in fact getpagesize() * 2. */
252 do_test (page_size
/ 2 - i
, i
, i
, 1, 0x9B);
253 do_test (page_size
/ 2 - i
, i
- 1, i
- 1, 1, 0x9B);
254 do_test (page_size
/ 2 - (i
* 4), i
+ 128, i
+ 128, i
, 0x9B);
258 do_overflow_tests ();
262 #include <support/test-driver.c>