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"
25 /* Naive implementation to verify results. */
27 simple_memccpy (void *dst
, const void *src
, int c
, size_t n
)
33 if ((*d
++ = *s
++) == (char) c
)
39 typedef void *(*proto_t
) (void *, const void *, int c
, size_t);
42 do_one_test (impl_t
*impl
, void *dst
, const void *src
, int c
, size_t len
,
45 void *expect
= len
> n
? NULL
: (char *) dst
+ len
;
46 if (CALL (impl
, dst
, src
, c
, n
) != expect
)
48 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
49 CALL (impl
, dst
, src
, c
, n
), expect
);
54 if (memcmp (dst
, src
, len
> n
? n
: len
) != 0)
56 error (0, 0, "Wrong result in function %s", impl
->name
);
63 do_test (size_t align1
, size_t align2
, int c
, size_t len
, size_t n
,
70 if (align1
+ len
>= page_size
)
74 if (align2
+ len
>= page_size
)
77 s1
= (char *) (buf1
+ align1
);
78 s2
= (char *) (buf2
+ align2
);
80 for (i
= 0; i
< len
- 1; ++i
)
82 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
83 if (s1
[i
] == (char) c
)
87 for (i
= len
; i
+ align1
< page_size
&& i
< len
+ 64; ++i
)
88 s1
[i
] = 32 + 32 * i
% (max_char
- 32);
90 FOR_EACH_IMPL (impl
, 0)
91 do_one_test (impl
, s2
, s1
, c
, len
, n
);
95 do_random_tests (void)
97 size_t i
, j
, n
, align1
, align2
, len
, size
, mode
;
98 unsigned char *p1
= buf1
+ page_size
- 512;
99 unsigned char *p2
= buf2
+ page_size
- 512;
100 unsigned char *res
, c
;
102 for (n
= 0; n
< ITERATIONS
; n
++)
108 size
= random () & 255;
109 align1
= 512 - size
- (random () & 15);
111 align2
= align1
- (random () & 24);
113 align2
= align1
- (random () & 31);
121 len
= size
- (random () & 31);
125 len
= random () & 511;
129 align1
= random () & 31;
131 align2
= random () & 31;
133 align2
= align1
+ (random () & 24);
134 len
= random () & 511;
140 size
= random () & 511;
142 size
= 512 - j
- (random() & 31);
146 if ((mode
& 8) && len
+ j
>= 512)
147 len
= 512 - j
- (random () & 7);
149 j
= len
+ align1
+ 64;
152 for (i
= 0; i
< j
; i
++)
154 if (i
== len
+ align1
)
158 p1
[i
] = random () & 255;
159 if (i
>= align1
&& i
< len
+ align1
&& p1
[i
] == c
)
160 p1
[i
] = (random () & 127) + 3 + c
;
164 FOR_EACH_IMPL (impl
, 1)
166 unsigned char *expect
;
167 memset (p2
- 64, '\1', 512 + 64);
168 res
= CALL (impl
, p2
+ align2
, p1
+ align1
, (char) c
, size
);
172 expect
= p2
+ align2
+ len
+ 1;
176 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %d) %p != %p",
177 n
, impl
->name
, align1
, align2
, len
, size
, c
, res
, expect
);
180 for (j
= 0; j
< align2
+ 64; ++j
)
182 if (p2
[j
- 64] != '\1')
184 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
185 n
, impl
->name
, align1
, align2
, len
);
190 j
= align2
+ len
+ 1;
191 if (size
+ align2
< j
)
197 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
198 n
, impl
->name
, align1
, align2
, len
);
206 if (memcmp (p1
+ align1
, p2
+ align2
, j
))
208 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
209 n
, impl
->name
, align1
, align2
, len
);
224 FOR_EACH_IMPL (impl
, 0)
225 printf ("\t%s", impl
->name
);
228 for (i
= 1; i
< 8; ++i
)
230 do_test (i
, i
, 12, 16, 16, 127);
231 do_test (i
, i
, 23, 16, 16, 255);
232 do_test (i
, 2 * i
, 28, 16, 16, 127);
233 do_test (2 * i
, i
, 31, 16, 16, 255);
234 do_test (8 - i
, 2 * i
, 1, 1 << i
, 2 << i
, 127);
235 do_test (2 * i
, 8 - i
, 17, 2 << i
, 1 << i
, 127);
236 do_test (8 - i
, 2 * i
, 0, 1 << i
, 2 << i
, 255);
237 do_test (2 * i
, 8 - i
, i
, 2 << i
, 1 << i
, 255);
240 for (i
= 1; i
< 8; ++i
)
242 do_test (0, 0, i
, 4 << i
, 8 << i
, 127);
243 do_test (0, 0, i
, 16 << i
, 8 << i
, 127);
244 do_test (8 - i
, 2 * i
, i
, 4 << i
, 8 << i
, 127);
245 do_test (8 - i
, 2 * i
, i
, 16 << i
, 8 << i
, 127);
252 #include <support/test-driver.c>