Update translations.
[glibc.git] / string / test-memchr.c
bloba4617086b3066ea578b1eab0964f61dc1361691e
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);
47 CHAR *SIMPLE_MEMCHR (const CHAR *, int, size_t);
49 IMPL (SIMPLE_MEMCHR, 0)
50 IMPL (MEMCHR, 1)
52 CHAR *
53 SIMPLE_MEMCHR (const CHAR *s, int c, size_t n)
55 while (n--)
56 if (*s++ == (CHAR) c)
57 return (CHAR *) s - 1;
58 return NULL;
61 static void
62 do_one_test (impl_t *impl, const CHAR *s, int c, size_t n, CHAR *exp_res)
64 CHAR *res = CALL (impl, s, c, n);
65 if (res != exp_res)
67 error (0, 0, "Wrong result in function %s (%p, %d, %zu) -> %p != %p",
68 impl->name, s, c, n, res, exp_res);
69 ret = 1;
70 return;
74 static void
75 do_test (size_t align, size_t pos, size_t len, size_t n, int seek_char)
77 size_t i;
78 CHAR *result;
80 if ((align + len) * sizeof (CHAR) >= page_size)
81 return;
83 CHAR *buf = (CHAR *) (buf1);
85 for (i = 0; i < len; ++i)
87 buf[align + i] = 1 + 23 * i % SMALL_CHAR;
88 if (buf[align + i] == seek_char)
89 buf[align + i] = seek_char + 1;
91 buf[align + len] = 0;
93 if (pos < MIN(n, len))
95 buf[align + pos] = seek_char;
96 buf[align + len] = -seek_char;
97 result = (CHAR *) (buf + align + pos);
99 else
101 result = NULL;
102 buf[align + len] = seek_char;
105 FOR_EACH_IMPL (impl, 0)
106 do_one_test (impl, (CHAR *) (buf + align), seek_char, n, result);
109 static void
110 do_overflow_tests (void)
112 size_t i, j, len;
113 const size_t one = 1;
114 uintptr_t buf_addr = (uintptr_t) buf1;
116 for (i = 0; i < 750; ++i)
118 do_test (0, i, 751, SIZE_MAX - i, BIG_CHAR);
119 do_test (0, i, 751, i - buf_addr, BIG_CHAR);
120 do_test (0, i, 751, -buf_addr - i, BIG_CHAR);
121 do_test (0, i, 751, SIZE_MAX - buf_addr - i, BIG_CHAR);
122 do_test (0, i, 751, SIZE_MAX - buf_addr + i, BIG_CHAR);
124 len = 0;
125 for (j = 8 * sizeof(size_t) - 1; j ; --j)
127 len |= one << j;
128 do_test (0, i, 751, len - i, BIG_CHAR);
129 do_test (0, i, 751, len + i, BIG_CHAR);
130 do_test (0, i, 751, len - buf_addr - i, BIG_CHAR);
131 do_test (0, i, 751, len - buf_addr + i, BIG_CHAR);
133 do_test (0, i, 751, ~len - i, BIG_CHAR);
134 do_test (0, i, 751, ~len + i, BIG_CHAR);
135 do_test (0, i, 751, ~len - buf_addr - i, BIG_CHAR);
136 do_test (0, i, 751, ~len - buf_addr + i, BIG_CHAR);
141 static void
142 do_random_tests (void)
144 size_t i, j, n, align, pos, len;
145 int seek_char;
146 CHAR *result;
147 UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
149 for (n = 0; n < ITERATIONS; n++)
151 align = random () & 15;
152 pos = random () & 511;
153 if (pos + align >= 512)
154 pos = 511 - align - (random () & 7);
155 len = random () & 511;
156 if (pos >= len)
157 len = pos + (random () & 7);
158 if (len + align >= 512)
159 len = 512 - align - (random () & 7);
160 seek_char = random () & BIG_CHAR;
161 j = len + align + 64;
162 if (j > 512)
163 j = 512;
165 for (i = 0; i < j; i++)
167 if (i == pos + align)
168 p[i] = seek_char;
169 else
171 p[i] = random () & BIG_CHAR;
172 if (i < pos + align && p[i] == seek_char)
173 p[i] = seek_char + 13;
177 if (pos < len)
179 size_t r = random ();
180 if ((r & 31) == 0)
181 len = ~(uintptr_t) (p + align) - ((r >> 5) & 31);
182 result = (CHAR *) (p + pos + align);
184 else
185 result = NULL;
187 FOR_EACH_IMPL (impl, 1)
188 if (CALL (impl, (CHAR *) (p + align), seek_char, len) != result)
190 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
191 n, impl->name, align, seek_char, len, pos,
192 CALL (impl, (CHAR *) (p + align), seek_char, len),
193 result, p);
194 ret = 1;
200 test_main (void)
202 size_t i, j;
204 test_init ();
206 printf ("%20s", "");
207 FOR_EACH_IMPL (impl, 0)
208 printf ("\t%s", impl->name);
209 putchar ('\n');
211 for (i = 1; i < 8; ++i)
213 /* Test n == 0. */
214 do_test (i, i, 0, 0, 23);
215 do_test (i, i, 0, 0, 0);
217 do_test (0, 16 << i, 2048, 2048, 23);
218 do_test (i, 64, 256, 256, 23);
219 do_test (0, 16 << i, 2048, 2048, 0);
220 do_test (i, 64, 256, 256, 0);
222 /* Check for large input sizes and for these cases we need to
223 make sure the byte is within the size range (that's why
224 7 << i must be smaller than 2048). */
225 do_test (0, 7 << i, 2048, SIZE_MAX, 23);
226 do_test (0, 2048 - i, 2048, SIZE_MAX, 23);
227 do_test (i, 64, 256, SIZE_MAX, 23);
228 do_test (0, 7 << i, 2048, SIZE_MAX, 0);
229 do_test (0, 2048 - i, 2048, SIZE_MAX, 0);
230 do_test (i, 64, 256, SIZE_MAX, 0);
233 for (i = 1; i < 64; ++i)
235 for (j = 1; j < 64; j++)
237 do_test (0, 64 - j, 64, SIZE_MAX, 23);
238 do_test (i, 64 - j, 64, SIZE_MAX, 23);
242 for (i = 1; i < 32; ++i)
244 do_test (0, i, i + 1, i + 1, 23);
245 do_test (0, i, i + 1, i + 1, 0);
248 /* BZ#21182 - wrong overflow calculation for i686 implementation
249 with address near end of the page. */
250 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);
254 do_random_tests ();
255 do_overflow_tests ();
256 return ret;
259 #include <support/test-driver.c>