Don't use -Wno-strict-prototypes in timezone/.
[glibc.git] / string / test-strncat.c
blob366e6fccb58452ebf169e793f6153cc96904d67b
1 /* Test and measure strncat functions.
2 Copyright (C) 2011-2015 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;
72 static void
73 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
74 size_t n, int max_char)
76 size_t i;
77 char *s1, *s2;
79 align1 &= 7;
80 if (align1 + len1 >= page_size)
81 return;
82 if (align1 + n > page_size)
83 return;
84 align2 &= 7;
85 if (align2 + len1 + len2 >= page_size)
86 return;
87 if (align2 + len1 + n > page_size)
88 return;
89 s1 = (char *) (buf1 + align1);
90 s2 = (char *) (buf2 + align2);
92 for (i = 0; i < len1; ++i)
93 s1[i] = 32 + 23 * i % (max_char - 32);
94 s1[len1] = '\0';
96 for (i = 0; i < len2; i++)
97 s2[i] = 32 + 23 * i % (max_char - 32);
99 FOR_EACH_IMPL (impl, 0)
101 s2[len2] = '\0';
102 do_one_test (impl, s2, s1, n);
106 static void
107 do_random_tests (void)
109 size_t i, j, n, align1, align2, len1, len2, N;
110 unsigned char *p1 = buf1 + page_size - 512;
111 unsigned char *p2 = buf2 + page_size - 512;
112 unsigned char *res;
113 fprintf (stdout, "Number of iterations in random test = %zd\n",
114 ITERATIONS);
115 for (n = 0; n < ITERATIONS; n++)
117 N = random () & 255;
118 align1 = random () & 31;
119 if (random () & 1)
120 align2 = random () & 31;
121 else
122 align2 = align1 + (random () & 24);
123 len1 = random () & 511;
124 if (len1 + align2 > 512)
125 len2 = random () & 7;
126 else
127 len2 = (512 - len1 - align2) * (random () & (1024 * 1024 - 1))
128 / (1024 * 1024);
129 j = align1;
130 if (align2 + len2 > j)
131 j = align2 + len2;
132 if (len1 + j >= 511)
133 len1 = 510 - j - (random () & 7);
134 if (len1 >= 512)
135 len1 = 0;
136 if (align1 + len1 < 512 - 8)
138 j = 510 - align1 - len1 - (random () & 31);
139 if (j > 0 && j < 512)
140 align1 += j;
142 j = len1 + align1 + 64;
143 if (j > 512)
144 j = 512;
145 for (i = 0; i < j; i++)
147 if (i == len1 + align1)
148 p1[i] = 0;
149 else
151 p1[i] = random () & 255;
152 if (i >= align1 && i < len1 + align1 && !p1[i])
153 p1[i] = (random () & 127) + 3;
156 for (i = 0; i < len2; i++)
158 buf1[i] = random () & 255;
159 if (!buf1[i])
160 buf1[i] = (random () & 127) + 3;
162 buf1[len2] = 0;
164 FOR_EACH_IMPL (impl, 1)
166 memset (p2 - 64, '\1', align2 + 64);
167 memset (p2 + align2 + len2 + 1, '\1', 512 - align2 - len2 - 1);
168 memcpy (p2 + align2, buf1, len2 + 1);
169 res = (unsigned char *) CALL (impl, (char *) (p2 + align2),
170 (char *) (p1 + align1), N);
171 if (res != p2 + align2)
173 error (0, 0, "Iteration %zd - wrong result in function %s "
174 "(%zd, %zd, %zd, %zd, %zd) %p != %p",
175 n, impl->name, align1, align2, len1, len2, N,
176 res, p2 + align2);
177 ret = 1;
179 for (j = 0; j < align2 + 64; ++j)
181 if (p2[j - 64] != '\1')
183 error (0, 0, "Iteration %zd - garbage before dst, %s "
184 "%zd, %zd, %zd, %zd, %zd)",
185 n, impl->name, align1, align2, len1, len2, N);
186 ret = 1;
187 break;
190 if (memcmp (p2 + align2, buf1, len2))
192 error (0, 0, "Iteration %zd - garbage in string before, %s "
193 "(%zd, %zd, %zd, %zd, %zd)",
194 n, impl->name, align1, align2, len1, len2, N);
195 ret = 1;
198 if ((len1 + 1) > N)
199 j = align2 + N + 1 + len2;
200 else
201 j = align2 + len1 + 1 + len2;
202 for (; j < 512; ++j)
204 if (p2[j] != '\1')
206 error (0, 0, "Iteration %zd - garbage after, %s "
207 "(%zd, %zd, %zd, %zd, %zd)",
208 n, impl->name, align1, align2, len1, len2, N);
209 ret = 1;
210 break;
213 if (len1 + 1 > N)
215 if (p2[align2 + N + len2] != '\0')
217 error (0, 0, "Iteration %zd - there is no zero at the "
218 "end of output string, %s (%zd, %zd, %zd, %zd, %zd)",
219 n, impl->name, align1, align2, len1, len2, N);
220 ret = 1;
223 if (memcmp (p1 + align1, p2 + align2 + len2,
224 (len1 + 1) > N ? N : len1 + 1))
226 error (0, 0, "Iteration %zd - different strings, %s "
227 "(%zd, %zd, %zd, %zd, %zd)",
228 n, impl->name, align1, align2, len1, len2, N);
229 ret = 1;
236 main (void)
238 size_t i, n;
240 test_init ();
242 printf ("%28s", "");
243 FOR_EACH_IMPL (impl, 0)
244 printf ("\t%s", impl->name);
245 putchar ('\n');
247 for (n = 2; n <= 2048; n*=4)
249 do_test (0, 2, 2, 2, n, 127);
250 do_test (0, 0, 4, 4, n, 127);
251 do_test (4, 0, 4, 4, n, 255);
252 do_test (0, 0, 8, 8, n, 127);
253 do_test (0, 8, 8, 8, n, 127);
255 for (i = 1; i < 8; ++i)
257 do_test (0, 0, 8 << i, 8 << i, n, 127);
258 do_test (8 - i, 2 * i, 8 << i, 8 << i, n, 127);
259 do_test (0, 0, 8 << i, 2 << i, n, 127);
260 do_test (8 - i, 2 * i, 8 << i, 2 << i, n, 127);
263 for (i = 1; i < 8; ++i)
265 do_test (i, 2 * i, 8 << i, 1, n, 127);
266 do_test (2 * i, i, 8 << i, 1, n, 255);
267 do_test (i, i, 8 << i, 10, n, 127);
271 do_random_tests ();
272 return ret;