Fix trailing whitespace.
[glibc.git] / string / test-memset.c
blobaf85a28762a236cfbcf2872fdeee47ab55f6b64a
1 /* Test and measure memset functions.
2 Copyright (C) 1999-2012 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 simple_memset (char *s, int c, size_t n)
69 char *r = s, *end = s + n;
70 while (r < end)
71 *r++ = c;
72 return s;
75 static void
76 do_one_test (impl_t *impl, char *s, int c __attribute ((unused)), size_t n)
78 char tstbuf[n];
79 #ifdef TEST_BZERO
80 simple_bzero (tstbuf, n);
81 CALL (impl, s, n);
82 if (memcmp (s, tstbuf, n) != 0)
83 #else
84 char *res = CALL (impl, s, c, n);
85 if (res != s
86 || simple_memset (tstbuf, c, n) != tstbuf
87 || memcmp (s, tstbuf, n) != 0)
88 #endif
90 error (0, 0, "Wrong result in function %s", impl->name);
91 ret = 1;
92 return;
95 if (HP_TIMING_AVAIL)
97 hp_timing_t start __attribute ((unused));
98 hp_timing_t stop __attribute ((unused));
99 hp_timing_t best_time = ~ (hp_timing_t) 0;
100 size_t i;
102 for (i = 0; i < 32; ++i)
104 HP_TIMING_NOW (start);
105 #ifdef TEST_BZERO
106 CALL (impl, s, n);
107 #else
108 CALL (impl, s, c, n);
109 #endif
111 HP_TIMING_NOW (stop);
112 HP_TIMING_BEST (best_time, start, stop);
115 printf ("\t%zd", (size_t) best_time);
119 static void
120 do_test (size_t align, int c, size_t len)
122 align &= 7;
123 if (align + len > page_size)
124 return;
126 if (HP_TIMING_AVAIL)
127 printf ("Length %4zd, alignment %2zd, c %2d:", len, align, c);
129 FOR_EACH_IMPL (impl, 0)
130 do_one_test (impl, (char *) buf1 + align, c, len);
132 if (HP_TIMING_AVAIL)
133 putchar ('\n');
136 #ifndef TEST_BZERO
137 static void
138 do_random_tests (void)
140 size_t i, j, k, n, align, len, size;
141 int c, o;
142 unsigned char *p, *res;
144 for (i = 0; i < 65536; ++i)
145 buf2[i] = random () & 255;
147 for (n = 0; n < ITERATIONS; n++)
149 if ((random () & 31) == 0)
150 size = 65536;
151 else
152 size = 512;
153 p = buf1 + page_size - size;
154 len = random () & (size - 1);
155 align = size - len - (random () & 31);
156 if (align > size)
157 align = size - len;
158 if ((random () & 7) == 0)
159 align &= ~63;
160 if ((random () & 7) == 0)
161 c = 0;
162 else
163 c = random () & 255;
164 o = random () & 255;
165 if (o == c)
166 o = (c + 1) & 255;
167 j = len + align + 128;
168 if (j > size)
169 j = size;
170 if (align >= 128)
171 k = align - 128;
172 else
173 k = 0;
174 for (i = k; i < align; ++i)
175 p[i] = o;
176 for (i = align + len; i < j; ++i)
177 p[i] = o;
179 FOR_EACH_IMPL (impl, 1)
181 for (i = 0; i < len; ++i)
183 p[i + align] = buf2[i];
184 if (p[i + align] == c)
185 p[i + align] = o;
187 res = (unsigned char *) CALL (impl, (char *) p + align, c, len);
188 if (res != p + align)
190 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
191 n, impl->name, align, c, len, res, p + align);
192 ret = 1;
194 for (i = k; i < align; ++i)
195 if (p[i] != o)
197 error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
198 n, impl->name, align, c, len);
199 ret = 1;
200 break;
202 for (; i < align + len; ++i)
203 if (p[i] != c)
205 error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
206 n, impl->name, align, c, len);
207 ret = 1;
208 break;
210 for (; i < j; ++i)
211 if (p[i] != o)
213 error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
214 n, impl->name, align, c, len);
215 ret = 1;
216 break;
221 #endif
224 test_main (void)
226 size_t i;
227 int c = 0;
229 test_init ();
231 printf ("%24s", "");
232 FOR_EACH_IMPL (impl, 0)
233 printf ("\t%s", impl->name);
234 putchar ('\n');
236 #ifndef TEST_BZERO
237 for (c = -65; c <= 130; c += 65)
238 #endif
240 for (i = 0; i < 18; ++i)
241 do_test (0, c, 1 << i);
242 for (i = 1; i < 32; ++i)
244 do_test (i, c, i);
245 if (i & (i - 1))
246 do_test (0, c, i);
248 do_test (1, c, 14);
249 do_test (3, c, 1024);
250 do_test (4, c, 64);
251 do_test (2, c, 25);
254 #ifndef TEST_BZERO
255 do_random_tests ();
256 #endif
258 return ret;
261 #include "../test-skeleton.c"