Set LC_ALL=C before sed.
[glibc.git] / string / test-strncat.c
blob41d409b04369b33257799cea50bd0ecb5f7514bc
1 /* Test and measure strncat functions.
2 Copyright (C) 2011-2013 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 #define TEST_NAME "strncat"
21 #include "test-string.h"
23 typedef char *(*proto_t) (char *, const char *, size_t);
24 char *stupid_strncat (char *, const char *, size_t);
25 char *simple_strncat (char *, const char *, size_t);
27 IMPL (stupid_strncat, 0)
28 IMPL (strncat, 2)
30 char *
31 stupid_strncat (char *dst, const char *src, size_t n)
33 char *ret = dst;
34 while (*dst++ != '\0');
35 --dst;
36 while (n--)
37 if ( (*dst++ = *src++) == '\0')
38 return ret;
39 *dst = '\0';
40 return ret;
43 static void
44 do_one_test (impl_t *impl, char *dst, const char *src, size_t n)
46 size_t k = strlen (dst);
47 if (CALL (impl, dst, src, n) != dst)
49 error (0, 0, "Wrong result in function %s %p != %p", impl->name,
50 CALL (impl, dst, src, n), dst);
51 ret = 1;
52 return;
55 size_t len = strlen (src);
56 if (memcmp (dst + k, src, len + 1 > n ? n : len + 1) != 0)
58 error (0, 0, "Incorrect cancatination in function %s",
59 impl->name);
60 ret = 1;
61 return;
63 if (n < len && dst[k + n] != '\0')
65 error (0, 0, "There is no zero in the end of output string in %s",
66 impl->name);
67 ret = 1;
68 return;
70 if (HP_TIMING_AVAIL)
72 hp_timing_t start __attribute ((unused));
73 hp_timing_t stop __attribute ((unused));
74 hp_timing_t best_time = ~ (hp_timing_t) 0;
75 size_t i;
77 for (i = 0; i < 32; ++i)
79 dst[k] = '\0';
80 HP_TIMING_NOW (start);
81 CALL (impl, dst, src, n);
82 HP_TIMING_NOW (stop);
83 HP_TIMING_BEST (best_time, start, stop);
86 printf ("\t%zd", (size_t) best_time);
90 static void
91 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
92 size_t n, int max_char)
94 size_t i;
95 char *s1, *s2;
97 align1 &= 7;
98 if (align1 + len1 >= page_size)
99 return;
100 if (align1 + n > page_size)
101 return;
102 align2 &= 7;
103 if (align2 + len1 + len2 >= page_size)
104 return;
105 if (align2 + len1 + n > page_size)
106 return;
107 s1 = (char *) (buf1 + align1);
108 s2 = (char *) (buf2 + align2);
110 for (i = 0; i < len1; ++i)
111 s1[i] = 32 + 23 * i % (max_char - 32);
112 s1[len1] = '\0';
114 for (i = 0; i < len2; i++)
115 s2[i] = 32 + 23 * i % (max_char - 32);
117 if (HP_TIMING_AVAIL)
118 printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
119 len1, len2, align1, align2, n);
121 FOR_EACH_IMPL (impl, 0)
123 s2[len2] = '\0';
124 do_one_test (impl, s2, s1, n);
127 if (HP_TIMING_AVAIL)
128 putchar ('\n');
131 static void
132 do_random_tests (void)
134 size_t i, j, n, align1, align2, len1, len2, N;
135 unsigned char *p1 = buf1 + page_size - 512;
136 unsigned char *p2 = buf2 + page_size - 512;
137 unsigned char *res;
138 fprintf (stdout, "Number of iterations in random test = %zd\n",
139 ITERATIONS);
140 for (n = 0; n < ITERATIONS; n++)
142 N = random () & 255;
143 align1 = random () & 31;
144 if (random () & 1)
145 align2 = random () & 31;
146 else
147 align2 = align1 + (random () & 24);
148 len1 = random () & 511;
149 if (len1 + align2 > 512)
150 len2 = random () & 7;
151 else
152 len2 = (512 - len1 - align2) * (random () & (1024 * 1024 - 1))
153 / (1024 * 1024);
154 j = align1;
155 if (align2 + len2 > j)
156 j = align2 + len2;
157 if (len1 + j >= 511)
158 len1 = 510 - j - (random () & 7);
159 if (len1 >= 512)
160 len1 = 0;
161 if (align1 + len1 < 512 - 8)
163 j = 510 - align1 - len1 - (random () & 31);
164 if (j > 0 && j < 512)
165 align1 += j;
167 j = len1 + align1 + 64;
168 if (j > 512)
169 j = 512;
170 for (i = 0; i < j; i++)
172 if (i == len1 + align1)
173 p1[i] = 0;
174 else
176 p1[i] = random () & 255;
177 if (i >= align1 && i < len1 + align1 && !p1[i])
178 p1[i] = (random () & 127) + 3;
181 for (i = 0; i < len2; i++)
183 buf1[i] = random () & 255;
184 if (!buf1[i])
185 buf1[i] = (random () & 127) + 3;
187 buf1[len2] = 0;
189 FOR_EACH_IMPL (impl, 1)
191 memset (p2 - 64, '\1', align2 + 64);
192 memset (p2 + align2 + len2 + 1, '\1', 512 - align2 - len2 - 1);
193 memcpy (p2 + align2, buf1, len2 + 1);
194 res = (unsigned char *) CALL (impl, (char *) (p2 + align2),
195 (char *) (p1 + align1), N);
196 if (res != p2 + align2)
198 error (0, 0, "Iteration %zd - wrong result in function %s "
199 "(%zd, %zd, %zd, %zd, %zd) %p != %p",
200 n, impl->name, align1, align2, len1, len2, N,
201 res, p2 + align2);
202 ret = 1;
204 for (j = 0; j < align2 + 64; ++j)
206 if (p2[j - 64] != '\1')
208 error (0, 0, "Iteration %zd - garbage before dst, %s "
209 "%zd, %zd, %zd, %zd, %zd)",
210 n, impl->name, align1, align2, len1, len2, N);
211 ret = 1;
212 break;
215 if (memcmp (p2 + align2, buf1, len2))
217 error (0, 0, "Iteration %zd - garbage in string before, %s "
218 "(%zd, %zd, %zd, %zd, %zd)",
219 n, impl->name, align1, align2, len1, len2, N);
220 ret = 1;
223 if ((len1 + 1) > N)
224 j = align2 + N + 1 + len2;
225 else
226 j = align2 + len1 + 1 + len2;
227 for (; j < 512; ++j)
229 if (p2[j] != '\1')
231 error (0, 0, "Iteration %zd - garbage after, %s "
232 "(%zd, %zd, %zd, %zd, %zd)",
233 n, impl->name, align1, align2, len1, len2, N);
234 ret = 1;
235 break;
238 if (len1 + 1 > N)
240 if (p2[align2 + N + len2] != '\0')
242 error (0, 0, "Iteration %zd - there is no zero at the "
243 "end of output string, %s (%zd, %zd, %zd, %zd, %zd)",
244 n, impl->name, align1, align2, len1, len2, N);
245 ret = 1;
248 if (memcmp (p1 + align1, p2 + align2 + len2,
249 (len1 + 1) > N ? N : len1 + 1))
251 error (0, 0, "Iteration %zd - different strings, %s "
252 "(%zd, %zd, %zd, %zd, %zd)",
253 n, impl->name, align1, align2, len1, len2, N);
254 ret = 1;
261 main (void)
263 size_t i, n;
265 test_init ();
267 printf ("%28s", "");
268 FOR_EACH_IMPL (impl, 0)
269 printf ("\t%s", impl->name);
270 putchar ('\n');
272 for (n = 2; n <= 2048; n*=4)
274 do_test (0, 2, 2, 2, n, 127);
275 do_test (0, 0, 4, 4, n, 127);
276 do_test (4, 0, 4, 4, n, 255);
277 do_test (0, 0, 8, 8, n, 127);
278 do_test (0, 8, 8, 8, n, 127);
280 for (i = 1; i < 8; ++i)
282 do_test (0, 0, 8 << i, 8 << i, n, 127);
283 do_test (8 - i, 2 * i, 8 << i, 8 << i, n, 127);
284 do_test (0, 0, 8 << i, 2 << i, n, 127);
285 do_test (8 - i, 2 * i, 8 << i, 2 << i, n, 127);
288 for (i = 1; i < 8; ++i)
290 do_test (i, 2 * i, 8 << i, 1, n, 127);
291 do_test (2 * i, i, 8 << i, 1, n, 255);
292 do_test (i, i, 8 << i, 10, n, 127);
296 do_random_tests ();
297 return ret;