1 /* Test memset 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 # ifdef TEST_EXPLICIT_BZERO
22 # define TEST_NAME "explicit_bzero"
24 # define TEST_NAME "bzero"
28 # define TEST_NAME "memset"
30 # define TEST_NAME "wmemset"
32 #endif /* !TEST_BZERO */
33 #define MIN_PAGE_SIZE 131072
34 #include "test-string.h"
37 # define MEMSET memset
39 # define UCHAR unsigned char
40 # define SIMPLE_MEMSET simple_memset
41 # define MEMCMP memcmp
42 # define BIG_CHAR CHAR_MAX
45 # define MEMSET wmemset
47 # define UCHAR wchar_t
48 # define SIMPLE_MEMSET simple_wmemset
49 # define MEMCMP wmemcmp
50 # define BIG_CHAR WCHAR_MAX
53 CHAR
*SIMPLE_MEMSET (CHAR
*, int, size_t);
56 typedef void (*proto_t
) (char *, size_t);
57 void simple_bzero (char *, size_t);
58 void builtin_bzero (char *, size_t);
60 IMPL (simple_bzero
, 0)
61 IMPL (builtin_bzero
, 0)
62 #ifdef TEST_EXPLICIT_BZERO
63 IMPL (explicit_bzero
, 1)
69 simple_bzero (char *s
, size_t n
)
71 SIMPLE_MEMSET (s
, 0, n
);
75 builtin_bzero (char *s
, size_t n
)
77 __builtin_bzero (s
, n
);
80 typedef CHAR
*(*proto_t
) (CHAR
*, int, size_t);
82 IMPL (SIMPLE_MEMSET
, 0)
84 char *builtin_memset (char *, int, size_t);
85 IMPL (builtin_memset
, 0)
91 builtin_memset (char *s
, int c
, size_t n
)
93 return __builtin_memset (s
, c
, n
);
96 #endif /* !TEST_BZERO */
99 inhibit_loop_to_libcall
100 SIMPLE_MEMSET (CHAR
*s
, int c
, size_t n
)
102 CHAR
*r
= s
, *end
= s
+ n
;
109 do_one_test (impl_t
*impl
, CHAR
*s
, int c
__attribute ((unused
)), size_t n
, int space_below
, int space_above
)
117 SIMPLE_MEMSET(s
, ~c
, n
);
119 simple_bzero (buf
, n
);
121 if (memcmp (s
, buf
, n
) != 0
122 || (space_below
&& s
[-1] != sentinel
)
123 || (space_above
&& s
[n
] != sentinel
))
125 CHAR
*res
= CALL (impl
, s
, c
, n
);
127 || SIMPLE_MEMSET (buf
, c
, n
) != buf
128 || MEMCMP (s
, buf
, n
) != 0
129 || (space_below
&& s
[-1] != sentinel
)
130 || (space_above
&& s
[n
] != sentinel
))
131 #endif /* !TEST_BZERO */
133 error (0, 0, "Wrong result in function %s", impl
->name
);
140 do_test (size_t align
, int c
, size_t len
)
142 int space_below
, space_above
;
144 if ((align
+ len
) * sizeof (CHAR
) > page_size
)
147 space_below
= !!align
;
148 space_above
= !((align
+ len
+ 1) * sizeof (CHAR
) > page_size
);
150 FOR_EACH_IMPL (impl
, 0)
151 do_one_test (impl
, (CHAR
*) (buf1
) + align
, c
, len
, space_below
, space_above
);
156 do_random_tests (void)
158 size_t i
, j
, k
, n
, align
, len
, size
;
161 UCHAR
*p2
= (UCHAR
*) buf2
;
163 for (i
= 0; i
< 65536 / sizeof (CHAR
); ++i
)
164 p2
[i
] = random () & BIG_CHAR
;
166 for (n
= 0; n
< ITERATIONS
; n
++)
168 if ((random () & 31) == 0)
169 size
= 65536 / sizeof (CHAR
);
172 p
= (UCHAR
*) (buf1
+ page_size
) - size
;
173 len
= random () & (size
- 1);
174 align
= size
- len
- (random () & 31);
177 if ((random () & 7) == 0)
179 if ((random () & 7) == 0)
182 c
= random () & BIG_CHAR
;
183 o
= random () & BIG_CHAR
;
185 o
= (c
+ 1) & BIG_CHAR
;
186 j
= len
+ align
+ 128;
193 for (i
= k
; i
< align
; ++i
)
195 for (i
= align
+ len
; i
< j
; ++i
)
198 FOR_EACH_IMPL (impl
, 1)
200 for (i
= 0; i
< len
; ++i
)
202 p
[i
+ align
] = p2
[i
];
203 if (p
[i
+ align
] == c
)
206 res
= (UCHAR
*) CALL (impl
, (CHAR
*) p
+ align
, c
, len
);
207 if (res
!= p
+ align
)
209 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
210 n
, impl
->name
, align
, c
, len
, res
, p
+ align
);
213 for (i
= k
; i
< align
; ++i
)
216 error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
217 n
, impl
->name
, align
, c
, len
);
221 for (; i
< align
+ len
; ++i
)
224 error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
225 n
, impl
->name
, align
, c
, len
);
232 error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
233 n
, impl
->name
, align
, c
, len
);
240 #endif /* !TEST_BZERO */
251 FOR_EACH_IMPL (impl
, 0)
252 printf ("\t%s", impl
->name
);
256 for (c
= -65; c
<= 130; c
+= 65)
259 for (i
= 0; i
< 18; ++i
)
260 do_test (0, c
, 1 << i
);
261 for (i
= 1; i
< 64; ++i
)
264 do_test (4096 - i
, c
, i
);
265 do_test (4095, c
, i
);
270 do_test (3, c
, 1024);
282 #include <support/test-driver.c>