s390: Update ulps
[glibc.git] / string / test-strncpy.c
blob84a3b83bbc1858029b5ec88f16f811fe4e9fd0ca
1 /* Test strncpy functions.
2 Copyright (C) 1999-2021 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 <https://www.gnu.org/licenses/>. */
20 #ifdef WIDE
21 # include <wchar.h>
22 # define CHAR wchar_t
23 # define UCHAR wchar_t
24 # define BIG_CHAR WCHAR_MAX
25 # define SMALL_CHAR 1273
26 # define MEMCMP wmemcmp
27 # define MEMSET wmemset
28 # define STRNLEN wcsnlen
29 #else
30 # define CHAR char
31 # define UCHAR unsigned char
32 # define BIG_CHAR CHAR_MAX
33 # define SMALL_CHAR 127
34 # define MEMCMP memcmp
35 # define MEMSET memset
36 # define STRNLEN strnlen
37 #endif /* !WIDE */
40 #ifndef STRNCPY_RESULT
41 # define STRNCPY_RESULT(dst, len, n) dst
42 # define TEST_MAIN
43 # ifndef WIDE
44 # define TEST_NAME "strncpy"
45 # else
46 # define TEST_NAME "wcsncpy"
47 # endif /* WIDE */
48 # include "test-string.h"
49 # ifndef WIDE
50 # define SIMPLE_STRNCPY simple_strncpy
51 # define STUPID_STRNCPY stupid_strncpy
52 # define STRNCPY strncpy
53 # else
54 # define SIMPLE_STRNCPY simple_wcsncpy
55 # define STUPID_STRNCPY stupid_wcsncpy
56 # define STRNCPY wcsncpy
57 # endif /* WIDE */
59 CHAR *SIMPLE_STRNCPY (CHAR *, const CHAR *, size_t);
60 CHAR *STUPID_STRNCPY (CHAR *, const CHAR *, size_t);
62 IMPL (STUPID_STRNCPY, 0)
63 IMPL (SIMPLE_STRNCPY, 0)
64 IMPL (STRNCPY, 1)
66 CHAR *
67 SIMPLE_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
69 CHAR *ret = dst;
70 while (n--)
71 if ((*dst++ = *src++) == '\0')
73 while (n--)
74 *dst++ = '\0';
75 return ret;
77 return ret;
80 CHAR *
81 STUPID_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
83 size_t nc = STRNLEN (src, n);
84 size_t i;
86 for (i = 0; i < nc; ++i)
87 dst[i] = src[i];
88 for (; i < n; ++i)
89 dst[i] = '\0';
90 return dst;
92 #endif /* !STRNCPY_RESULT */
94 typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
96 static void
97 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t len, size_t n)
99 if (CALL (impl, dst, src, n) != STRNCPY_RESULT (dst, len, n))
101 error (0, 0, "Wrong result in function %s %p %p", impl->name,
102 CALL (impl, dst, src, n), dst);
103 ret = 1;
104 return;
107 if (memcmp (dst, src, (len > n ? n : len) * sizeof (CHAR)) != 0)
109 error (0, 0, "Wrong result in function %s", impl->name);
110 ret = 1;
111 return;
114 if (n > len)
116 size_t i;
118 for (i = len; i < n; ++i)
119 if (dst [i] != '\0')
121 error (0, 0, "Wrong result in function %s", impl->name);
122 ret = 1;
123 return;
128 static void
129 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
131 size_t i;
132 CHAR *s1, *s2;
134 /* For wcsncpy: align1 and align2 here mean alignment not in bytes,
135 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)). */
136 align1 &= 7;
137 if ((align1 + len) * sizeof (CHAR) >= page_size)
138 return;
140 align2 &= 7;
141 if ((align2 + len) * sizeof (CHAR) >= page_size)
142 return;
144 s1 = (CHAR *) (buf1) + align1;
145 s2 = (CHAR *) (buf2) + align2;
147 for (i = 0; i < len; ++i)
148 s1[i] = 32 + 23 * i % (max_char - 32);
149 s1[len] = 0;
150 for (i = len + 1; (i + align1) * sizeof (CHAR) < page_size && i < len + 64;
151 ++i)
152 s1[i] = 32 + 32 * i % (max_char - 32);
154 FOR_EACH_IMPL (impl, 0)
155 do_one_test (impl, s2, s1, len, n);
158 static void
159 do_page_tests (void)
161 CHAR *s1, *s2;
162 const size_t maxoffset = 64;
164 /* Put s1 at the maxoffset from the edge of buf1's last page. */
165 s1 = (CHAR *) buf1 + BUF1PAGES * page_size / sizeof(CHAR) - maxoffset;
166 /* s2 needs room to put a string with size of maxoffset + 1 at s2 +
167 (maxoffset - 1). */
168 s2 = (CHAR *) buf2 + page_size / sizeof(CHAR) - maxoffset * 2;
170 MEMSET (s1, 'a', maxoffset - 1);
171 s1[maxoffset - 1] = '\0';
173 /* Both strings are bounded to a page with read/write access and the next
174 page is protected with PROT_NONE (meaning that any access outside of the
175 page regions will trigger an invalid memory access).
177 The loop copies the string s1 for all possible offsets up to maxoffset
178 for both inputs with a size larger than s1 (so memory access outside the
179 expected memory regions might trigger invalid access). */
181 for (size_t off1 = 0; off1 < maxoffset; off1++)
183 for (size_t off2 = 0; off2 < maxoffset; off2++)
185 FOR_EACH_IMPL (impl, 0)
186 do_one_test (impl, s2 + off2, s1 + off1, maxoffset - off1 - 1,
187 maxoffset + (maxoffset - off2));
192 static void
193 do_random_tests (void)
195 size_t i, j, n, align1, align2, len, size, mode;
196 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
197 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
198 UCHAR *res;
200 for (n = 0; n < ITERATIONS; n++)
202 /* For wcsncpy: align1 and align2 here mean align not in bytes,
203 but in wchar_ts, in bytes it will equal to align * (sizeof
204 (wchar_t)). */
206 mode = random ();
207 if (mode & 1)
209 size = random () & 255;
210 align1 = 512 - size - (random () & 15);
211 if (mode & 2)
212 align2 = align1 - (random () & 24);
213 else
214 align2 = align1 - (random () & 31);
215 if (mode & 4)
217 j = align1;
218 align1 = align2;
219 align2 = j;
221 if (mode & 8)
222 len = size - (random () & 31);
223 else
224 len = 512;
225 if (len >= 512)
226 len = random () & 511;
228 else
230 align1 = random () & 31;
231 if (mode & 2)
232 align2 = random () & 31;
233 else
234 align2 = align1 + (random () & 24);
235 len = random () & 511;
236 j = align1;
237 if (align2 > j)
238 j = align2;
239 if (mode & 4)
241 size = random () & 511;
242 if (size + j > 512)
243 size = 512 - j - (random () & 31);
245 else
246 size = 512 - j;
247 if ((mode & 8) && len + j >= 512)
248 len = 512 - j - (random () & 7);
250 j = len + align1 + 64;
251 if (j > 512)
252 j = 512;
253 for (i = 0; i < j; i++)
255 if (i == len + align1)
256 p1[i] = 0;
257 else
259 p1[i] = random () & BIG_CHAR;
260 if (i >= align1 && i < len + align1 && !p1[i])
261 p1[i] = (random () & SMALL_CHAR) + 3;
265 FOR_EACH_IMPL (impl, 1)
267 MEMSET (p2 - 64, '\1', 512 + 64);
268 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2),
269 (CHAR *) (p1 + align1), size);
270 if (res != STRNCPY_RESULT (p2 + align2, len, size))
272 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
273 n, impl->name, align1, align2, len, res,
274 STRNCPY_RESULT (p2 + align2, len, size));
275 ret = 1;
277 for (j = 0; j < align2 + 64; ++j)
279 if (p2[j - 64] != '\1')
281 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
282 n, impl->name, align1, align2, len);
283 ret = 1;
284 break;
287 j = align2 + len + 1;
288 if (size + align2 > j)
289 j = size + align2;
290 for (; j < 512; ++j)
292 if (p2[j] != '\1')
294 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
295 n, impl->name, align1, align2, len);
296 ret = 1;
297 break;
300 for (j = align2 + len + 1; j < align2 + size; ++j)
301 if (p2[j])
303 error (0, 0, "Iteration %zd - garbage after size, %s (%zd, %zd, %zd)",
304 n, impl->name, align1, align2, len);
305 ret = 1;
306 break;
308 j = len + 1;
309 if (size < j)
310 j = size;
311 if (MEMCMP (p1 + align1, p2 + align2, j))
313 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
314 n, impl->name, align1, align2, len);
315 ret = 1;
322 test_main (void)
324 size_t i;
326 test_init ();
328 printf ("%28s", "");
329 FOR_EACH_IMPL (impl, 0)
330 printf ("\t%s", impl->name);
331 putchar ('\n');
333 for (i = 1; i < 8; ++i)
335 do_test (i, i, 16, 16, SMALL_CHAR);
336 do_test (i, i, 16, 16, BIG_CHAR);
337 do_test (i, 2 * i, 16, 16, SMALL_CHAR);
338 do_test (2 * i, i, 16, 16, BIG_CHAR);
339 do_test (8 - i, 2 * i, 1 << i, 2 << i, SMALL_CHAR);
340 do_test (2 * i, 8 - i, 2 << i, 1 << i, SMALL_CHAR);
341 do_test (8 - i, 2 * i, 1 << i, 2 << i, BIG_CHAR);
342 do_test (2 * i, 8 - i, 2 << i, 1 << i, BIG_CHAR);
345 for (i = 1; i < 8; ++i)
347 do_test (0, 0, 4 << i, 8 << i, SMALL_CHAR);
348 do_test (0, 0, 16 << i, 8 << i, SMALL_CHAR);
349 do_test (8 - i, 2 * i, 4 << i, 8 << i, SMALL_CHAR);
350 do_test (8 - i, 2 * i, 16 << i, 8 << i, SMALL_CHAR);
353 do_random_tests ();
354 do_page_tests ();
355 return ret;
358 #include <support/test-driver.c>