1 /* Test memset 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/>. */
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
54 typedef void (*proto_t) (char *, size_t);
55 # ifdef TEST_EXPLICIT_BZERO
56 IMPL (explicit_bzero, 1)
61 typedef CHAR *(*proto_t) (CHAR *, int, size_t);
63 #endif /* !TEST_BZERO */
65 /* Naive implementation to verify results. */
67 inhibit_loop_to_libcall
68 SIMPLE_MEMSET (CHAR *s, int c, size_t n)
70 CHAR *r = s, *end = s + n;
77 do_one_test (impl_t *impl, CHAR *s, int c __attribute ((unused)), size_t n, int space_below, int space_above)
85 SIMPLE_MEMSET(s, ~c, n);
87 SIMPLE_MEMSET (buf, 0, n);
89 if (memcmp (s, buf, n) != 0
90 || (space_below && s[-1] != sentinel)
91 || (space_above && s[n] != sentinel))
93 CHAR *res = CALL (impl, s, c, n);
95 || SIMPLE_MEMSET (buf, c, n) != buf
96 || MEMCMP (s, buf, n) != 0
97 || (space_below && s[-1] != sentinel)
98 || (space_above && s[n] != sentinel))
99 #endif /* !TEST_BZERO */
101 error (0, 0, "Wrong result in function %s", impl->name);
108 do_test (size_t align, int c, size_t len)
110 int space_below, space_above;
112 if ((align + len) * sizeof (CHAR) > page_size)
115 space_below = !!align;
116 space_above = !((align + len + 1) * sizeof (CHAR) > page_size);
118 FOR_EACH_IMPL (impl, 0)
119 do_one_test (impl, (CHAR *) (buf1) + align, c, len, space_below, space_above);
124 do_random_tests (void)
126 size_t i, j, k, n, align, len, size;
129 UCHAR *p2 = (UCHAR *) buf2;
131 for (i = 0; i < 65536 / sizeof (CHAR); ++i)
132 p2[i] = random () & BIG_CHAR;
134 for (n = 0; n < ITERATIONS; n++)
136 if ((random () & 31) == 0)
137 size = 65536 / sizeof (CHAR);
140 p = (UCHAR *) (buf1 + page_size) - size;
141 len = random () & (size - 1);
142 align = size - len - (random () & 31);
145 if ((random () & 7) == 0)
147 if ((random () & 7) == 0)
150 c = random () & BIG_CHAR;
151 o = random () & BIG_CHAR;
153 o = (c + 1) & BIG_CHAR;
154 j = len + align + 128;
161 for (i = k; i < align; ++i)
163 for (i = align + len; i < j; ++i)
166 FOR_EACH_IMPL (impl, 1)
168 for (i = 0; i < len; ++i)
170 p[i + align] = p2[i];
171 if (p[i + align] == c)
174 res = (UCHAR *) CALL (impl, (CHAR *) p + align, c, len);
175 if (res != p + align)
177 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
178 n, impl->name, align, c, len, res, p + align);
181 for (i = k; i < align; ++i)
184 error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
185 n, impl->name, align, c, len);
189 for (; i < align + len; ++i)
192 error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
193 n, impl->name, align, c, len);
200 error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
201 n, impl->name, align, c, len);
208 #endif /* !TEST_BZERO */
219 FOR_EACH_IMPL (impl, 0)
220 printf ("\t%s", impl->name);
224 for (c = -65; c <= 130; c += 65)
227 for (i = 0; i < 18; ++i)
228 do_test (0, c, 1 << i);
229 for (i = 1; i < 64; ++i)
232 do_test (4096 - i, c, i);
233 do_test (4095, c, i);
238 do_test (3, c, 1024);
250 #include <support/test-driver.c>