]>
git.ipfire.org Git - thirdparty/glibc.git/blob - string/test-memset.c
1 /* Test memset functions.
2 Copyright (C) 1999-2021 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
)
112 CHAR
*tstbuf
= buf
+ 1;
113 CHAR sentinel
= c
- 1;
115 buf
[n
+ 1] = sentinel
;
117 simple_bzero (tstbuf
, n
);
119 if (memcmp (s
, tstbuf
, n
) != 0
120 || buf
[0] != sentinel
121 || buf
[n
+ 1] != sentinel
)
123 CHAR
*res
= CALL (impl
, s
, c
, n
);
125 || SIMPLE_MEMSET (tstbuf
, c
, n
) != tstbuf
126 || MEMCMP (s
, tstbuf
, n
) != 0
127 || buf
[0] != sentinel
128 || buf
[n
+ 1] != sentinel
)
129 #endif /* !TEST_BZERO */
131 error (0, 0, "Wrong result in function %s", impl
->name
);
138 do_test (size_t align
, int c
, size_t len
)
141 if ((align
+ len
) * sizeof (CHAR
) > page_size
)
144 FOR_EACH_IMPL (impl
, 0)
145 do_one_test (impl
, (CHAR
*) (buf1
) + align
, c
, len
);
150 do_random_tests (void)
152 size_t i
, j
, k
, n
, align
, len
, size
;
155 UCHAR
*p2
= (UCHAR
*) buf2
;
157 for (i
= 0; i
< 65536 / sizeof (CHAR
); ++i
)
158 p2
[i
] = random () & BIG_CHAR
;
160 for (n
= 0; n
< ITERATIONS
; n
++)
162 if ((random () & 31) == 0)
163 size
= 65536 / sizeof (CHAR
);
166 p
= (UCHAR
*) (buf1
+ page_size
) - size
;
167 len
= random () & (size
- 1);
168 align
= size
- len
- (random () & 31);
171 if ((random () & 7) == 0)
173 if ((random () & 7) == 0)
176 c
= random () & BIG_CHAR
;
177 o
= random () & BIG_CHAR
;
179 o
= (c
+ 1) & BIG_CHAR
;
180 j
= len
+ align
+ 128;
187 for (i
= k
; i
< align
; ++i
)
189 for (i
= align
+ len
; i
< j
; ++i
)
192 FOR_EACH_IMPL (impl
, 1)
194 for (i
= 0; i
< len
; ++i
)
196 p
[i
+ align
] = p2
[i
];
197 if (p
[i
+ align
] == c
)
200 res
= (UCHAR
*) CALL (impl
, (CHAR
*) p
+ align
, c
, len
);
201 if (res
!= p
+ align
)
203 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
204 n
, impl
->name
, align
, c
, len
, res
, p
+ align
);
207 for (i
= k
; i
< align
; ++i
)
210 error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
211 n
, impl
->name
, align
, c
, len
);
215 for (; i
< align
+ len
; ++i
)
218 error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
219 n
, impl
->name
, align
, c
, len
);
226 error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
227 n
, impl
->name
, align
, c
, len
);
234 #endif /* !TEST_BZERO */
245 FOR_EACH_IMPL (impl
, 0)
246 printf ("\t%s", impl
->name
);
250 for (c
= -65; c
<= 130; c
+= 65)
253 for (i
= 0; i
< 18; ++i
)
254 do_test (0, c
, 1 << i
);
255 for (i
= 1; i
< 64; ++i
)
258 do_test (4096 - i
, c
, i
);
259 do_test (4095, c
, i
);
264 do_test (3, c
, 1024);
276 #include <support/test-driver.c>