1 /* Test and measure memchr 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/>. */
20 #include <support/xunistd.h>
23 #define TEST_NAME "rawmemchr"
24 #include "test-string.h"
26 typedef char *(*proto_t
) (const char *, int);
27 char *simple_rawmemchr (const char *, int);
29 IMPL (simple_rawmemchr
, 0)
33 simple_rawmemchr (const char *s
, int c
)
37 return (char *) s
- 1;
42 do_one_test (impl_t
*impl
, const char *s
, int c
, char *exp_res
)
44 char *res
= CALL (impl
, s
, c
);
47 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
55 do_test_bz29234 (void)
59 char *buf
= xmmap (0, 8192, PROT_READ
| PROT_WRITE
,
60 MAP_PRIVATE
| MAP_ANONYMOUS
, -1);
62 memset (buf
, -1, 8192);
64 ptr_start
= buf
+ 4096 - 8;
66 /* Out of range matches before the start of a page. */
67 memset (ptr_start
- 8, 0x1, 8);
69 for (j
= 0; j
< 8; ++j
)
71 for (i
= 0; i
< 128; ++i
)
73 ptr_start
[i
+ j
] = 0x1;
75 FOR_EACH_IMPL (impl
, 0)
76 do_one_test (impl
, (char *) (ptr_start
+ j
), 0x1,
79 ptr_start
[i
+ j
] = 0xff;
87 do_test (size_t align
, size_t pos
, size_t len
, int seek_char
)
92 align
&= getpagesize () - 1;
93 if (align
+ len
>= page_size
)
96 for (i
= 0; i
< len
; ++i
)
98 buf1
[align
+ i
] = 1 + 23 * i
% 127;
99 if (buf1
[align
+ i
] == seek_char
)
100 buf1
[align
+ i
] = seek_char
+ 1;
102 buf1
[align
+ len
] = 0;
106 buf1
[align
+ pos
] = seek_char
;
107 buf1
[align
+ len
] = -seek_char
;
108 result
= (char *) (buf1
+ align
+ pos
);
110 FOR_EACH_IMPL (impl
, 0)
111 do_one_test (impl
, (char *) (buf1
+ align
), seek_char
, result
);
115 do_random_tests (void)
117 size_t i
, j
, n
, align
, pos
, len
;
120 unsigned char *p
= buf1
+ page_size
- 512;
122 for (n
= 0; n
< ITERATIONS
; n
++)
124 align
= random () & 15;
125 pos
= random () & 511;
126 if (pos
+ align
>= 512)
127 pos
= 511 - align
- (random () & 7);
128 len
= random () & 511;
129 if (len
+ align
>= 512)
130 len
= 512 - align
- (random () & 7);
133 seek_char
= random () & 255;
134 j
= len
+ align
+ 64;
138 for (i
= 0; i
< j
; i
++)
140 if (i
== pos
+ align
)
144 p
[i
] = random () & 255;
145 if (i
< pos
+ align
&& p
[i
] == seek_char
)
146 p
[i
] = seek_char
+ 13;
152 p
[align
- 1] = seek_char
;
154 p
[align
- 4] = seek_char
;
158 size_t r
= random ();
160 len
= ~(uintptr_t) (p
+ align
) - ((r
>> 5) & 31);
161 result
= (char *) (p
+ pos
+ align
);
163 FOR_EACH_IMPL (impl
, 1)
164 if (CALL (impl
, (char *) (p
+ align
), seek_char
) != result
)
166 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
167 n
, impl
->name
, align
, seek_char
, len
, pos
,
168 CALL (impl
, (char *) (p
+ align
), seek_char
),
175 p
[align
- 1] = seek_char
;
177 p
[align
- 4] = seek_char
;
190 FOR_EACH_IMPL (impl
, 0)
191 printf ("\t%s", impl
->name
);
194 for (i
= 1; i
< 7; ++i
)
196 do_test (0, 16 << i
, 2048, 23);
197 do_test (i
, 64, 256, 23);
198 do_test (0, 16 << i
, 2048, 0);
199 do_test (i
, 64, 256, 0);
201 do_test (getpagesize () - i
, 64, 256, 23);
202 do_test (getpagesize () - i
, 64, 256, 0);
204 for (i
= 1; i
< 32; ++i
)
206 do_test (0, i
, i
+ 1, 23);
207 do_test (0, i
, i
+ 1, 0);
209 do_test (getpagesize () - 7, i
, i
+ 1, 23);
210 do_test (getpagesize () - i
/ 2, i
, i
+ 1, 23);
211 do_test (getpagesize () - i
, i
, i
+ 1, 23);
219 #include <support/test-driver.c>