Mention BZ #14229 in NEWS
[glibc.git] / string / test-strncat.c
blob69c8c0b21d48fe583b63276d838904d0c79ff3d0
1 /* Test and measure strncat functions.
2 Copyright (C) 2011 Free Software Foundation, Inc.
3 Contributed by Intel Corporation.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #define TEST_MAIN
20 #include "test-string.h"
22 typedef char *(*proto_t) (char *, const char *, size_t);
23 char *stupid_strncat (char *, const char *, size_t);
24 char *simple_strncat (char *, const char *, size_t);
26 IMPL (stupid_strncat, 0)
27 IMPL (strncat, 2)
29 char *
30 stupid_strncat (char *dst, const char *src, size_t n)
32 char *ret = dst;
33 while (*dst++ != '\0');
34 --dst;
35 while (n--)
36 if ( (*dst++ = *src++) == '\0')
37 return ret;
38 *dst = '\0';
39 return ret;
42 static void
43 do_one_test (impl_t *impl, char *dst, const char *src, size_t n)
45 size_t k = strlen (dst);
46 if (CALL (impl, dst, src, n) != dst)
48 error (0, 0, "Wrong result in function %s %p != %p", impl->name,
49 CALL (impl, dst, src, n), dst);
50 ret = 1;
51 return;
54 size_t len = strlen (src);
55 if (memcmp (dst + k, src, len + 1 > n ? n : len + 1) != 0)
57 error (0, 0, "Incorrect cancatination in function %s",
58 impl->name);
59 ret = 1;
60 return;
62 if (n < len && dst[k + n] != '\0')
64 error (0, 0, "There is no zero in the end of output string in %s",
65 impl->name);
66 ret = 1;
67 return;
69 if (HP_TIMING_AVAIL)
71 hp_timing_t start __attribute ((unused));
72 hp_timing_t stop __attribute ((unused));
73 hp_timing_t best_time = ~ (hp_timing_t) 0;
74 size_t i;
76 for (i = 0; i < 32; ++i)
78 dst[k] = '\0';
79 HP_TIMING_NOW (start);
80 CALL (impl, dst, src, n);
81 HP_TIMING_NOW (stop);
82 HP_TIMING_BEST (best_time, start, stop);
85 printf ("\t%zd", (size_t) best_time);
89 static void
90 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
91 size_t n, int max_char)
93 size_t i;
94 char *s1, *s2;
96 align1 &= 7;
97 if (align1 + len1 >= page_size)
98 return;
99 if (align1 + n > page_size)
100 return;
101 align2 &= 7;
102 if (align2 + len1 + len2 >= page_size)
103 return;
104 if (align2 + len1 + n > page_size)
105 return;
106 s1 = (char *) (buf1 + align1);
107 s2 = (char *) (buf2 + align2);
109 for (i = 0; i < len1; ++i)
110 s1[i] = 32 + 23 * i % (max_char - 32);
111 s1[len1] = '\0';
113 for (i = 0; i < len2; i++)
114 s2[i] = 32 + 23 * i % (max_char - 32);
116 if (HP_TIMING_AVAIL)
117 printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
118 len1, len2, align1, align2, n);
120 FOR_EACH_IMPL (impl, 0)
122 s2[len2] = '\0';
123 do_one_test (impl, s2, s1, n);
126 if (HP_TIMING_AVAIL)
127 putchar ('\n');
130 static void
131 do_random_tests (void)
133 size_t i, j, n, align1, align2, len1, len2, N;
134 unsigned char *p1 = buf1 + page_size - 512;
135 unsigned char *p2 = buf2 + page_size - 512;
136 unsigned char *res;
137 fprintf (stdout, "Number of iterations in random test = %zd\n",
138 ITERATIONS);
139 for (n = 0; n < ITERATIONS; n++)
141 N = random () & 255;
142 align1 = random () & 31;
143 if (random () & 1)
144 align2 = random () & 31;
145 else
146 align2 = align1 + (random () & 24);
147 len1 = random () & 511;
148 if (len1 + align2 > 512)
149 len2 = random () & 7;
150 else
151 len2 = (512 - len1 - align2) * (random () & (1024 * 1024 - 1))
152 / (1024 * 1024);
153 j = align1;
154 if (align2 + len2 > j)
155 j = align2 + len2;
156 if (len1 + j >= 511)
157 len1 = 510 - j - (random () & 7);
158 if (len1 >= 512)
159 len1 = 0;
160 if (align1 + len1 < 512 - 8)
162 j = 510 - align1 - len1 - (random () & 31);
163 if (j > 0 && j < 512)
164 align1 += j;
166 j = len1 + align1 + 64;
167 if (j > 512)
168 j = 512;
169 for (i = 0; i < j; i++)
171 if (i == len1 + align1)
172 p1[i] = 0;
173 else
175 p1[i] = random () & 255;
176 if (i >= align1 && i < len1 + align1 && !p1[i])
177 p1[i] = (random () & 127) + 3;
180 for (i = 0; i < len2; i++)
182 buf1[i] = random () & 255;
183 if (!buf1[i])
184 buf1[i] = (random () & 127) + 3;
186 buf1[len2] = 0;
188 FOR_EACH_IMPL (impl, 1)
190 memset (p2 - 64, '\1', align2 + 64);
191 memset (p2 + align2 + len2 + 1, '\1', 512 - align2 - len2 - 1);
192 memcpy (p2 + align2, buf1, len2 + 1);
193 res = (unsigned char *) CALL (impl, (char *) (p2 + align2),
194 (char *) (p1 + align1), N);
195 if (res != p2 + align2)
197 error (0, 0, "Iteration %zd - wrong result in function %s "
198 "(%zd, %zd, %zd, %zd, %zd) %p != %p",
199 n, impl->name, align1, align2, len1, len2, N,
200 res, p2 + align2);
201 ret = 1;
203 for (j = 0; j < align2 + 64; ++j)
205 if (p2[j - 64] != '\1')
207 error (0, 0, "Iteration %zd - garbage before dst, %s "
208 "%zd, %zd, %zd, %zd, %zd)",
209 n, impl->name, align1, align2, len1, len2, N);
210 ret = 1;
211 break;
214 if (memcmp (p2 + align2, buf1, len2))
216 error (0, 0, "Iteration %zd - garbage in string before, %s "
217 "(%zd, %zd, %zd, %zd, %zd)",
218 n, impl->name, align1, align2, len1, len2, N);
219 ret = 1;
222 if ((len1 + 1) > N)
223 j = align2 + N + 1 + len2;
224 else
225 j = align2 + len1 + 1 + len2;
226 for (; j < 512; ++j)
228 if (p2[j] != '\1')
230 error (0, 0, "Iteration %zd - garbage after, %s "
231 "(%zd, %zd, %zd, %zd, %zd)",
232 n, impl->name, align1, align2, len1, len2, N);
233 ret = 1;
234 break;
237 if (len1 + 1 > N)
239 if (p2[align2 + N + len2] != '\0')
241 error (0, 0, "Iteration %zd - there is no zero at the "
242 "end of output string, %s (%zd, %zd, %zd, %zd, %zd)",
243 n, impl->name, align1, align2, len1, len2, N);
244 ret = 1;
247 if (memcmp (p1 + align1, p2 + align2 + len2,
248 (len1 + 1) > N ? N : len1 + 1))
250 error (0, 0, "Iteration %zd - different strings, %s "
251 "(%zd, %zd, %zd, %zd, %zd)",
252 n, impl->name, align1, align2, len1, len2, N);
253 ret = 1;
260 main (void)
262 size_t i, n;
264 test_init ();
266 printf ("%28s", "");
267 FOR_EACH_IMPL (impl, 0)
268 printf ("\t%s", impl->name);
269 putchar ('\n');
271 for (n = 2; n <= 2048; n*=4)
273 do_test (0, 2, 2, 2, n, 127);
274 do_test (0, 0, 4, 4, n, 127);
275 do_test (4, 0, 4, 4, n, 255);
276 do_test (0, 0, 8, 8, n, 127);
277 do_test (0, 8, 8, 8, n, 127);
279 for (i = 1; i < 8; ++i)
281 do_test (0, 0, 8 << i, 8 << i, n, 127);
282 do_test (8 - i, 2 * i, 8 << i, 8 << i, n, 127);
283 do_test (0, 0, 8 << i, 2 << i, n, 127);
284 do_test (8 - i, 2 * i, 8 << i, 2 << i, n, 127);
287 for (i = 1; i < 8; ++i)
289 do_test (i, 2 * i, 8 << i, 1, n, 127);
290 do_test (2 * i, i, 8 << i, 1, n, 255);
291 do_test (i, i, 8 << i, 10, n, 127);
295 do_random_tests ();
296 return ret;