locale: Add more cached data to LC_CTYPE
[glibc.git] / string / test-strnlen.c
blob83c4502b9ab2505810488a42655dd98ec2a17d17
1 /* Test strlen functions.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 "strnlen"
22 #else
23 # define TEST_NAME "wcsnlen"
24 #endif /* !WIDE */
25 #include "test-string.h"
27 #ifndef WIDE
28 # define STRNLEN strnlen
29 # define MEMSET memset
30 # define CHAR char
31 # define BIG_CHAR CHAR_MAX
32 # define MIDDLE_CHAR 127
33 # define SIMPLE_STRNLEN simple_strnlen
34 #else
35 # include <wchar.h>
36 # define STRNLEN wcsnlen
37 # define MEMSET wmemset
38 # define CHAR wchar_t
39 # define BIG_CHAR WCHAR_MAX
40 # define MIDDLE_CHAR 1121
41 # define SIMPLE_STRNLEN simple_wcsnlen
42 #endif /* !WIDE */
44 typedef size_t (*proto_t) (const CHAR *, size_t);
46 IMPL (STRNLEN, 1)
48 /* Naive implementation to verify results. */
49 size_t
50 SIMPLE_STRNLEN (const CHAR *s, size_t maxlen)
52 size_t i;
54 for (i = 0; i < maxlen && s[i]; ++i);
55 return i;
58 static void
59 do_one_test (impl_t *impl, const CHAR *s, size_t maxlen, size_t exp_len)
61 size_t len = CALL (impl, s, maxlen);
62 if (len != exp_len)
64 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
65 len, exp_len);
66 ret = 1;
67 return;
71 static void
72 do_test (size_t align, size_t len, size_t maxlen, int max_char)
74 size_t i;
76 align &= 63;
77 if ((align + len) * sizeof (CHAR) >= page_size)
78 return;
80 CHAR *buf = (CHAR *) (buf1);
82 for (i = 0; i < len; ++i)
83 buf[align + i] = 1 + 11111 * i % max_char;
84 buf[align + len] = 0;
86 FOR_EACH_IMPL (impl, 0)
87 do_one_test (impl, (CHAR *) (buf + align), maxlen, MIN (len, maxlen));
90 static void
91 do_overflow_tests (void)
93 size_t i, j, len;
94 const size_t one = 1;
95 uintptr_t buf_addr = (uintptr_t) buf1;
97 for (i = 0; i < 750; ++i)
99 do_test (0, i, SIZE_MAX - i, BIG_CHAR);
100 do_test (0, i, i - buf_addr, BIG_CHAR);
101 do_test (0, i, -buf_addr - i, BIG_CHAR);
102 do_test (0, i, SIZE_MAX - buf_addr - i, BIG_CHAR);
103 do_test (0, i, SIZE_MAX - buf_addr + i, BIG_CHAR);
105 len = 0;
106 for (j = 8 * sizeof(size_t) - 1; j ; --j)
108 len |= one << j;
109 do_test (0, i, len - i, BIG_CHAR);
110 do_test (0, i, len + i, BIG_CHAR);
111 do_test (0, i, len - buf_addr - i, BIG_CHAR);
112 do_test (0, i, len - buf_addr + i, BIG_CHAR);
114 do_test (0, i, ~len - i, BIG_CHAR);
115 do_test (0, i, ~len + i, BIG_CHAR);
116 do_test (0, i, ~len - buf_addr - i, BIG_CHAR);
117 do_test (0, i, ~len - buf_addr + i, BIG_CHAR);
119 do_test (0, i, -buf_addr, BIG_CHAR);
120 do_test (0, i, j - buf_addr, BIG_CHAR);
121 do_test (0, i, -buf_addr - j, BIG_CHAR);
126 static void
127 do_random_tests (void)
129 size_t i, j, n, align, len;
130 CHAR *p = (CHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
132 for (n = 0; n < ITERATIONS; n++)
134 align = random () & 15;
135 len = random () & 511;
136 if (len + align > 510)
137 len = 511 - align - (random () & 7);
138 j = len + align + 64;
139 if (j > 512)
140 j = 512;
142 for (i = 0; i < j; i++)
144 if (i == len + align)
145 p[i] = 0;
146 else
148 p[i] = random () & 255;
149 if (i >= align && i < len + align && !p[i])
150 p[i] = (random () & 127) + 1;
154 FOR_EACH_IMPL (impl, 1)
156 if (len > 0
157 && CALL (impl, (CHAR *) (p + align), len - 1) != len - 1)
159 error (0, 0, "Iteration %zd (limited) - wrong result in function %s (%zd) %zd != %zd, p %p",
160 n, impl->name, align,
161 CALL (impl, (CHAR *) (p + align), len - 1), len - 1, p);
162 ret = 1;
164 if (CALL (impl, (CHAR *) (p + align), len) != len)
166 error (0, 0, "Iteration %zd (exact) - wrong result in function %s (%zd) %zd != %zd, p %p",
167 n, impl->name, align,
168 CALL (impl, (CHAR *) (p + align), len), len, p);
169 ret = 1;
171 if (CALL (impl, (CHAR *) (p + align), len + 1) != len)
173 error (0, 0, "Iteration %zd (long) - wrong result in function %s (%zd) %zd != %zd, p %p",
174 n, impl->name, align,
175 CALL (impl, (CHAR *) (p + align), len + 1), len, p);
176 ret = 1;
182 /* Tests meant to unveil fail on implementation that does not access bytes
183 around the page boundary accordingly. */
184 static void
185 do_page_tests (void)
187 size_t i, exp_len, start_offset, offset;
188 /* Calculate the null character offset. */
189 size_t last_offset = (page_size / sizeof (CHAR)) - 1;
191 CHAR *s = (CHAR *) buf2;
192 MEMSET (s, 65, (last_offset - 1));
193 s[last_offset] = 0;
195 /* Place short strings ending at page boundary. */
196 offset = last_offset;
197 for (i = 0; i < 128; i++)
199 /* Decrease offset to stress several sizes and alignments. */
200 offset--;
201 exp_len = last_offset - offset;
202 FOR_EACH_IMPL (impl, 0)
204 /* Varies maxlen value to cover the cases where it is:
205 - larger than length;
206 - slightly greater than length;
207 - equal to length;
208 - slightly less than length. */
209 do_one_test (impl, (CHAR *) (s + offset), page_size, exp_len);
210 do_one_test (impl, (CHAR *) (s + offset), exp_len + 1, exp_len);
211 do_one_test (impl, (CHAR *) (s + offset), exp_len, exp_len);
212 if (exp_len > 0)
213 do_one_test (impl, (CHAR *) (s + offset), exp_len - 1, exp_len - 1);
217 /* Place long strings ending at page boundary. */
218 start_offset = (last_offset + 1) / 2;
219 for (i = 0; i < 64; ++i)
221 /* Increase offset to stress several alignments. */
222 offset = start_offset + i;
223 if (offset >= (last_offset + 1))
224 break;
225 exp_len = last_offset - offset;
226 FOR_EACH_IMPL (impl, 0)
228 /* Checks only for maxlen much larger than length because smaller
229 values are already covered in do_random_tests function. */
230 do_one_test (impl, (CHAR *) (s + offset), page_size, exp_len);
235 /* Tests meant to unveil fail on implementations that access bytes
236 beyond the maxium length. */
238 static void
239 do_page_2_tests (void)
241 size_t i, exp_len, offset;
242 size_t last_offset = page_size / sizeof (CHAR);
244 CHAR *s = (CHAR *) buf2;
245 MEMSET (s, 65, last_offset);
247 /* Place short strings ending at page boundary without the null
248 byte. */
249 offset = last_offset;
250 for (i = 0; i < 128; i++)
252 /* Decrease offset to stress several sizes and alignments. */
253 offset--;
254 exp_len = last_offset - offset;
255 FOR_EACH_IMPL (impl, 0)
257 /* If an implementation goes beyond EXP_LEN, it will trigger
258 the segfault. */
259 do_one_test (impl, (CHAR *) (s + offset), exp_len, exp_len);
265 test_main (void)
267 size_t i, length, char_per_page;
269 test_init ();
271 printf ("%20s", "");
272 FOR_EACH_IMPL (impl, 0)
273 printf ("\t%s", impl->name);
274 putchar ('\n');
276 for (i = 1; i < 8; ++i)
278 do_test (0, i, i - 1, MIDDLE_CHAR);
279 do_test (0, i, i, MIDDLE_CHAR);
280 do_test (0, i, i + 1, MIDDLE_CHAR);
283 for (i = 1; i < 8; ++i)
285 do_test (i, i, i - 1, MIDDLE_CHAR);
286 do_test (i, i, i, MIDDLE_CHAR);
287 do_test (i, i, i + 1, MIDDLE_CHAR);
290 for (i = 2; i <= 10; ++i)
292 do_test (0, 1 << i, 5000, MIDDLE_CHAR);
293 do_test (1, 1 << i, 5000, MIDDLE_CHAR);
296 for (i = 1; i < 8; ++i)
297 do_test (0, i, 5000, BIG_CHAR);
299 for (i = 1; i < 8; ++i)
300 do_test (i, i, 5000, BIG_CHAR);
302 for (i = 2; i <= 10; ++i)
304 do_test (0, 1 << i, 5000, BIG_CHAR);
305 do_test (1, 1 << i, 5000, BIG_CHAR);
308 char_per_page = getpagesize () / sizeof (CHAR);
310 for (i = 0; i <= 127; i++)
311 for (length = i; length <= 512; length++)
313 do_test (i, length, 512, BIG_CHAR);
314 do_test (char_per_page - i, length, 512, BIG_CHAR);
317 do_random_tests ();
318 do_page_tests ();
319 do_page_2_tests ();
320 do_overflow_tests ();
321 return ret;
324 #include <support/test-driver.c>