]> git.ipfire.org Git - thirdparty/glibc.git/blame - string/test-memset.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / string / test-memset.c
CommitLineData
2e9e1667 1/* Test memset functions.
04277e02 2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
58ef9ef7
RM
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
58ef9ef7
RM
19
20#define TEST_MAIN
69f07e5f 21#ifdef TEST_BZERO
ea1bd74d
ZW
22# ifdef TEST_EXPLICIT_BZERO
23# define TEST_NAME "explicit_bzero"
24# else
25# define TEST_NAME "bzero"
26# endif
69f07e5f 27#else
2e9e1667
SL
28# ifndef WIDE
29# define TEST_NAME "memset"
30# else
31# define TEST_NAME "wmemset"
32# endif /* WIDE */
33#endif /* !TEST_BZERO */
58ef9ef7
RM
34#define MIN_PAGE_SIZE 131072
35#include "test-string.h"
36
2e9e1667
SL
37#ifndef WIDE
38# define MEMSET memset
39# define CHAR char
40# define UCHAR unsigned char
41# define SIMPLE_MEMSET simple_memset
42# define MEMCMP memcmp
43# define BIG_CHAR CHAR_MAX
44#else
45# include <wchar.h>
46# define MEMSET wmemset
47# define CHAR wchar_t
48# define UCHAR wchar_t
49# define SIMPLE_MEMSET simple_wmemset
50# define MEMCMP wmemcmp
51# define BIG_CHAR WCHAR_MAX
52#endif /* WIDE */
53
54CHAR *SIMPLE_MEMSET (CHAR *, int, size_t);
9a387d1f
L
55
56#ifdef TEST_BZERO
57typedef void (*proto_t) (char *, size_t);
58void simple_bzero (char *, size_t);
59void builtin_bzero (char *, size_t);
60
61IMPL (simple_bzero, 0)
62IMPL (builtin_bzero, 0)
ea1bd74d
ZW
63#ifdef TEST_EXPLICIT_BZERO
64IMPL (explicit_bzero, 1)
65#else
9a387d1f 66IMPL (bzero, 1)
ea1bd74d 67#endif
9a387d1f
L
68
69void
70simple_bzero (char *s, size_t n)
71{
2e9e1667 72 SIMPLE_MEMSET (s, 0, n);
9a387d1f
L
73}
74
75void
76builtin_bzero (char *s, size_t n)
77{
78 __builtin_bzero (s, n);
79}
80#else
2e9e1667 81typedef CHAR *(*proto_t) (CHAR *, int, size_t);
58ef9ef7 82
2e9e1667
SL
83IMPL (SIMPLE_MEMSET, 0)
84# ifndef WIDE
85char *builtin_memset (char *, int, size_t);
58ef9ef7 86IMPL (builtin_memset, 0)
2e9e1667
SL
87# endif /* !WIDE */
88IMPL (MEMSET, 1)
58ef9ef7 89
2e9e1667 90# ifndef WIDE
9a387d1f
L
91char *
92builtin_memset (char *s, int c, size_t n)
93{
94 return __builtin_memset (s, c, n);
95}
2e9e1667
SL
96# endif /* !WIDE */
97#endif /* !TEST_BZERO */
9a387d1f 98
2e9e1667 99CHAR *
85c2e611 100inhibit_loop_to_libcall
2e9e1667 101SIMPLE_MEMSET (CHAR *s, int c, size_t n)
58ef9ef7 102{
2e9e1667 103 CHAR *r = s, *end = s + n;
58ef9ef7
RM
104 while (r < end)
105 *r++ = c;
106 return s;
107}
108
58ef9ef7 109static void
2e9e1667 110do_one_test (impl_t *impl, CHAR *s, int c __attribute ((unused)), size_t n)
58ef9ef7 111{
2e9e1667 112 CHAR tstbuf[n];
9a387d1f
L
113#ifdef TEST_BZERO
114 simple_bzero (tstbuf, n);
115 CALL (impl, s, n);
116 if (memcmp (s, tstbuf, n) != 0)
117#else
2e9e1667 118 CHAR *res = CALL (impl, s, c, n);
c158fc76 119 if (res != s
2e9e1667
SL
120 || SIMPLE_MEMSET (tstbuf, c, n) != tstbuf
121 || MEMCMP (s, tstbuf, n) != 0)
122#endif /* !TEST_BZERO */
58ef9ef7 123 {
c158fc76 124 error (0, 0, "Wrong result in function %s", impl->name);
58ef9ef7
RM
125 ret = 1;
126 return;
127 }
58ef9ef7
RM
128}
129
130static void
131do_test (size_t align, int c, size_t len)
132{
133 align &= 7;
2e9e1667 134 if ((align + len) * sizeof (CHAR) > page_size)
58ef9ef7
RM
135 return;
136
58ef9ef7 137 FOR_EACH_IMPL (impl, 0)
2e9e1667 138 do_one_test (impl, (CHAR *) (buf1) + align, c, len);
58ef9ef7
RM
139}
140
9a387d1f 141#ifndef TEST_BZERO
58ef9ef7
RM
142static void
143do_random_tests (void)
144{
145 size_t i, j, k, n, align, len, size;
146 int c, o;
2e9e1667
SL
147 UCHAR *p, *res;
148 UCHAR *p2 = (UCHAR *) buf2;
58ef9ef7 149
2e9e1667
SL
150 for (i = 0; i < 65536 / sizeof (CHAR); ++i)
151 p2[i] = random () & BIG_CHAR;
58ef9ef7
RM
152
153 for (n = 0; n < ITERATIONS; n++)
154 {
155 if ((random () & 31) == 0)
2e9e1667 156 size = 65536 / sizeof (CHAR);
58ef9ef7
RM
157 else
158 size = 512;
2e9e1667 159 p = (UCHAR *) (buf1 + page_size) - size;
58ef9ef7
RM
160 len = random () & (size - 1);
161 align = size - len - (random () & 31);
162 if (align > size)
163 align = size - len;
164 if ((random () & 7) == 0)
165 align &= ~63;
166 if ((random () & 7) == 0)
167 c = 0;
168 else
2e9e1667
SL
169 c = random () & BIG_CHAR;
170 o = random () & BIG_CHAR;
58ef9ef7 171 if (o == c)
2e9e1667 172 o = (c + 1) & BIG_CHAR;
58ef9ef7
RM
173 j = len + align + 128;
174 if (j > size)
175 j = size;
176 if (align >= 128)
177 k = align - 128;
178 else
179 k = 0;
180 for (i = k; i < align; ++i)
181 p[i] = o;
182 for (i = align + len; i < j; ++i)
183 p[i] = o;
184
185 FOR_EACH_IMPL (impl, 1)
186 {
187 for (i = 0; i < len; ++i)
188 {
2e9e1667 189 p[i + align] = p2[i];
58ef9ef7
RM
190 if (p[i + align] == c)
191 p[i + align] = o;
192 }
2e9e1667 193 res = (UCHAR *) CALL (impl, (CHAR *) p + align, c, len);
58ef9ef7
RM
194 if (res != p + align)
195 {
196 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
197 n, impl->name, align, c, len, res, p + align);
198 ret = 1;
199 }
200 for (i = k; i < align; ++i)
201 if (p[i] != o)
202 {
203 error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
204 n, impl->name, align, c, len);
205 ret = 1;
206 break;
207 }
208 for (; i < align + len; ++i)
209 if (p[i] != c)
210 {
211 error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
212 n, impl->name, align, c, len);
213 ret = 1;
214 break;
215 }
216 for (; i < j; ++i)
217 if (p[i] != o)
218 {
219 error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
220 n, impl->name, align, c, len);
221 ret = 1;
222 break;
223 }
224 }
225 }
226}
2e9e1667 227#endif /* !TEST_BZERO */
58ef9ef7
RM
228
229int
230test_main (void)
231{
232 size_t i;
9a387d1f 233 int c = 0;
58ef9ef7
RM
234
235 test_init ();
236
237 printf ("%24s", "");
238 FOR_EACH_IMPL (impl, 0)
239 printf ("\t%s", impl->name);
240 putchar ('\n');
241
9a387d1f 242#ifndef TEST_BZERO
09987e42 243 for (c = -65; c <= 130; c += 65)
9a387d1f 244#endif
58ef9ef7
RM
245 {
246 for (i = 0; i < 18; ++i)
247 do_test (0, c, 1 << i);
248 for (i = 1; i < 32; ++i)
249 {
250 do_test (i, c, i);
251 if (i & (i - 1))
252 do_test (0, c, i);
253 }
254 do_test (1, c, 14);
255 do_test (3, c, 1024);
256 do_test (4, c, 64);
257 do_test (2, c, 25);
258 }
259
9a387d1f 260#ifndef TEST_BZERO
58ef9ef7 261 do_random_tests ();
9a387d1f
L
262#endif
263
58ef9ef7
RM
264 return ret;
265}
266
fb82116f 267#include <support/test-driver.c>