1 /* Test memset functions.
2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
22 # ifdef TEST_EXPLICIT_BZERO
23 # define TEST_NAME "explicit_bzero"
25 # define TEST_NAME "bzero"
29 # define TEST_NAME "memset"
31 # define TEST_NAME "wmemset"
33 #endif /* !TEST_BZERO */
34 #define MIN_PAGE_SIZE 131072
35 #include "test-string.h"
38 # define MEMSET memset
40 # define UCHAR unsigned char
41 # define SIMPLE_MEMSET simple_memset
42 # define MEMCMP memcmp
43 # define BIG_CHAR CHAR_MAX
46 # define MEMSET wmemset
48 # define UCHAR wchar_t
49 # define SIMPLE_MEMSET simple_wmemset
50 # define MEMCMP wmemcmp
51 # define BIG_CHAR WCHAR_MAX
54 CHAR
*SIMPLE_MEMSET (CHAR
*, int, size_t);
57 typedef void (*proto_t
) (char *, size_t);
58 void simple_bzero (char *, size_t);
59 void builtin_bzero (char *, size_t);
61 IMPL (simple_bzero
, 0)
62 IMPL (builtin_bzero
, 0)
63 #ifdef TEST_EXPLICIT_BZERO
64 IMPL (explicit_bzero
, 1)
70 simple_bzero (char *s
, size_t n
)
72 SIMPLE_MEMSET (s
, 0, n
);
76 builtin_bzero (char *s
, size_t n
)
78 __builtin_bzero (s
, n
);
81 typedef CHAR
*(*proto_t
) (CHAR
*, int, size_t);
83 IMPL (SIMPLE_MEMSET
, 0)
85 char *builtin_memset (char *, int, size_t);
86 IMPL (builtin_memset
, 0)
92 builtin_memset (char *s
, int c
, size_t n
)
94 return __builtin_memset (s
, c
, n
);
97 #endif /* !TEST_BZERO */
100 inhibit_loop_to_libcall
101 SIMPLE_MEMSET (CHAR
*s
, int c
, size_t n
)
103 CHAR
*r
= s
, *end
= s
+ n
;
110 do_one_test (impl_t
*impl
, CHAR
*s
, int c
__attribute ((unused
)), size_t n
)
114 simple_bzero (tstbuf
, n
);
116 if (memcmp (s
, tstbuf
, n
) != 0)
118 CHAR
*res
= CALL (impl
, s
, c
, n
);
120 || SIMPLE_MEMSET (tstbuf
, c
, n
) != tstbuf
121 || MEMCMP (s
, tstbuf
, n
) != 0)
122 #endif /* !TEST_BZERO */
124 error (0, 0, "Wrong result in function %s", impl
->name
);
131 do_test (size_t align
, int c
, size_t len
)
134 if ((align
+ len
) * sizeof (CHAR
) > page_size
)
137 FOR_EACH_IMPL (impl
, 0)
138 do_one_test (impl
, (CHAR
*) (buf1
) + align
, c
, len
);
143 do_random_tests (void)
145 size_t i
, j
, k
, n
, align
, len
, size
;
148 UCHAR
*p2
= (UCHAR
*) buf2
;
150 for (i
= 0; i
< 65536 / sizeof (CHAR
); ++i
)
151 p2
[i
] = random () & BIG_CHAR
;
153 for (n
= 0; n
< ITERATIONS
; n
++)
155 if ((random () & 31) == 0)
156 size
= 65536 / sizeof (CHAR
);
159 p
= (UCHAR
*) (buf1
+ page_size
) - size
;
160 len
= random () & (size
- 1);
161 align
= size
- len
- (random () & 31);
164 if ((random () & 7) == 0)
166 if ((random () & 7) == 0)
169 c
= random () & BIG_CHAR
;
170 o
= random () & BIG_CHAR
;
172 o
= (c
+ 1) & BIG_CHAR
;
173 j
= len
+ align
+ 128;
180 for (i
= k
; i
< align
; ++i
)
182 for (i
= align
+ len
; i
< j
; ++i
)
185 FOR_EACH_IMPL (impl
, 1)
187 for (i
= 0; i
< len
; ++i
)
189 p
[i
+ align
] = p2
[i
];
190 if (p
[i
+ align
] == c
)
193 res
= (UCHAR
*) CALL (impl
, (CHAR
*) p
+ align
, c
, len
);
194 if (res
!= p
+ align
)
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
);
200 for (i
= k
; i
< align
; ++i
)
203 error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
204 n
, impl
->name
, align
, c
, len
);
208 for (; i
< align
+ len
; ++i
)
211 error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
212 n
, impl
->name
, align
, c
, len
);
219 error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
220 n
, impl
->name
, align
, c
, len
);
227 #endif /* !TEST_BZERO */
238 FOR_EACH_IMPL (impl
, 0)
239 printf ("\t%s", impl
->name
);
243 for (c
= -65; c
<= 130; c
+= 65)
246 for (i
= 0; i
< 18; ++i
)
247 do_test (0, c
, 1 << i
);
248 for (i
= 1; i
< 32; ++i
)
255 do_test (3, c
, 1024);
267 #include <support/test-driver.c>