PowerPC: memmove default implementation cleanup
[glibc.git] / string / test-memset.c
blob2171b0d9e671440263836bc96374e05c7faa084d
1 /* Test and measure memset functions.
2 Copyright (C) 1999-2014 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/>. */
20 #define TEST_MAIN
21 #ifdef TEST_BZERO
22 # define TEST_NAME "bzero"
23 #else
24 # define TEST_NAME "memset"
25 #endif
26 #define MIN_PAGE_SIZE 131072
27 #include "test-string.h"
29 char *simple_memset (char *, int, size_t);
31 #ifdef TEST_BZERO
32 typedef void (*proto_t) (char *, size_t);
33 void simple_bzero (char *, size_t);
34 void builtin_bzero (char *, size_t);
36 IMPL (simple_bzero, 0)
37 IMPL (builtin_bzero, 0)
38 IMPL (bzero, 1)
40 void
41 simple_bzero (char *s, size_t n)
43 simple_memset (s, 0, n);
46 void
47 builtin_bzero (char *s, size_t n)
49 __builtin_bzero (s, n);
51 #else
52 typedef char *(*proto_t) (char *, int, size_t);
53 char *builtin_memset (char *, int, size_t);
55 IMPL (simple_memset, 0)
56 IMPL (builtin_memset, 0)
57 IMPL (memset, 1)
59 char *
60 builtin_memset (char *s, int c, size_t n)
62 return __builtin_memset (s, c, n);
64 #endif
66 char *
67 inhibit_loop_to_libcall
68 simple_memset (char *s, int c, size_t n)
70 char *r = s, *end = s + n;
71 while (r < end)
72 *r++ = c;
73 return s;
76 static void
77 do_one_test (impl_t *impl, char *s, int c __attribute ((unused)), size_t n)
79 char tstbuf[n];
80 #ifdef TEST_BZERO
81 simple_bzero (tstbuf, n);
82 CALL (impl, s, n);
83 if (memcmp (s, tstbuf, n) != 0)
84 #else
85 char *res = CALL (impl, s, c, n);
86 if (res != s
87 || simple_memset (tstbuf, c, n) != tstbuf
88 || memcmp (s, tstbuf, n) != 0)
89 #endif
91 error (0, 0, "Wrong result in function %s", impl->name);
92 ret = 1;
93 return;
97 static void
98 do_test (size_t align, int c, size_t len)
100 align &= 7;
101 if (align + len > page_size)
102 return;
104 FOR_EACH_IMPL (impl, 0)
105 do_one_test (impl, (char *) buf1 + align, c, len);
108 #ifndef TEST_BZERO
109 static void
110 do_random_tests (void)
112 size_t i, j, k, n, align, len, size;
113 int c, o;
114 unsigned char *p, *res;
116 for (i = 0; i < 65536; ++i)
117 buf2[i] = random () & 255;
119 for (n = 0; n < ITERATIONS; n++)
121 if ((random () & 31) == 0)
122 size = 65536;
123 else
124 size = 512;
125 p = buf1 + page_size - size;
126 len = random () & (size - 1);
127 align = size - len - (random () & 31);
128 if (align > size)
129 align = size - len;
130 if ((random () & 7) == 0)
131 align &= ~63;
132 if ((random () & 7) == 0)
133 c = 0;
134 else
135 c = random () & 255;
136 o = random () & 255;
137 if (o == c)
138 o = (c + 1) & 255;
139 j = len + align + 128;
140 if (j > size)
141 j = size;
142 if (align >= 128)
143 k = align - 128;
144 else
145 k = 0;
146 for (i = k; i < align; ++i)
147 p[i] = o;
148 for (i = align + len; i < j; ++i)
149 p[i] = o;
151 FOR_EACH_IMPL (impl, 1)
153 for (i = 0; i < len; ++i)
155 p[i + align] = buf2[i];
156 if (p[i + align] == c)
157 p[i + align] = o;
159 res = (unsigned char *) CALL (impl, (char *) p + align, c, len);
160 if (res != p + align)
162 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
163 n, impl->name, align, c, len, res, p + align);
164 ret = 1;
166 for (i = k; i < align; ++i)
167 if (p[i] != o)
169 error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
170 n, impl->name, align, c, len);
171 ret = 1;
172 break;
174 for (; i < align + len; ++i)
175 if (p[i] != c)
177 error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
178 n, impl->name, align, c, len);
179 ret = 1;
180 break;
182 for (; i < j; ++i)
183 if (p[i] != o)
185 error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
186 n, impl->name, align, c, len);
187 ret = 1;
188 break;
193 #endif
196 test_main (void)
198 size_t i;
199 int c = 0;
201 test_init ();
203 printf ("%24s", "");
204 FOR_EACH_IMPL (impl, 0)
205 printf ("\t%s", impl->name);
206 putchar ('\n');
208 #ifndef TEST_BZERO
209 for (c = -65; c <= 130; c += 65)
210 #endif
212 for (i = 0; i < 18; ++i)
213 do_test (0, c, 1 << i);
214 for (i = 1; i < 32; ++i)
216 do_test (i, c, i);
217 if (i & (i - 1))
218 do_test (0, c, i);
220 do_test (1, c, 14);
221 do_test (3, c, 1024);
222 do_test (4, c, 64);
223 do_test (2, c, 25);
226 #ifndef TEST_BZERO
227 do_random_tests ();
228 #endif
230 return ret;
233 #include "../test-skeleton.c"