s390x: Update math: redirect roundeven function
[glibc.git] / string / test-strncat.c
blob37ea26ea057938a4785d82b0d14693242e31f307
1 /* Test strncat functions.
2 Copyright (C) 2011-2021 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 <https://www.gnu.org/licenses/>. */
19 #define TEST_MAIN
20 #ifndef WIDE
21 # define TEST_NAME "strncat"
22 #else
23 # define TEST_NAME "wcsncat"
24 #endif /* WIDE */
25 #include "test-string.h"
27 #ifndef WIDE
28 # define STRNCAT strncat
29 # define CHAR char
30 # define UCHAR unsigned char
31 # define SIMPLE_STRNCAT simple_strncat
32 # define STUPID_STRNCAT stupid_strncat
33 # define STRLEN strlen
34 # define MEMSET memset
35 # define MEMCPY memcpy
36 # define MEMCMP memcmp
37 # define BIG_CHAR CHAR_MAX
38 # define SMALL_CHAR 127
39 #else
40 # include <wchar.h>
41 # define STRNCAT wcsncat
42 # define CHAR wchar_t
43 # define UCHAR wchar_t
44 # define SIMPLE_STRNCAT simple_wcsncat
45 # define STUPID_STRNCAT stupid_wcsncat
46 # define STRLEN wcslen
47 # define MEMSET wmemset
48 # define MEMCPY wmemcpy
49 # define MEMCMP wmemcmp
50 # define BIG_CHAR WCHAR_MAX
51 # define SMALL_CHAR 1273
52 #endif /* WIDE */
54 typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
55 CHAR *STUPID_STRNCAT (CHAR *, const CHAR *, size_t);
56 CHAR *SIMPLE_STRNCAT (CHAR *, const CHAR *, size_t);
58 IMPL (STUPID_STRNCAT, 0)
59 IMPL (STRNCAT, 2)
61 CHAR *
62 STUPID_STRNCAT (CHAR *dst, const CHAR *src, size_t n)
64 CHAR *ret = dst;
65 while (*dst++ != '\0');
66 --dst;
67 while (n--)
68 if ((*dst++ = *src++) == '\0')
69 return ret;
70 *dst = '\0';
71 return ret;
74 static void
75 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t n)
77 size_t k = STRLEN (dst);
78 if (CALL (impl, dst, src, n) != dst)
80 error (0, 0, "Wrong result in function %s %p != %p", impl->name,
81 CALL (impl, dst, src, n), dst);
82 ret = 1;
83 return;
86 size_t len = STRLEN (src);
87 if (MEMCMP (dst + k, src, len + 1 > n ? n : len + 1) != 0)
89 error (0, 0, "Incorrect concatenation in function %s",
90 impl->name);
91 ret = 1;
92 return;
94 if (n < len && dst[k + n] != '\0')
96 error (0, 0, "There is no zero in the end of output string in %s",
97 impl->name);
98 ret = 1;
99 return;
103 static void
104 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
105 size_t n, int max_char)
107 size_t i;
108 CHAR *s1, *s2;
110 align1 &= 7;
111 if ((align1 + len1) * sizeof (CHAR) >= page_size)
112 return;
113 if ((align1 + n) * sizeof (CHAR) > page_size)
114 return;
115 align2 &= 7;
116 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
117 return;
118 if ((align2 + len1 + n) * sizeof (CHAR) > page_size)
119 return;
120 s1 = (CHAR *) (buf1) + align1;
121 s2 = (CHAR *) (buf2) + align2;
123 for (i = 0; i < len1; ++i)
124 s1[i] = 32 + 23 * i % (max_char - 32);
125 s1[len1] = '\0';
127 for (i = 0; i < len2; i++)
128 s2[i] = 32 + 23 * i % (max_char - 32);
130 FOR_EACH_IMPL (impl, 0)
132 s2[len2] = '\0';
133 do_one_test (impl, s2, s1, n);
137 static void
138 do_overflow_tests (void)
140 size_t i, j, len;
141 const size_t one = 1;
142 CHAR *s1, *s2;
143 uintptr_t s1_addr;
144 s1 = (CHAR *) buf1;
145 s2 = (CHAR *) buf2;
146 s1_addr = (uintptr_t)s1;
147 for (j = 0; j < 200; ++j)
148 s2[j] = 32 + 23 * j % (BIG_CHAR - 32);
149 s2[200] = 0;
150 for (i = 0; i < 750; ++i) {
151 for (j = 0; j < i; ++j)
152 s1[j] = 32 + 23 * j % (BIG_CHAR - 32);
153 s1[i] = '\0';
155 FOR_EACH_IMPL (impl, 0)
157 s2[200] = '\0';
158 do_one_test (impl, s2, s1, SIZE_MAX - i);
159 s2[200] = '\0';
160 do_one_test (impl, s2, s1, i - s1_addr);
161 s2[200] = '\0';
162 do_one_test (impl, s2, s1, -s1_addr - i);
163 s2[200] = '\0';
164 do_one_test (impl, s2, s1, SIZE_MAX - s1_addr - i);
165 s2[200] = '\0';
166 do_one_test (impl, s2, s1, SIZE_MAX - s1_addr + i);
169 len = 0;
170 for (j = 8 * sizeof(size_t) - 1; j ; --j)
172 len |= one << j;
173 FOR_EACH_IMPL (impl, 0)
175 s2[200] = '\0';
176 do_one_test (impl, s2, s1, len - i);
177 s2[200] = '\0';
178 do_one_test (impl, s2, s1, len + i);
179 s2[200] = '\0';
180 do_one_test (impl, s2, s1, len - s1_addr - i);
181 s2[200] = '\0';
182 do_one_test (impl, s2, s1, len - s1_addr + i);
184 s2[200] = '\0';
185 do_one_test (impl, s2, s1, ~len - i);
186 s2[200] = '\0';
187 do_one_test (impl, s2, s1, ~len + i);
188 s2[200] = '\0';
189 do_one_test (impl, s2, s1, ~len - s1_addr - i);
190 s2[200] = '\0';
191 do_one_test (impl, s2, s1, ~len - s1_addr + i);
197 static void
198 do_random_tests (void)
200 size_t i, j, n, align1, align2, len1, len2, N;
201 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
202 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
203 UCHAR *p3 = (UCHAR *) buf1;
204 UCHAR *res;
205 fprintf (stdout, "Number of iterations in random test = %zd\n",
206 ITERATIONS);
207 for (n = 0; n < ITERATIONS; n++)
209 N = random () & 255;
210 align1 = random () & 31;
211 if (random () & 1)
212 align2 = random () & 31;
213 else
214 align2 = align1 + (random () & 24);
215 len1 = random () & 511;
216 if (len1 + align2 > 512)
217 len2 = random () & 7;
218 else
219 len2 = (512 - len1 - align2) * (random () & (1024 * 1024 - 1))
220 / (1024 * 1024);
221 j = align1;
222 if (align2 + len2 > j)
223 j = align2 + len2;
224 if (len1 + j >= 511)
225 len1 = 510 - j - (random () & 7);
226 if (len1 >= 512)
227 len1 = 0;
228 if (align1 + len1 < 512 - 8)
230 j = 510 - align1 - len1 - (random () & 31);
231 if (j > 0 && j < 512)
232 align1 += j;
234 j = len1 + align1 + 64;
235 if (j > 512)
236 j = 512;
237 for (i = 0; i < j; i++)
239 if (i == len1 + align1)
240 p1[i] = 0;
241 else
243 p1[i] = random () & BIG_CHAR;
244 if (i >= align1 && i < len1 + align1 && !p1[i])
245 p1[i] = (random () & SMALL_CHAR) + 3;
248 for (i = 0; i < len2; i++)
250 p3[i] = random () & BIG_CHAR;
251 if (!p3[i])
252 p3[i] = (random () & SMALL_CHAR) + 3;
254 p3[len2] = 0;
256 FOR_EACH_IMPL (impl, 1)
258 MEMSET (p2 - 64, '\1', align2 + 64);
259 MEMSET (p2 + align2 + len2 + 1, '\1', 512 - align2 - len2 - 1);
260 MEMCPY (p2 + align2, p3, len2 + 1);
261 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2),
262 (CHAR *) (p1 + align1), N);
263 if (res != p2 + align2)
265 error (0, 0, "Iteration %zd - wrong result in function %s "
266 "(%zd, %zd, %zd, %zd, %zd) %p != %p",
267 n, impl->name, align1, align2, len1, len2, N,
268 res, p2 + align2);
269 ret = 1;
271 for (j = 0; j < align2 + 64; ++j)
273 if (p2[j - 64] != '\1')
275 error (0, 0, "Iteration %zd - garbage before dst, %s "
276 "%zd, %zd, %zd, %zd, %zd)",
277 n, impl->name, align1, align2, len1, len2, N);
278 ret = 1;
279 break;
282 if (MEMCMP (p2 + align2, p3, len2))
284 error (0, 0, "Iteration %zd - garbage in string before, %s "
285 "(%zd, %zd, %zd, %zd, %zd)",
286 n, impl->name, align1, align2, len1, len2, N);
287 ret = 1;
290 if ((len1 + 1) > N)
291 j = align2 + N + 1 + len2;
292 else
293 j = align2 + len1 + 1 + len2;
294 for (; j < 512; ++j)
296 if (p2[j] != '\1')
298 error (0, 0, "Iteration %zd - garbage after, %s "
299 "(%zd, %zd, %zd, %zd, %zd)",
300 n, impl->name, align1, align2, len1, len2, N);
301 ret = 1;
302 break;
305 if (len1 + 1 > N)
307 if (p2[align2 + N + len2] != '\0')
309 error (0, 0, "Iteration %zd - there is no zero at the "
310 "end of output string, %s (%zd, %zd, %zd, %zd, %zd)",
311 n, impl->name, align1, align2, len1, len2, N);
312 ret = 1;
315 if (MEMCMP (p1 + align1, p2 + align2 + len2,
316 (len1 + 1) > N ? N : len1 + 1))
318 error (0, 0, "Iteration %zd - different strings, %s "
319 "(%zd, %zd, %zd, %zd, %zd)",
320 n, impl->name, align1, align2, len1, len2, N);
321 ret = 1;
328 test_main (void)
330 size_t i, n;
332 test_init ();
334 printf ("%28s", "");
335 FOR_EACH_IMPL (impl, 0)
336 printf ("\t%s", impl->name);
337 putchar ('\n');
339 for (n = 2; n <= 2048; n*=4)
341 do_test (0, 2, 2, 2, n, SMALL_CHAR);
342 do_test (0, 0, 4, 4, n, SMALL_CHAR);
343 do_test (4, 0, 4, 4, n, BIG_CHAR);
344 do_test (0, 0, 8, 8, n, SMALL_CHAR);
345 do_test (0, 8, 8, 8, n, SMALL_CHAR);
347 do_test (0, 2, 2, 2, SIZE_MAX, SMALL_CHAR);
348 do_test (0, 0, 4, 4, SIZE_MAX, SMALL_CHAR);
349 do_test (4, 0, 4, 4, SIZE_MAX, BIG_CHAR);
350 do_test (0, 0, 8, 8, SIZE_MAX, SMALL_CHAR);
351 do_test (0, 8, 8, 8, SIZE_MAX, SMALL_CHAR);
353 for (i = 1; i < 8; ++i)
355 do_test (0, 0, 8 << i, 8 << i, n, SMALL_CHAR);
356 do_test (8 - i, 2 * i, 8 << i, 8 << i, n, SMALL_CHAR);
357 do_test (0, 0, 8 << i, 2 << i, n, SMALL_CHAR);
358 do_test (8 - i, 2 * i, 8 << i, 2 << i, n, SMALL_CHAR);
360 do_test (0, 0, 8 << i, 8 << i, SIZE_MAX, SMALL_CHAR);
361 do_test (8 - i, 2 * i, 8 << i, 8 << i, SIZE_MAX, SMALL_CHAR);
362 do_test (0, 0, 8 << i, 2 << i, SIZE_MAX, SMALL_CHAR);
363 do_test (8 - i, 2 * i, 8 << i, 2 << i, SIZE_MAX, SMALL_CHAR);
366 for (i = 1; i < 8; ++i)
368 do_test (i, 2 * i, 8 << i, 1, n, SMALL_CHAR);
369 do_test (2 * i, i, 8 << i, 1, n, BIG_CHAR);
370 do_test (i, i, 8 << i, 10, n, SMALL_CHAR);
372 do_test (i, 2 * i, 8 << i, 1, SIZE_MAX, SMALL_CHAR);
373 do_test (2 * i, i, 8 << i, 1, SIZE_MAX, BIG_CHAR);
374 do_test (i, i, 8 << i, 10, SIZE_MAX, SMALL_CHAR);
378 do_random_tests ();
379 do_overflow_tests ();
380 return ret;
383 #include <support/test-driver.c>