linux/aarch64: sync sys/ptrace.h with Linux 4.15 [BZ #22433]
[glibc.git] / string / test-memmove.c
blobedc7a4c3bf65d9628cb5f0a0d7aac6a05c5039c0
1 /* Test and measure memmove 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/>. */
20 #define TEST_MAIN
21 #ifdef TEST_BCOPY
22 # define TEST_NAME "bcopy"
23 #else
24 # define TEST_NAME "memmove"
25 #endif
26 #include "test-string.h"
28 char *simple_memmove (char *, const char *, size_t);
30 #ifdef TEST_BCOPY
31 typedef void (*proto_t) (const char *, char *, size_t);
32 void simple_bcopy (const char *, char *, size_t);
34 IMPL (simple_bcopy, 0)
35 IMPL (bcopy, 1)
37 void
38 simple_bcopy (const char *src, char *dst, size_t n)
40 simple_memmove (dst, src, n);
42 #else
43 typedef char *(*proto_t) (char *, const char *, size_t);
45 IMPL (simple_memmove, 0)
46 IMPL (memmove, 1)
47 #endif
49 char *
50 inhibit_loop_to_libcall
51 simple_memmove (char *dst, const char *src, size_t n)
53 char *ret = dst;
54 if (src < dst)
56 dst += n;
57 src += n;
58 while (n--)
59 *--dst = *--src;
61 else
62 while (n--)
63 *dst++ = *src++;
64 return ret;
67 static void
68 do_one_test (impl_t *impl, char *dst, char *src, const char *orig_src,
69 size_t len)
71 /* This also clears the destination buffer set by the previous run. */
72 memcpy (src, orig_src, len);
73 #ifdef TEST_BCOPY
74 CALL (impl, src, dst, len);
75 #else
76 char *res;
78 res = CALL (impl, dst, src, len);
79 if (res != dst)
81 error (0, 0, "Wrong result in function %s %p %p", impl->name,
82 res, dst);
83 ret = 1;
84 return;
86 #endif
88 if (memcmp (dst, orig_src, len) != 0)
90 error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
91 impl->name, dst, src);
92 ret = 1;
93 return;
97 static void
98 do_test (size_t align1, size_t align2, size_t len)
100 size_t i, j;
101 char *s1, *s2;
103 align1 &= 63;
104 if (align1 + len >= page_size)
105 return;
107 align2 &= 63;
108 if (align2 + len >= page_size)
109 return;
111 s1 = (char *) (buf1 + align1);
112 s2 = (char *) (buf2 + align2);
114 for (i = 0, j = 1; i < len; i++, j += 23)
115 s1[i] = j;
117 FOR_EACH_IMPL (impl, 0)
118 do_one_test (impl, s2, (char *) (buf2 + align1), s1, len);
121 static void
122 do_random_tests (void)
124 size_t i, n, align1, align2, len, size;
125 size_t srcstart, srcend, dststart, dstend;
126 int c;
127 unsigned char *p1, *p2;
128 #ifndef TEST_BCOPY
129 unsigned char *res;
130 #endif
132 for (n = 0; n < ITERATIONS; n++)
134 if ((random () & 255) == 0)
135 size = 65536;
136 else
137 size = 512;
138 if (size > page_size)
139 size = page_size;
140 if ((random () & 3) == 0)
142 len = random () & (size - 1);
143 align1 = size - len - (random () & 31);
144 align2 = size - len - (random () & 31);
145 if (align1 > size)
146 align1 = 0;
147 if (align2 > size)
148 align2 = 0;
150 else
152 align1 = random () & (size / 2 - 1);
153 align2 = random () & (size / 2 - 1);
154 len = random () & (size - 1);
155 if (align1 + len > size)
156 align1 = size - len;
157 if (align2 + len > size)
158 align2 = size - len;
161 p1 = buf1 + page_size - size;
162 p2 = buf2 + page_size - size;
163 c = random () & 255;
164 srcend = align1 + len + 256;
165 if (srcend > size)
166 srcend = size;
167 if (align1 > 256)
168 srcstart = align1 - 256;
169 else
170 srcstart = 0;
171 for (i = srcstart; i < srcend; ++i)
172 p1[i] = random () & 255;
173 dstend = align2 + len + 256;
174 if (dstend > size)
175 dstend = size;
176 if (align2 > 256)
177 dststart = align2 - 256;
178 else
179 dststart = 0;
181 FOR_EACH_IMPL (impl, 1)
183 memset (p2 + dststart, c, dstend - dststart);
184 memcpy (p2 + srcstart, p1 + srcstart, srcend - srcstart);
185 #ifdef TEST_BCOPY
186 CALL (impl, (char *) (p2 + align1), (char *) (p2 + align2), len);
187 #else
188 res = (unsigned char *) CALL (impl,
189 (char *) (p2 + align2),
190 (char *) (p2 + align1), len);
191 if (res != p2 + align2)
193 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
194 n, impl->name, align1, align2, len, res, p2 + align2);
195 ret = 1;
197 #endif
198 if (memcmp (p1 + align1, p2 + align2, len))
200 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
201 n, impl->name, align1, align2, len);
202 ret = 1;
204 for (i = dststart; i < dstend; ++i)
206 if (i >= align2 && i < align2 + len)
208 i = align2 + len - 1;
209 continue;
211 if (i >= srcstart && i < srcend)
213 i = srcend - 1;
214 continue;
216 if (p2[i] != c)
218 error (0, 0, "Iteration %zd - garbage in memset area, %s (%zd, %zd, %zd)",
219 n, impl->name, align1, align2, len);
220 ret = 1;
221 break;
225 if (srcstart < align2
226 && memcmp (p2 + srcstart, p1 + srcstart,
227 (srcend > align2 ? align2 : srcend) - srcstart))
229 error (0, 0, "Iteration %zd - garbage before dst, %s (%zd, %zd, %zd)",
230 n, impl->name, align1, align2, len);
231 ret = 1;
232 break;
235 i = srcstart > align2 + len ? srcstart : align2 + len;
236 if (srcend > align2 + len
237 && memcmp (p2 + i, p1 + i, srcend - i))
239 error (0, 0, "Iteration %zd - garbage after dst, %s (%zd, %zd, %zd)",
240 n, impl->name, align1, align2, len);
241 ret = 1;
242 break;
249 test_main (void)
251 size_t i;
253 test_init ();
255 printf ("%23s", "");
256 FOR_EACH_IMPL (impl, 0)
257 printf ("\t%s", impl->name);
258 putchar ('\n');
260 for (i = 0; i < 14; ++i)
262 do_test (0, 32, 1 << i);
263 do_test (32, 0, 1 << i);
264 do_test (0, i, 1 << i);
265 do_test (i, 0, 1 << i);
268 for (i = 0; i < 32; ++i)
270 do_test (0, 32, i);
271 do_test (32, 0, i);
272 do_test (0, i, i);
273 do_test (i, 0, i);
276 for (i = 3; i < 32; ++i)
278 if ((i & (i - 1)) == 0)
279 continue;
280 do_test (0, 32, 16 * i);
281 do_test (32, 0, 16 * i);
282 do_test (0, i, 16 * i);
283 do_test (i, 0, 16 * i);
286 do_random_tests ();
287 return ret;
290 #include <support/test-driver.c>