1 /* Test and measure memmove 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/>. */
22 # define TEST_NAME "bcopy"
24 # define TEST_NAME "memmove"
26 #include "test-string.h"
28 char *simple_memmove (char *, const char *, size_t);
31 typedef void (*proto_t
) (const char *, char *, size_t);
32 void simple_bcopy (const char *, char *, size_t);
34 IMPL (simple_bcopy
, 0)
38 simple_bcopy (const char *src
, char *dst
, size_t n
)
40 simple_memmove (dst
, src
, n
);
43 typedef char *(*proto_t
) (char *, const char *, size_t);
45 IMPL (simple_memmove
, 0)
50 simple_memmove (char *dst
, const char *src
, size_t n
)
67 do_one_test (impl_t
*impl
, char *dst
, char *src
, const char *orig_src
,
70 memcpy (src
, orig_src
, len
);
72 CALL (impl
, src
, dst
, len
);
76 res
= CALL (impl
, dst
, src
, len
);
79 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
86 if (memcmp (dst
, orig_src
, len
) != 0)
88 error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
89 impl
->name
, dst
, src
);
96 hp_timing_t start
__attribute ((unused
));
97 hp_timing_t stop
__attribute ((unused
));
98 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
101 for (i
= 0; i
< 32; ++i
)
103 HP_TIMING_NOW (start
);
105 CALL (impl
, src
, dst
, len
);
107 CALL (impl
, dst
, src
, len
);
109 HP_TIMING_NOW (stop
);
110 HP_TIMING_BEST (best_time
, start
, stop
);
113 printf ("\t%zd", (size_t) best_time
);
118 do_test (size_t align1
, size_t align2
, size_t len
)
124 if (align1
+ len
>= page_size
)
128 if (align2
+ len
>= page_size
)
131 s1
= (char *) (buf1
+ align1
);
132 s2
= (char *) (buf2
+ align2
);
134 for (i
= 0, j
= 1; i
< len
; i
++, j
+= 23)
138 printf ("Length %4zd, alignment %2zd/%2zd:", len
, align1
, align2
);
140 FOR_EACH_IMPL (impl
, 0)
141 do_one_test (impl
, s2
, (char *) (buf2
+ align1
), s1
, len
);
148 do_random_tests (void)
150 size_t i
, n
, align1
, align2
, len
, size
;
151 size_t srcstart
, srcend
, dststart
, dstend
;
153 unsigned char *p1
, *p2
;
158 for (n
= 0; n
< ITERATIONS
; n
++)
160 if ((random () & 255) == 0)
164 if (size
> page_size
)
166 if ((random () & 3) == 0)
168 len
= random () & (size
- 1);
169 align1
= size
- len
- (random () & 31);
170 align2
= size
- len
- (random () & 31);
178 align1
= random () & (size
/ 2 - 1);
179 align2
= random () & (size
/ 2 - 1);
180 len
= random () & (size
- 1);
181 if (align1
+ len
> size
)
183 if (align2
+ len
> size
)
187 p1
= buf1
+ page_size
- size
;
188 p2
= buf2
+ page_size
- size
;
190 srcend
= align1
+ len
+ 256;
194 srcstart
= align1
- 256;
197 for (i
= srcstart
; i
< srcend
; ++i
)
198 p1
[i
] = random () & 255;
199 dstend
= align2
+ len
+ 256;
203 dststart
= align2
- 256;
207 FOR_EACH_IMPL (impl
, 1)
209 memset (p2
+ dststart
, c
, dstend
- dststart
);
210 memcpy (p2
+ srcstart
, p1
+ srcstart
, srcend
- srcstart
);
212 CALL (impl
, (char *) (p2
+ align1
), (char *) (p2
+ align2
), len
);
214 res
= (unsigned char *) CALL (impl
,
215 (char *) (p2
+ align2
),
216 (char *) (p2
+ align1
), len
);
217 if (res
!= p2
+ align2
)
219 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
220 n
, impl
->name
, align1
, align2
, len
, res
, p2
+ align2
);
224 if (memcmp (p1
+ align1
, p2
+ align2
, len
))
226 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
227 n
, impl
->name
, align1
, align2
, len
);
230 for (i
= dststart
; i
< dstend
; ++i
)
232 if (i
>= align2
&& i
< align2
+ len
)
234 i
= align2
+ len
- 1;
237 if (i
>= srcstart
&& i
< srcend
)
244 error (0, 0, "Iteration %zd - garbage in memset area, %s (%zd, %zd, %zd)",
245 n
, impl
->name
, align1
, align2
, len
);
251 if (srcstart
< align2
252 && memcmp (p2
+ srcstart
, p1
+ srcstart
,
253 (srcend
> align2
? align2
: srcend
) - srcstart
))
255 error (0, 0, "Iteration %zd - garbage before dst, %s (%zd, %zd, %zd)",
256 n
, impl
->name
, align1
, align2
, len
);
261 i
= srcstart
> align2
+ len
? srcstart
: align2
+ len
;
262 if (srcend
> align2
+ len
263 && memcmp (p2
+ i
, p1
+ i
, srcend
- i
))
265 error (0, 0, "Iteration %zd - garbage after dst, %s (%zd, %zd, %zd)",
266 n
, impl
->name
, align1
, align2
, len
);
282 FOR_EACH_IMPL (impl
, 0)
283 printf ("\t%s", impl
->name
);
286 for (i
= 0; i
< 14; ++i
)
288 do_test (0, 32, 1 << i
);
289 do_test (32, 0, 1 << i
);
290 do_test (0, i
, 1 << i
);
291 do_test (i
, 0, 1 << i
);
294 for (i
= 0; i
< 32; ++i
)
302 for (i
= 3; i
< 32; ++i
)
304 if ((i
& (i
- 1)) == 0)
306 do_test (0, 32, 16 * i
);
307 do_test (32, 0, 16 * i
);
308 do_test (0, i
, 16 * i
);
309 do_test (i
, 0, 16 * i
);
316 #include "../test-skeleton.c"