* Makerules (common-before-compile): New variable.
[glibc.git] / string / test-memset.c
blob9ae774162f55de1d1005c923c775c40723c26716
1 /* Test and measure memset functions.
2 Copyright (C) 1999, 2002, 2003 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #define TEST_MAIN
22 #define MIN_PAGE_SIZE 131072
23 #include "test-string.h"
25 typedef char *(*proto_t) (char *, int, size_t);
26 char *simple_memset (char *, int, size_t);
27 char *builtin_memset (char *, int, size_t);
29 IMPL (simple_memset, 0)
30 IMPL (builtin_memset, 0)
31 IMPL (memset, 1)
33 char *
34 simple_memset (char *s, int c, size_t n)
36 char *r = s, *end = s + n;
37 while (r < end)
38 *r++ = c;
39 return s;
42 char *
43 builtin_memset (char *s, int c, size_t n)
45 return __builtin_memset (s, c, n);
48 static void
49 do_one_test (impl_t *impl, char *s, int c, size_t n)
51 char *res = CALL (impl, s, c, n);
52 if (res != s)
54 error (0, 0, "Wrong result in function %s %p != %p", impl->name,
55 res, s);
56 ret = 1;
57 return;
60 if (HP_TIMING_AVAIL)
62 hp_timing_t start __attribute ((unused));
63 hp_timing_t stop __attribute ((unused));
64 hp_timing_t best_time = ~ (hp_timing_t) 0;
65 size_t i;
67 for (i = 0; i < 32; ++i)
69 HP_TIMING_NOW (start);
70 CALL (impl, s, c, n);
71 HP_TIMING_NOW (stop);
72 HP_TIMING_BEST (best_time, start, stop);
75 printf ("\t%zd", (size_t) best_time);
79 static void
80 do_test (size_t align, int c, size_t len)
82 align &= 7;
83 if (align + len > page_size)
84 return;
86 if (HP_TIMING_AVAIL)
87 printf ("Length %4zd, alignment %2zd, c %2d:", len, align, c);
89 FOR_EACH_IMPL (impl, 0)
90 do_one_test (impl, buf1 + align, c, len);
92 if (HP_TIMING_AVAIL)
93 putchar ('\n');
96 static void
97 do_random_tests (void)
99 size_t i, j, k, n, align, len, size;
100 int c, o;
101 unsigned char *p, *res;
103 for (i = 0; i < 65536; ++i)
104 buf2[i] = random () & 255;
106 for (n = 0; n < ITERATIONS; n++)
108 if ((random () & 31) == 0)
109 size = 65536;
110 else
111 size = 512;
112 p = buf1 + page_size - size;
113 len = random () & (size - 1);
114 align = size - len - (random () & 31);
115 if (align > size)
116 align = size - len;
117 if ((random () & 7) == 0)
118 align &= ~63;
119 if ((random () & 7) == 0)
120 c = 0;
121 else
122 c = random () & 255;
123 o = random () & 255;
124 if (o == c)
125 o = (c + 1) & 255;
126 j = len + align + 128;
127 if (j > size)
128 j = size;
129 if (align >= 128)
130 k = align - 128;
131 else
132 k = 0;
133 for (i = k; i < align; ++i)
134 p[i] = o;
135 for (i = align + len; i < j; ++i)
136 p[i] = o;
138 FOR_EACH_IMPL (impl, 1)
140 for (i = 0; i < len; ++i)
142 p[i + align] = buf2[i];
143 if (p[i + align] == c)
144 p[i + align] = o;
146 res = CALL (impl, p + align, c, len);
147 if (res != p + align)
149 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd) %p != %p",
150 n, impl->name, align, c, len, res, p + align);
151 ret = 1;
153 for (i = k; i < align; ++i)
154 if (p[i] != o)
156 error (0, 0, "Iteration %zd - garbage before %s (%zd, %d, %zd)",
157 n, impl->name, align, c, len);
158 ret = 1;
159 break;
161 for (; i < align + len; ++i)
162 if (p[i] != c)
164 error (0, 0, "Iteration %zd - not cleared correctly %s (%zd, %d, %zd)",
165 n, impl->name, align, c, len);
166 ret = 1;
167 break;
169 for (; i < j; ++i)
170 if (p[i] != o)
172 error (0, 0, "Iteration %zd - garbage after %s (%zd, %d, %zd)",
173 n, impl->name, align, c, len);
174 ret = 1;
175 break;
182 test_main (void)
184 size_t i;
185 int c;
187 test_init ();
189 printf ("%24s", "");
190 FOR_EACH_IMPL (impl, 0)
191 printf ("\t%s", impl->name);
192 putchar ('\n');
194 for (c = 0; c <= 65; c += 65)
196 for (i = 0; i < 18; ++i)
197 do_test (0, c, 1 << i);
198 for (i = 1; i < 32; ++i)
200 do_test (i, c, i);
201 if (i & (i - 1))
202 do_test (0, c, i);
204 do_test (1, c, 14);
205 do_test (3, c, 1024);
206 do_test (4, c, 64);
207 do_test (2, c, 25);
210 do_random_tests ();
211 return ret;
214 #include "../test-skeleton.c"