Fix tst-rfc3484* build failures from USE_NSCD move to config.h.
[glibc.git] / string / test-memccpy.c
blob8fa96bc9b71a3ba9f6b8506dfa722ab7db927354
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, see
18 <http://www.gnu.org/licenses/>. */
20 #define TEST_MAIN
21 #include "test-string.h"
23 void *simple_memccpy (void *, const void *, int, size_t);
24 void *stupid_memccpy (void *, const void *, int, size_t);
26 IMPL (stupid_memccpy, 0)
27 IMPL (simple_memccpy, 0)
28 IMPL (memccpy, 1)
30 void *
31 simple_memccpy (void *dst, const void *src, int c, size_t n)
33 const char *s = src;
34 char *d = dst;
36 while (n-- > 0)
37 if ((*d++ = *s++) == (char) c)
38 return d;
40 return NULL;
43 void *
44 stupid_memccpy (void *dst, const void *src, int c, size_t n)
46 void *p = memchr (src, c, n);
48 if (p != NULL)
49 return mempcpy (dst, src, p - src + 1);
51 memcpy (dst, src, n);
52 return NULL;
55 typedef void *(*proto_t) (void *, const void *, int c, size_t);
57 static void
58 do_one_test (impl_t *impl, void *dst, const void *src, int c, size_t len,
59 size_t n)
61 void *expect = len > n ? NULL : (char *) dst + len;
62 if (CALL (impl, dst, src, c, n) != expect)
64 error (0, 0, "Wrong result in function %s %p %p", impl->name,
65 CALL (impl, dst, src, c, n), expect);
66 ret = 1;
67 return;
70 if (memcmp (dst, src, len > n ? n : len) != 0)
72 error (0, 0, "Wrong result in function %s", impl->name);
73 ret = 1;
74 return;
77 if (HP_TIMING_AVAIL)
79 hp_timing_t start __attribute__ ((unused));
80 hp_timing_t stop __attribute__ ((unused));
81 hp_timing_t best_time = ~ (hp_timing_t) 0;
82 size_t i;
84 for (i = 0; i < 32; ++i)
86 HP_TIMING_NOW (start);
87 CALL (impl, dst, src, c, n);
88 HP_TIMING_NOW (stop);
89 HP_TIMING_BEST (best_time, start, stop);
92 printf ("\t%zd", (size_t) best_time);
96 static void
97 do_test (size_t align1, size_t align2, int c, size_t len, size_t n,
98 int max_char)
100 size_t i;
101 char *s1, *s2;
103 align1 &= 7;
104 if (align1 + len >= page_size)
105 return;
107 align2 &= 7;
108 if (align2 + len >= page_size)
109 return;
111 s1 = (char *) (buf1 + align1);
112 s2 = (char *) (buf2 + align2);
114 for (i = 0; i < len - 1; ++i)
116 s1[i] = 32 + 23 * i % (max_char - 32);
117 if (s1[i] == (char) c)
118 --s1[i];
120 s1[len - 1] = c;
121 for (i = len; i + align1 < page_size && i < len + 64; ++i)
122 s1[i] = 32 + 32 * i % (max_char - 32);
124 if (HP_TIMING_AVAIL)
125 printf ("Length %4zd, n %4zd, char %d, alignment %2zd/%2zd:", len, n, c, align1, align2);
127 FOR_EACH_IMPL (impl, 0)
128 do_one_test (impl, s2, s1, c, len, n);
130 if (HP_TIMING_AVAIL)
131 putchar ('\n');
134 static void
135 do_random_tests (void)
137 size_t i, j, n, align1, align2, len, size, mode;
138 unsigned char *p1 = buf1 + page_size - 512;
139 unsigned char *p2 = buf2 + page_size - 512;
140 unsigned char *res, c;
142 for (n = 0; n < ITERATIONS; n++)
144 mode = random ();
145 c = random ();
146 if (mode & 1)
148 size = random () & 255;
149 align1 = 512 - size - (random () & 15);
150 if (mode & 2)
151 align2 = align1 - (random () & 24);
152 else
153 align2 = align1 - (random () & 31);
154 if (mode & 4)
156 j = align1;
157 align1 = align2;
158 align2 = j;
160 if (mode & 8)
161 len = size - (random () & 31);
162 else
163 len = 512;
164 if (len >= 512)
165 len = random () & 511;
167 else
169 align1 = random () & 31;
170 if (mode & 2)
171 align2 = random () & 31;
172 else
173 align2 = align1 + (random () & 24);
174 len = random () & 511;
175 j = align1;
176 if (align2 > j)
177 j = align2;
178 if (mode & 4)
180 size = random () & 511;
181 if (size + j > 512)
182 size = 512 - j - (random() & 31);
184 else
185 size = 512 - j;
186 if ((mode & 8) && len + j >= 512)
187 len = 512 - j - (random () & 7);
189 j = len + align1 + 64;
190 if (j > 512)
191 j = 512;
192 for (i = 0; i < j; i++)
194 if (i == len + align1)
195 p1[i] = c;
196 else
198 p1[i] = random () & 255;
199 if (i >= align1 && i < len + align1 && p1[i] == c)
200 p1[i] = (random () & 127) + 3 + c;
204 FOR_EACH_IMPL (impl, 1)
206 unsigned char *expect;
207 memset (p2 - 64, '\1', 512 + 64);
208 res = CALL (impl, p2 + align2, p1 + align1, (char) c, size);
209 if (len >= size)
210 expect = NULL;
211 else
212 expect = p2 + align2 + len + 1;
214 if (res != expect)
216 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %d) %p != %p",
217 n, impl->name, align1, align2, len, size, c, res, expect);
218 ret = 1;
220 for (j = 0; j < align2 + 64; ++j)
222 if (p2[j - 64] != '\1')
224 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
225 n, impl->name, align1, align2, len);
226 ret = 1;
227 break;
230 j = align2 + len + 1;
231 if (size + align2 < j)
232 j = size + align2;
233 for (; j < 512; ++j)
235 if (p2[j] != '\1')
237 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
238 n, impl->name, align1, align2, len);
239 ret = 1;
240 break;
243 j = len + 1;
244 if (size < j)
245 j = size;
246 if (memcmp (p1 + align1, p2 + align2, j))
248 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
249 n, impl->name, align1, align2, len);
250 ret = 1;
257 test_main (void)
259 size_t i;
261 test_init ();
263 printf ("%28s", "");
264 FOR_EACH_IMPL (impl, 0)
265 printf ("\t%s", impl->name);
266 putchar ('\n');
268 for (i = 1; i < 8; ++i)
270 do_test (i, i, 12, 16, 16, 127);
271 do_test (i, i, 23, 16, 16, 255);
272 do_test (i, 2 * i, 28, 16, 16, 127);
273 do_test (2 * i, i, 31, 16, 16, 255);
274 do_test (8 - i, 2 * i, 1, 1 << i, 2 << i, 127);
275 do_test (2 * i, 8 - i, 17, 2 << i, 1 << i, 127);
276 do_test (8 - i, 2 * i, 0, 1 << i, 2 << i, 255);
277 do_test (2 * i, 8 - i, i, 2 << i, 1 << i, 255);
280 for (i = 1; i < 8; ++i)
282 do_test (0, 0, i, 4 << i, 8 << i, 127);
283 do_test (0, 0, i, 16 << i, 8 << i, 127);
284 do_test (8 - i, 2 * i, i, 4 << i, 8 << i, 127);
285 do_test (8 - i, 2 * i, i, 16 << i, 8 << i, 127);
288 do_random_tests ();
289 return ret;
292 #include "../test-skeleton.c"