Optimize for kernels which are known to have the vfork syscall.
[glibc/pb-stable.git] / string / test-strncpy.c
blobc30c9b040baada18fd572087037f8f04679b2d9e
1 /* Test and measure strncpy functions.
2 Copyright (C) 1999, 2002 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 #ifndef STRNCPY_RESULT
22 # define STRNCPY_RESULT(dst, len, n) dst
23 # define TEST_MAIN
24 # include "test-string.h"
26 char *simple_strncpy (char *, const char *, size_t);
27 char *stupid_strncpy (char *, const char *, size_t);
29 IMPL (stupid_strncpy, 0)
30 IMPL (simple_strncpy, 0)
31 IMPL (strncpy, 1)
33 char *
34 simple_strncpy (char *dst, const char *src, size_t n)
36 char *ret = dst;
37 while (n--)
38 if ((*dst++ = *src++) == '\0')
40 while (n--)
41 *dst++ = '\0';
42 return ret;
44 return ret;
47 char *
48 stupid_strncpy (char *dst, const char *src, size_t n)
50 size_t nc = strnlen (src, n);
51 size_t i;
53 for (i = 0; i < nc; ++i)
54 dst[i] = src[i];
55 for (; i < n; ++i)
56 dst[i] = '\0';
57 return dst;
59 #endif
61 typedef char *(*proto_t) (char *, const char *, size_t);
63 static void
64 do_one_test (impl_t *impl, char *dst, const char *src, size_t len, size_t n)
66 if (CALL (impl, dst, src, n) != STRNCPY_RESULT (dst, len, n))
68 error (0, 0, "Wrong result in function %s %p %p", impl->name,
69 CALL (impl, dst, src, n), dst);
70 ret = 1;
71 return;
74 if (memcmp (dst, src, len > n ? n : len) != 0)
76 error (0, 0, "Wrong result in function %s", impl->name);
77 ret = 1;
78 return;
81 if (n > len)
83 size_t i;
85 for (i = len; i < n; ++i)
86 if (dst [i] != '\0')
88 error (0, 0, "Wrong result in function %s", impl->name);
89 ret = 1;
90 return;
94 if (HP_TIMING_AVAIL)
96 hp_timing_t start, stop, best_time = ~ (hp_timing_t) 0;
97 size_t i;
99 for (i = 0; i < 32; ++i)
101 HP_TIMING_NOW (start);
102 CALL (impl, dst, src, n);
103 HP_TIMING_NOW (stop);
104 HP_TIMING_BEST (best_time, start, stop);
107 printf ("\t%zd", (size_t) best_time);
111 static void
112 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
114 size_t i;
115 char *s1, *s2;
117 align1 &= 7;
118 if (align1 + len >= page_size)
119 return;
121 align2 &= 7;
122 if (align2 + len >= page_size)
123 return;
125 s1 = buf1 + align1;
126 s2 = buf2 + align2;
128 for (i = 0; i < len; ++i)
129 s1[i] = 32 + 23 * i % (max_char - 32);
130 s1[len] = 0;
131 for (i = len + 1; i + align1 < page_size && i < len + 64; ++i)
132 s1[i] = 32 + 32 * i % (max_char - 32);
134 if (HP_TIMING_AVAIL)
135 printf ("Length %4zd, n %4zd, alignment %2zd/%2zd:", len, n, align1, align2);
137 FOR_EACH_IMPL (impl, 0)
138 do_one_test (impl, s2, s1, len, n);
140 if (HP_TIMING_AVAIL)
141 putchar ('\n');
144 static void
145 do_random_tests (void)
147 size_t i, j, n, align1, align2, len, size, mode;
148 unsigned char *p1 = buf1 + page_size - 512;
149 unsigned char *p2 = buf2 + page_size - 512;
150 unsigned char *res;
152 for (n = 0; n < ITERATIONS; n++)
154 mode = random ();
155 if (mode & 1)
157 size = random () & 255;
158 align1 = 512 - size - (random () & 15);
159 if (mode & 2)
160 align2 = align1 - (random () & 24);
161 else
162 align2 = align1 - (random () & 31);
163 if (mode & 4)
165 j = align1;
166 align1 = align2;
167 align2 = j;
169 if (mode & 8)
170 len = size - (random () & 31);
171 else
172 len = 512;
173 if (len >= 512)
174 len = random () & 511;
176 else
178 align1 = random () & 31;
179 if (mode & 2)
180 align2 = random () & 31;
181 else
182 align2 = align1 + (random () & 24);
183 len = random () & 511;
184 j = align1;
185 if (align2 > j)
186 j = align2;
187 if (mode & 4)
189 size = random () & 511;
190 if (size + j > 512)
191 size = 512 - j - (random() & 31);
193 else
194 size = 512 - j;
195 if ((mode & 8) && len + j >= 512)
196 len = 512 - j - (random () & 7);
198 j = len + align1 + 64;
199 if (j > 512)
200 j = 512;
201 for (i = 0; i < j; i++)
203 if (i == len + align1)
204 p1[i] = 0;
205 else
207 p1[i] = random () & 255;
208 if (i >= align1 && i < len + align1 && !p1[i])
209 p1[i] = (random () & 127) + 3;
213 FOR_EACH_IMPL (impl, 1)
215 memset (p2 - 64, '\1', 512 + 64);
216 res = CALL (impl, p2 + align2, p1 + align1, size);
217 if (res != STRNCPY_RESULT (p2 + align2, len, size))
219 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
220 n, impl->name, align1, align2, len, res,
221 STRNCPY_RESULT (p2 + align2, len, size));
222 ret = 1;
224 for (j = 0; j < align2 + 64; ++j)
226 if (p2[j - 64] != '\1')
228 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
229 n, impl->name, align1, align2, len);
230 ret = 1;
231 break;
234 j = align2 + len + 1;
235 if (size + align2 > j)
236 j = size + align2;
237 for (; j < 512; ++j)
239 if (p2[j] != '\1')
241 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
242 n, impl->name, align1, align2, len);
243 ret = 1;
244 break;
247 for (j = align2 + len + 1; j < align2 + size; ++j)
248 if (p2[j])
250 error (0, 0, "Iteration %zd - garbage after size, %s (%zd, %zd, %zd)",
251 n, impl->name, align1, align2, len);
252 ret = 1;
253 break;
255 j = len + 1;
256 if (size < j)
257 j = size;
258 if (memcmp (p1 + align1, p2 + align2, j))
260 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
261 n, impl->name, align1, align2, len);
262 ret = 1;
269 test_main (void)
271 size_t i;
273 test_init ();
275 printf ("%28s", "");
276 FOR_EACH_IMPL (impl, 0)
277 printf ("\t%s", impl->name);
278 putchar ('\n');
280 for (i = 1; i < 8; ++i)
282 do_test (i, i, 16, 16, 127);
283 do_test (i, i, 16, 16, 255);
284 do_test (i, 2 * i, 16, 16, 127);
285 do_test (2 * i, i, 16, 16, 255);
286 do_test (8 - i, 2 * i, 1 << i, 2 << i, 127);
287 do_test (2 * i, 8 - i, 2 << i, 1 << i, 127);
288 do_test (8 - i, 2 * i, 1 << i, 2 << i, 255);
289 do_test (2 * i, 8 - i, 2 << i, 1 << i, 255);
292 for (i = 1; i < 8; ++i)
294 do_test (0, 0, 4 << i, 8 << i, 127);
295 do_test (0, 0, 16 << i, 8 << i, 127);
296 do_test (8 - i, 2 * i, 4 << i, 8 << i, 127);
297 do_test (8 - i, 2 * i, 16 << i, 8 << i, 127);
300 do_random_tests ();
301 return ret;
304 #include "../test-skeleton.c"