time: Use 64 bit time on tzfile
[glibc.git] / string / test-memchr.c
blobab206e05db3dfe42c9d8b46ba7d89d9b45f7a820
1 /* Test memchr 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 "memchr"
22 #else
23 # define TEST_NAME "wmemchr"
24 #endif /* WIDE */
26 #include "test-string.h"
27 #include <stdint.h>
29 #ifndef WIDE
30 # define MEMCHR memchr
31 # define CHAR char
32 # define UCHAR unsigned char
33 # define SIMPLE_MEMCHR simple_memchr
34 # define BIG_CHAR CHAR_MAX
35 # define SMALL_CHAR 127
36 #else
37 # include <wchar.h>
38 # define MEMCHR wmemchr
39 # define CHAR wchar_t
40 # define UCHAR wchar_t
41 # define SIMPLE_MEMCHR simple_wmemchr
42 # define BIG_CHAR WCHAR_MAX
43 # define SMALL_CHAR 1273
44 #endif /* WIDE */
46 typedef CHAR *(*proto_t) (const CHAR *, int, size_t);
48 IMPL (MEMCHR, 1)
50 /* Naive implementation to verify results. */
51 CHAR *
52 SIMPLE_MEMCHR (const CHAR *s, int c, size_t n)
54 while (n--)
55 if (*s++ == (CHAR) c)
56 return (CHAR *) s - 1;
57 return NULL;
60 static void
61 do_one_test (impl_t *impl, const CHAR *s, int c, size_t n, CHAR *exp_res)
63 CHAR *res = CALL (impl, s, c, n);
64 if (res != exp_res)
66 error (0, 0, "Wrong result in function %s (%p, %d, %zu) -> %p != %p",
67 impl->name, s, c, n, res, exp_res);
68 ret = 1;
69 return;
73 static void
74 do_test (size_t align, size_t pos, size_t len, size_t n, int seek_char)
76 size_t i;
77 CHAR *result;
79 if ((align + len) * sizeof (CHAR) >= page_size)
80 return;
82 CHAR *buf = (CHAR *) (buf1);
84 for (i = 0; i < len; ++i)
86 buf[align + i] = 1 + 23 * i % SMALL_CHAR;
87 if (buf[align + i] == seek_char)
88 buf[align + i] = seek_char + 1;
90 buf[align + len] = 0;
92 if (pos < MIN(n, len))
94 buf[align + pos] = seek_char;
95 buf[align + len] = -seek_char;
96 result = (CHAR *) (buf + align + pos);
98 else
100 result = NULL;
101 buf[align + len] = seek_char;
104 FOR_EACH_IMPL (impl, 0)
105 do_one_test (impl, (CHAR *) (buf + align), seek_char, n, result);
108 static void
109 do_overflow_tests (void)
111 size_t i, j, len;
112 const size_t one = 1;
113 uintptr_t buf_addr = (uintptr_t) buf1;
115 for (i = 0; i < 750; ++i)
117 do_test (0, i, 751, SIZE_MAX - i, BIG_CHAR);
118 do_test (0, i, 751, i - buf_addr, BIG_CHAR);
119 do_test (0, i, 751, -buf_addr - i, BIG_CHAR);
120 do_test (0, i, 751, SIZE_MAX - buf_addr - i, BIG_CHAR);
121 do_test (0, i, 751, SIZE_MAX - buf_addr + i, BIG_CHAR);
123 len = 0;
124 for (j = 8 * sizeof(size_t) - 1; j ; --j)
126 len |= one << j;
127 do_test (0, i, 751, len - i, BIG_CHAR);
128 do_test (0, i, 751, len + i, BIG_CHAR);
129 do_test (0, i, 751, len - buf_addr - i, BIG_CHAR);
130 do_test (0, i, 751, len - buf_addr + i, BIG_CHAR);
132 do_test (0, i, 751, ~len - i, BIG_CHAR);
133 do_test (0, i, 751, ~len + i, BIG_CHAR);
134 do_test (0, i, 751, ~len - buf_addr - i, BIG_CHAR);
135 do_test (0, i, 751, ~len - buf_addr + i, BIG_CHAR);
140 static void
141 do_random_tests (void)
143 size_t i, j, n, align, pos, len;
144 int seek_char;
145 CHAR *result;
146 UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
148 for (n = 0; n < ITERATIONS; n++)
150 align = random () & 15;
151 pos = random () & 511;
152 if (pos + align >= 512)
153 pos = 511 - align - (random () & 7);
154 len = random () & 511;
155 if (pos >= len)
156 len = pos + (random () & 7);
157 if (len + align >= 512)
158 len = 512 - align - (random () & 7);
159 seek_char = random () & BIG_CHAR;
160 j = len + align + 64;
161 if (j > 512)
162 j = 512;
164 for (i = 0; i < j; i++)
166 if (i == pos + align)
167 p[i] = seek_char;
168 else
170 p[i] = random () & BIG_CHAR;
171 if (i < pos + align && p[i] == seek_char)
172 p[i] = seek_char + 13;
176 if (pos < len)
178 size_t r = random ();
179 if ((r & 31) == 0)
180 len = ~(uintptr_t) (p + align) - ((r >> 5) & 31);
181 result = (CHAR *) (p + pos + align);
183 else
184 result = NULL;
186 FOR_EACH_IMPL (impl, 1)
187 if (CALL (impl, (CHAR *) (p + align), seek_char, len) != result)
189 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
190 n, impl->name, align, seek_char, len, pos,
191 CALL (impl, (CHAR *) (p + align), seek_char, len),
192 result, p);
193 ret = 1;
199 test_main (void)
201 size_t i, j;
203 test_init ();
205 printf ("%20s", "");
206 FOR_EACH_IMPL (impl, 0)
207 printf ("\t%s", impl->name);
208 putchar ('\n');
210 for (i = 1; i < 8; ++i)
212 /* Test n == 0. */
213 do_test (i, i, 0, 0, 23);
214 do_test (i, i, 0, 0, 0);
216 do_test (0, 16 << i, 2048, 2048, 23);
217 do_test (i, 64, 256, 256, 23);
218 do_test (0, 16 << i, 2048, 2048, 0);
219 do_test (i, 64, 256, 256, 0);
221 /* Check for large input sizes and for these cases we need to
222 make sure the byte is within the size range (that's why
223 7 << i must be smaller than 2048). */
224 do_test (0, 7 << i, 2048, SIZE_MAX, 23);
225 do_test (0, 2048 - i, 2048, SIZE_MAX, 23);
226 do_test (i, 64, 256, SIZE_MAX, 23);
227 do_test (0, 7 << i, 2048, SIZE_MAX, 0);
228 do_test (0, 2048 - i, 2048, SIZE_MAX, 0);
229 do_test (i, 64, 256, SIZE_MAX, 0);
232 for (i = 1; i < 64; ++i)
234 for (j = 1; j < 64; j++)
236 do_test (0, 64 - j, 64, SIZE_MAX, 23);
237 do_test (i, 64 - j, 64, SIZE_MAX, 23);
241 for (i = 1; i < 32; ++i)
243 do_test (0, i, i + 1, i + 1, 23);
244 do_test (0, i, i + 1, i + 1, 0);
247 /* BZ#21182 - wrong overflow calculation for i686 implementation
248 with address near end of the page. */
249 for (i = 2; i < 16; ++i)
251 /* page_size is in fact getpagesize() * 2. */
252 do_test (page_size / 2 - i, i, i, 1, 0x9B);
253 do_test (page_size / 2 - i, i - 1, i - 1, 1, 0x9B);
254 do_test (page_size / 2 - (i * 4), i + 128, i + 128, i, 0x9B);
257 do_random_tests ();
258 do_overflow_tests ();
259 return ret;
262 #include <support/test-driver.c>