1 /* Test and measure memccpy 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/>. */
20 #define TEST_NAME "memccpy"
21 #include "test-string.h"
23 void *simple_memccpy (void *, const void *, int, size_t);
24 void *stupid_memccpy (void *, const void *, int, size_t);
26 IMPL (stupid_memccpy
, 0)
27 IMPL (simple_memccpy
, 0)
31 simple_memccpy (void *dst
, const void *src
, int c
, size_t n
)
37 if ((*d
++ = *s
++) == (char) c
)
44 stupid_memccpy (void *dst
, const void *src
, int c
, size_t n
)
46 void *p
= memchr (src
, c
, n
);
49 return mempcpy (dst
, src
, p
- src
+ 1);
55 typedef void *(*proto_t
) (void *, const void *, int c
, size_t);
58 do_one_test (impl_t
*impl
, void *dst
, const void *src
, int c
, size_t len
,
61 void *expect
= len
> n
? NULL
: (char *) dst
+ len
;
62 if (CALL (impl
, dst
, src
, c
, n
) != expect
)
64 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
65 CALL (impl
, dst
, src
, c
, n
), expect
);
70 if (memcmp (dst
, src
, len
> n
? n
: len
) != 0)
72 error (0, 0, "Wrong result in function %s", impl
->name
);
79 do_test (size_t align1
, size_t align2
, int c
, size_t len
, size_t n
,
86 if (align1
+ len
>= page_size
)
90 if (align2
+ len
>= page_size
)
93 s1
= (char *) (buf1
+ align1
);
94 s2
= (char *) (buf2
+ align2
);
96 for (i
= 0; i
< len
- 1; ++i
)
98 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
99 if (s1
[i
] == (char) c
)
103 for (i
= len
; i
+ align1
< page_size
&& i
< len
+ 64; ++i
)
104 s1
[i
] = 32 + 32 * i
% (max_char
- 32);
106 FOR_EACH_IMPL (impl
, 0)
107 do_one_test (impl
, s2
, s1
, c
, len
, n
);
111 do_random_tests (void)
113 size_t i
, j
, n
, align1
, align2
, len
, size
, mode
;
114 unsigned char *p1
= buf1
+ page_size
- 512;
115 unsigned char *p2
= buf2
+ page_size
- 512;
116 unsigned char *res
, c
;
118 for (n
= 0; n
< ITERATIONS
; n
++)
124 size
= random () & 255;
125 align1
= 512 - size
- (random () & 15);
127 align2
= align1
- (random () & 24);
129 align2
= align1
- (random () & 31);
137 len
= size
- (random () & 31);
141 len
= random () & 511;
145 align1
= random () & 31;
147 align2
= random () & 31;
149 align2
= align1
+ (random () & 24);
150 len
= random () & 511;
156 size
= random () & 511;
158 size
= 512 - j
- (random() & 31);
162 if ((mode
& 8) && len
+ j
>= 512)
163 len
= 512 - j
- (random () & 7);
165 j
= len
+ align1
+ 64;
168 for (i
= 0; i
< j
; i
++)
170 if (i
== len
+ align1
)
174 p1
[i
] = random () & 255;
175 if (i
>= align1
&& i
< len
+ align1
&& p1
[i
] == c
)
176 p1
[i
] = (random () & 127) + 3 + c
;
180 FOR_EACH_IMPL (impl
, 1)
182 unsigned char *expect
;
183 memset (p2
- 64, '\1', 512 + 64);
184 res
= CALL (impl
, p2
+ align2
, p1
+ align1
, (char) c
, size
);
188 expect
= p2
+ align2
+ len
+ 1;
192 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %d) %p != %p",
193 n
, impl
->name
, align1
, align2
, len
, size
, c
, res
, expect
);
196 for (j
= 0; j
< align2
+ 64; ++j
)
198 if (p2
[j
- 64] != '\1')
200 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
201 n
, impl
->name
, align1
, align2
, len
);
206 j
= align2
+ len
+ 1;
207 if (size
+ align2
< j
)
213 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
214 n
, impl
->name
, align1
, align2
, len
);
222 if (memcmp (p1
+ align1
, p2
+ align2
, j
))
224 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
225 n
, impl
->name
, align1
, align2
, len
);
240 FOR_EACH_IMPL (impl
, 0)
241 printf ("\t%s", impl
->name
);
244 for (i
= 1; i
< 8; ++i
)
246 do_test (i
, i
, 12, 16, 16, 127);
247 do_test (i
, i
, 23, 16, 16, 255);
248 do_test (i
, 2 * i
, 28, 16, 16, 127);
249 do_test (2 * i
, i
, 31, 16, 16, 255);
250 do_test (8 - i
, 2 * i
, 1, 1 << i
, 2 << i
, 127);
251 do_test (2 * i
, 8 - i
, 17, 2 << i
, 1 << i
, 127);
252 do_test (8 - i
, 2 * i
, 0, 1 << i
, 2 << i
, 255);
253 do_test (2 * i
, 8 - i
, i
, 2 << i
, 1 << i
, 255);
256 for (i
= 1; i
< 8; ++i
)
258 do_test (0, 0, i
, 4 << i
, 8 << i
, 127);
259 do_test (0, 0, i
, 16 << i
, 8 << i
, 127);
260 do_test (8 - i
, 2 * i
, i
, 4 << i
, 8 << i
, 127);
261 do_test (8 - i
, 2 * i
, i
, 16 << i
, 8 << i
, 127);
268 #include <support/test-driver.c>