Replace FSF snail mail address with URLs.
[glibc.git] / string / test-memmove.c
blob7d107123946f5635c93e1fba5f28f40084b4a11d
1 /* Test and measure memmove 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, see
18 <http://www.gnu.org/licenses/>. */
20 #define TEST_MAIN
21 #include "test-string.h"
23 typedef char *(*proto_t) (char *, const char *, size_t);
24 char *simple_memmove (char *, const char *, size_t);
26 IMPL (simple_memmove, 0)
27 IMPL (memmove, 1)
29 char *
30 simple_memmove (char *dst, const char *src, size_t n)
32 char *ret = dst;
33 if (src < dst)
35 dst += n;
36 src += n;
37 while (n--)
38 *--dst = *--src;
40 else
41 while (n--)
42 *dst++ = *src++;
43 return ret;
46 static void
47 do_one_test (impl_t *impl, char *dst, char *src, const char *orig_src,
48 size_t len)
50 char *res;
52 memcpy (src, orig_src, len);
53 res = CALL (impl, dst, src, len);
54 if (res != dst)
56 error (0, 0, "Wrong result in function %s %p %p", impl->name,
57 res, dst);
58 ret = 1;
59 return;
62 if (memcmp (dst, orig_src, len) != 0)
64 error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
65 impl->name, dst, src);
66 ret = 1;
67 return;
70 if (HP_TIMING_AVAIL)
72 hp_timing_t start __attribute ((unused));
73 hp_timing_t stop __attribute ((unused));
74 hp_timing_t best_time = ~ (hp_timing_t) 0;
75 size_t i;
77 for (i = 0; i < 32; ++i)
79 HP_TIMING_NOW (start);
80 CALL (impl, dst, src, len);
81 HP_TIMING_NOW (stop);
82 HP_TIMING_BEST (best_time, start, stop);
85 printf ("\t%zd", (size_t) best_time);
89 static void
90 do_test (size_t align1, size_t align2, size_t len)
92 size_t i, j;
93 char *s1, *s2;
95 align1 &= 63;
96 if (align1 + len >= page_size)
97 return;
99 align2 &= 63;
100 if (align2 + len >= page_size)
101 return;
103 s1 = (char *) (buf1 + align1);
104 s2 = (char *) (buf2 + align2);
106 for (i = 0, j = 1; i < len; i++, j += 23)
107 s1[i] = j;
109 if (HP_TIMING_AVAIL)
110 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
112 FOR_EACH_IMPL (impl, 0)
113 do_one_test (impl, s2, (char *) (buf2 + align1), s1, len);
115 if (HP_TIMING_AVAIL)
116 putchar ('\n');
119 static void
120 do_random_tests (void)
122 size_t i, n, align1, align2, len, size;
123 size_t srcstart, srcend, dststart, dstend;
124 int c;
125 unsigned char *p1, *p2;
126 unsigned char *res;
128 for (n = 0; n < ITERATIONS; n++)
130 if ((random () & 255) == 0)
131 size = 65536;
132 else
133 size = 512;
134 if (size > page_size)
135 size = page_size;
136 if ((random () & 3) == 0)
138 len = random () & (size - 1);
139 align1 = size - len - (random () & 31);
140 align2 = size - len - (random () & 31);
141 if (align1 > size)
142 align1 = 0;
143 if (align2 > size)
144 align2 = 0;
146 else
148 align1 = random () & (size / 2 - 1);
149 align2 = random () & (size / 2 - 1);
150 len = random () & (size - 1);
151 if (align1 + len > size)
152 align1 = size - len;
153 if (align2 + len > size)
154 align2 = size - len;
157 p1 = buf1 + page_size - size;
158 p2 = buf2 + page_size - size;
159 c = random () & 255;
160 srcend = align1 + len + 256;
161 if (srcend > size)
162 srcend = size;
163 if (align1 > 256)
164 srcstart = align1 - 256;
165 else
166 srcstart = 0;
167 for (i = srcstart; i < srcend; ++i)
168 p1[i] = random () & 255;
169 dstend = align2 + len + 256;
170 if (dstend > size)
171 dstend = size;
172 if (align2 > 256)
173 dststart = align2 - 256;
174 else
175 dststart = 0;
177 FOR_EACH_IMPL (impl, 1)
179 memset (p2 + dststart, c, dstend - dststart);
180 memcpy (p2 + srcstart, p1 + srcstart, srcend - srcstart);
181 res = (unsigned char *) CALL (impl,
182 (char *) (p2 + align2),
183 (char *) (p2 + align1), len);
184 if (res != p2 + align2)
186 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
187 n, impl->name, align1, align2, len, res, p2 + align2);
188 ret = 1;
190 if (memcmp (p1 + align1, p2 + align2, len))
192 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
193 n, impl->name, align1, align2, len);
194 ret = 1;
196 for (i = dststart; i < dstend; ++i)
198 if (i >= align2 && i < align2 + len)
200 i = align2 + len - 1;
201 continue;
203 if (i >= srcstart && i < srcend)
205 i = srcend - 1;
206 continue;
208 if (p2[i] != c)
210 error (0, 0, "Iteration %zd - garbage in memset area, %s (%zd, %zd, %zd)",
211 n, impl->name, align1, align2, len);
212 ret = 1;
213 break;
217 if (srcstart < align2
218 && memcmp (p2 + srcstart, p1 + srcstart,
219 (srcend > align2 ? align2 : srcend) - srcstart))
221 error (0, 0, "Iteration %zd - garbage before dst, %s (%zd, %zd, %zd)",
222 n, impl->name, align1, align2, len);
223 ret = 1;
224 break;
227 i = srcstart > align2 + len ? srcstart : align2 + len;
228 if (srcend > align2 + len
229 && memcmp (p2 + i, p1 + i, srcend - i))
231 error (0, 0, "Iteration %zd - garbage after dst, %s (%zd, %zd, %zd)",
232 n, impl->name, align1, align2, len);
233 ret = 1;
234 break;
241 test_main (void)
243 size_t i;
245 test_init ();
247 printf ("%23s", "");
248 FOR_EACH_IMPL (impl, 0)
249 printf ("\t%s", impl->name);
250 putchar ('\n');
252 for (i = 0; i < 14; ++i)
254 do_test (0, 32, 1 << i);
255 do_test (32, 0, 1 << i);
256 do_test (0, i, 1 << i);
257 do_test (i, 0, 1 << i);
260 for (i = 0; i < 32; ++i)
262 do_test (0, 32, i);
263 do_test (32, 0, i);
264 do_test (0, i, i);
265 do_test (i, 0, i);
268 for (i = 3; i < 32; ++i)
270 if ((i & (i - 1)) == 0)
271 continue;
272 do_test (0, 32, 16 * i);
273 do_test (32, 0, 16 * i);
274 do_test (0, i, 16 * i);
275 do_test (i, 0, 16 * i);
278 do_random_tests ();
279 return ret;
282 #include "../test-skeleton.c"