[BZ #9895]
[glibc.git] / string / test-memccpy.c
blob26532633df576b4cd7c54e9cb1465cbb14e8af8a
1 /* Test and measure memccpy functions.
2 Copyright (C) 1999, 2002, 2003, 2005 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 #include "test-string.h"
24 void *simple_memccpy (void *, const void *, int, size_t);
25 void *stupid_memccpy (void *, const void *, int, size_t);
27 IMPL (stupid_memccpy, 0)
28 IMPL (simple_memccpy, 0)
29 IMPL (memccpy, 1)
31 void *
32 simple_memccpy (void *dst, const void *src, int c, size_t n)
34 const char *s = src;
35 char *d = dst;
37 while (n-- > 0)
38 if ((*d++ = *s++) == (char) c)
39 return d;
41 return NULL;
44 void *
45 stupid_memccpy (void *dst, const void *src, int c, size_t n)
47 void *p = memchr (src, c, n);
49 if (p != NULL)
50 return mempcpy (dst, src, p - src + 1);
52 memcpy (dst, src, n);
53 return NULL;
56 typedef void *(*proto_t) (void *, const void *, int c, size_t);
58 static void
59 do_one_test (impl_t *impl, void *dst, const void *src, int c, size_t len,
60 size_t n)
62 void *expect = len > n ? NULL : (char *) dst + len;
63 if (CALL (impl, dst, src, c, n) != expect)
65 error (0, 0, "Wrong result in function %s %p %p", impl->name,
66 CALL (impl, dst, src, c, n), expect);
67 ret = 1;
68 return;
71 if (memcmp (dst, src, len > n ? n : len) != 0)
73 error (0, 0, "Wrong result in function %s", impl->name);
74 ret = 1;
75 return;
78 if (HP_TIMING_AVAIL)
80 hp_timing_t start __attribute__ ((unused));
81 hp_timing_t stop __attribute__ ((unused));
82 hp_timing_t best_time = ~ (hp_timing_t) 0;
83 size_t i;
85 for (i = 0; i < 32; ++i)
87 HP_TIMING_NOW (start);
88 CALL (impl, dst, src, c, n);
89 HP_TIMING_NOW (stop);
90 HP_TIMING_BEST (best_time, start, stop);
93 printf ("\t%zd", (size_t) best_time);
97 static void
98 do_test (size_t align1, size_t align2, int c, size_t len, size_t n,
99 int max_char)
101 size_t i;
102 char *s1, *s2;
104 align1 &= 7;
105 if (align1 + len >= page_size)
106 return;
108 align2 &= 7;
109 if (align2 + len >= page_size)
110 return;
112 s1 = (char *) (buf1 + align1);
113 s2 = (char *) (buf2 + align2);
115 for (i = 0; i < len - 1; ++i)
117 s1[i] = 32 + 23 * i % (max_char - 32);
118 if (s1[i] == (char) c)
119 --s1[i];
121 s1[len - 1] = c;
122 for (i = len; i + align1 < page_size && i < len + 64; ++i)
123 s1[i] = 32 + 32 * i % (max_char - 32);
125 if (HP_TIMING_AVAIL)
126 printf ("Length %4zd, n %4zd, char %d, alignment %2zd/%2zd:", len, n, c, align1, align2);
128 FOR_EACH_IMPL (impl, 0)
129 do_one_test (impl, s2, s1, c, len, n);
131 if (HP_TIMING_AVAIL)
132 putchar ('\n');
135 static void
136 do_random_tests (void)
138 size_t i, j, n, align1, align2, len, size, mode;
139 unsigned char *p1 = buf1 + page_size - 512;
140 unsigned char *p2 = buf2 + page_size - 512;
141 unsigned char *res, c;
143 for (n = 0; n < ITERATIONS; n++)
145 mode = random ();
146 c = random ();
147 if (mode & 1)
149 size = random () & 255;
150 align1 = 512 - size - (random () & 15);
151 if (mode & 2)
152 align2 = align1 - (random () & 24);
153 else
154 align2 = align1 - (random () & 31);
155 if (mode & 4)
157 j = align1;
158 align1 = align2;
159 align2 = j;
161 if (mode & 8)
162 len = size - (random () & 31);
163 else
164 len = 512;
165 if (len >= 512)
166 len = random () & 511;
168 else
170 align1 = random () & 31;
171 if (mode & 2)
172 align2 = random () & 31;
173 else
174 align2 = align1 + (random () & 24);
175 len = random () & 511;
176 j = align1;
177 if (align2 > j)
178 j = align2;
179 if (mode & 4)
181 size = random () & 511;
182 if (size + j > 512)
183 size = 512 - j - (random() & 31);
185 else
186 size = 512 - j;
187 if ((mode & 8) && len + j >= 512)
188 len = 512 - j - (random () & 7);
190 j = len + align1 + 64;
191 if (j > 512)
192 j = 512;
193 for (i = 0; i < j; i++)
195 if (i == len + align1)
196 p1[i] = c;
197 else
199 p1[i] = random () & 255;
200 if (i >= align1 && i < len + align1 && p1[i] == c)
201 p1[i] = (random () & 127) + 3 + c;
205 FOR_EACH_IMPL (impl, 1)
207 unsigned char *expect;
208 memset (p2 - 64, '\1', 512 + 64);
209 res = CALL (impl, p2 + align2, p1 + align1, (char) c, size);
210 if (len >= size)
211 expect = NULL;
212 else
213 expect = p2 + align2 + len + 1;
215 if (res != expect)
217 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %d) %p != %p",
218 n, impl->name, align1, align2, len, size, c, res, expect);
219 ret = 1;
221 for (j = 0; j < align2 + 64; ++j)
223 if (p2[j - 64] != '\1')
225 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
226 n, impl->name, align1, align2, len);
227 ret = 1;
228 break;
231 j = align2 + len + 1;
232 if (size + align2 < j)
233 j = size + align2;
234 for (; j < 512; ++j)
236 if (p2[j] != '\1')
238 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
239 n, impl->name, align1, align2, len);
240 ret = 1;
241 break;
244 j = len + 1;
245 if (size < j)
246 j = size;
247 if (memcmp (p1 + align1, p2 + align2, j))
249 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
250 n, impl->name, align1, align2, len);
251 ret = 1;
258 test_main (void)
260 size_t i;
262 test_init ();
264 printf ("%28s", "");
265 FOR_EACH_IMPL (impl, 0)
266 printf ("\t%s", impl->name);
267 putchar ('\n');
269 for (i = 1; i < 8; ++i)
271 do_test (i, i, 12, 16, 16, 127);
272 do_test (i, i, 23, 16, 16, 255);
273 do_test (i, 2 * i, 28, 16, 16, 127);
274 do_test (2 * i, i, 31, 16, 16, 255);
275 do_test (8 - i, 2 * i, 1, 1 << i, 2 << i, 127);
276 do_test (2 * i, 8 - i, 17, 2 << i, 1 << i, 127);
277 do_test (8 - i, 2 * i, 0, 1 << i, 2 << i, 255);
278 do_test (2 * i, 8 - i, i, 2 << i, 1 << i, 255);
281 for (i = 1; i < 8; ++i)
283 do_test (0, 0, i, 4 << i, 8 << i, 127);
284 do_test (0, 0, i, 16 << i, 8 << i, 127);
285 do_test (8 - i, 2 * i, i, 4 << i, 8 << i, 127);
286 do_test (8 - i, 2 * i, i, 16 << i, 8 << i, 127);
289 do_random_tests ();
290 return ret;
293 #include "../test-skeleton.c"