Hurd: empty file names fail with ENOENT
[glibc.git] / string / test-strncpy.c
blobf683f7d743c7c1dd18a2017fa2e2a321141347c6
1 /* Test and measure strncpy 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 #ifndef STRNCPY_RESULT
21 # define STRNCPY_RESULT(dst, len, n) dst
22 # define TEST_MAIN
23 # include "test-string.h"
25 char *simple_strncpy (char *, const char *, size_t);
26 char *stupid_strncpy (char *, const char *, size_t);
28 IMPL (stupid_strncpy, 0)
29 IMPL (simple_strncpy, 0)
30 IMPL (strncpy, 1)
32 char *
33 simple_strncpy (char *dst, const char *src, size_t n)
35 char *ret = dst;
36 while (n--)
37 if ((*dst++ = *src++) == '\0')
39 while (n--)
40 *dst++ = '\0';
41 return ret;
43 return ret;
46 char *
47 stupid_strncpy (char *dst, const char *src, size_t n)
49 size_t nc = strnlen (src, n);
50 size_t i;
52 for (i = 0; i < nc; ++i)
53 dst[i] = src[i];
54 for (; i < n; ++i)
55 dst[i] = '\0';
56 return dst;
58 #endif
60 typedef char *(*proto_t) (char *, const char *, size_t);
62 static void
63 do_one_test (impl_t *impl, char *dst, const char *src, size_t len, size_t n)
65 if (CALL (impl, dst, src, n) != STRNCPY_RESULT (dst, len, n))
67 error (0, 0, "Wrong result in function %s %p %p", impl->name,
68 CALL (impl, dst, src, n), dst);
69 ret = 1;
70 return;
73 if (memcmp (dst, src, len > n ? n : len) != 0)
75 error (0, 0, "Wrong result in function %s", impl->name);
76 ret = 1;
77 return;
80 if (n > len)
82 size_t i;
84 for (i = len; i < n; ++i)
85 if (dst [i] != '\0')
87 error (0, 0, "Wrong result in function %s", impl->name);
88 ret = 1;
89 return;
93 if (HP_TIMING_AVAIL)
95 hp_timing_t start __attribute__ ((unused));
96 hp_timing_t stop __attribute__ ((unused));
97 hp_timing_t best_time = ~ (hp_timing_t) 0;
98 size_t i;
100 for (i = 0; i < 32; ++i)
102 HP_TIMING_NOW (start);
103 CALL (impl, dst, src, n);
104 HP_TIMING_NOW (stop);
105 HP_TIMING_BEST (best_time, start, stop);
108 printf ("\t%zd", (size_t) best_time);
112 static void
113 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
115 size_t i;
116 char *s1, *s2;
118 align1 &= 7;
119 if (align1 + len >= page_size)
120 return;
122 align2 &= 7;
123 if (align2 + len >= page_size)
124 return;
126 s1 = (char *) (buf1 + align1);
127 s2 = (char *) (buf2 + align2);
129 for (i = 0; i < len; ++i)
130 s1[i] = 32 + 23 * i % (max_char - 32);
131 s1[len] = 0;
132 for (i = len + 1; i + align1 < page_size && i < len + 64; ++i)
133 s1[i] = 32 + 32 * i % (max_char - 32);
135 if (HP_TIMING_AVAIL)
136 printf ("Length %4zd, n %4zd, alignment %2zd/%2zd:", len, n, align1, align2);
138 FOR_EACH_IMPL (impl, 0)
139 do_one_test (impl, s2, s1, len, n);
141 if (HP_TIMING_AVAIL)
142 putchar ('\n');
145 static void
146 do_random_tests (void)
148 size_t i, j, n, align1, align2, len, size, mode;
149 unsigned char *p1 = buf1 + page_size - 512;
150 unsigned char *p2 = buf2 + page_size - 512;
151 unsigned char *res;
153 for (n = 0; n < ITERATIONS; n++)
155 mode = random ();
156 if (mode & 1)
158 size = random () & 255;
159 align1 = 512 - size - (random () & 15);
160 if (mode & 2)
161 align2 = align1 - (random () & 24);
162 else
163 align2 = align1 - (random () & 31);
164 if (mode & 4)
166 j = align1;
167 align1 = align2;
168 align2 = j;
170 if (mode & 8)
171 len = size - (random () & 31);
172 else
173 len = 512;
174 if (len >= 512)
175 len = random () & 511;
177 else
179 align1 = random () & 31;
180 if (mode & 2)
181 align2 = random () & 31;
182 else
183 align2 = align1 + (random () & 24);
184 len = random () & 511;
185 j = align1;
186 if (align2 > j)
187 j = align2;
188 if (mode & 4)
190 size = random () & 511;
191 if (size + j > 512)
192 size = 512 - j - (random() & 31);
194 else
195 size = 512 - j;
196 if ((mode & 8) && len + j >= 512)
197 len = 512 - j - (random () & 7);
199 j = len + align1 + 64;
200 if (j > 512)
201 j = 512;
202 for (i = 0; i < j; i++)
204 if (i == len + align1)
205 p1[i] = 0;
206 else
208 p1[i] = random () & 255;
209 if (i >= align1 && i < len + align1 && !p1[i])
210 p1[i] = (random () & 127) + 3;
214 FOR_EACH_IMPL (impl, 1)
216 memset (p2 - 64, '\1', 512 + 64);
217 res = (unsigned char *) CALL (impl,
218 (char *) (p2 + align2),
219 (char *) (p1 + align1), size);
220 if (res != STRNCPY_RESULT (p2 + align2, len, size))
222 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
223 n, impl->name, align1, align2, len, res,
224 STRNCPY_RESULT (p2 + align2, len, size));
225 ret = 1;
227 for (j = 0; j < align2 + 64; ++j)
229 if (p2[j - 64] != '\1')
231 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
232 n, impl->name, align1, align2, len);
233 ret = 1;
234 break;
237 j = align2 + len + 1;
238 if (size + align2 > j)
239 j = size + align2;
240 for (; j < 512; ++j)
242 if (p2[j] != '\1')
244 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
245 n, impl->name, align1, align2, len);
246 ret = 1;
247 break;
250 for (j = align2 + len + 1; j < align2 + size; ++j)
251 if (p2[j])
253 error (0, 0, "Iteration %zd - garbage after size, %s (%zd, %zd, %zd)",
254 n, impl->name, align1, align2, len);
255 ret = 1;
256 break;
258 j = len + 1;
259 if (size < j)
260 j = size;
261 if (memcmp (p1 + align1, p2 + align2, j))
263 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
264 n, impl->name, align1, align2, len);
265 ret = 1;
272 test_main (void)
274 size_t i;
276 test_init ();
278 printf ("%28s", "");
279 FOR_EACH_IMPL (impl, 0)
280 printf ("\t%s", impl->name);
281 putchar ('\n');
283 for (i = 1; i < 8; ++i)
285 do_test (i, i, 16, 16, 127);
286 do_test (i, i, 16, 16, 255);
287 do_test (i, 2 * i, 16, 16, 127);
288 do_test (2 * i, i, 16, 16, 255);
289 do_test (8 - i, 2 * i, 1 << i, 2 << i, 127);
290 do_test (2 * i, 8 - i, 2 << i, 1 << i, 127);
291 do_test (8 - i, 2 * i, 1 << i, 2 << i, 255);
292 do_test (2 * i, 8 - i, 2 << i, 1 << i, 255);
295 for (i = 1; i < 8; ++i)
297 do_test (0, 0, 4 << i, 8 << i, 127);
298 do_test (0, 0, 16 << i, 8 << i, 127);
299 do_test (8 - i, 2 * i, 4 << i, 8 << i, 127);
300 do_test (8 - i, 2 * i, 16 << i, 8 << i, 127);
303 do_random_tests ();
304 return ret;
307 #include "../test-skeleton.c"