Use Linux 5.19 in build-many-glibcs.py
[glibc.git] / string / test-strchr.c
blob631e3779913acdede49c3ff555c632a88a92a125
1 /* Test STRCHR 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 # ifdef USE_FOR_STRCHRNUL
22 # define TEST_NAME "strchrnul"
23 # else
24 # define TEST_NAME "strchr"
25 # endif /* !USE_FOR_STRCHRNUL */
26 #else
27 # ifdef USE_FOR_STRCHRNUL
28 # define TEST_NAME "wcschrnul"
29 # else
30 # define TEST_NAME "wcschr"
31 # endif /* !USE_FOR_STRCHRNUL */
32 #endif /* WIDE */
33 #include "test-string.h"
35 #ifndef WIDE
36 # ifdef USE_FOR_STRCHRNUL
37 # define STRCHR strchrnul
38 # define simple_STRCHR simple_STRCHRNUL
39 # else
40 # define STRCHR strchr
41 # endif /* !USE_FOR_STRCHRNUL */
42 # define STRLEN strlen
43 # define CHAR char
44 # define BIG_CHAR CHAR_MAX
45 # define MIDDLE_CHAR 127
46 # define SMALL_CHAR 23
47 # define UCHAR unsigned char
48 # define L(s) s
49 #else
50 # include <wchar.h>
51 # ifdef USE_FOR_STRCHRNUL
52 # define STRCHR wcschrnul
53 # define simple_STRCHR simple_WCSCHRNUL
54 # else
55 # define STRCHR wcschr
56 # endif /* !USE_FOR_STRCHRNUL */
57 # define STRLEN wcslen
58 # define CHAR wchar_t
59 # define BIG_CHAR WCHAR_MAX
60 # define MIDDLE_CHAR 1121
61 # define SMALL_CHAR 851
62 # define UCHAR wchar_t
63 # define L(s) L ## s
64 #endif /* WIDE */
66 #ifdef USE_FOR_STRCHRNUL
67 # define NULLRET(endptr) endptr
68 #else
69 # define NULLRET(endptr) NULL
70 #endif /* !USE_FOR_STRCHRNUL */
73 typedef CHAR *(*proto_t) (const CHAR *, int);
75 /* Naive implementation to verify results. */
76 CHAR *
77 simple_STRCHR (const CHAR *s, int c)
79 size_t n = STRLEN (s) + 1;
81 while (n--)
82 if (*s++ == (CHAR) c)
83 return (CHAR *) s - 1;
84 return NULLRET ((CHAR *) s - 1);
87 IMPL (STRCHR, 1)
89 static int
90 check_result (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
92 CHAR *res = CALL (impl, s, c);
93 if (res != exp_res)
95 error (0, 0, "Wrong result in function %s %#x %p %p", impl->name,
96 c, res, exp_res);
97 ret = 1;
98 return -1;
100 return 0;
103 static void
104 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
106 if (check_result (impl, s, c, exp_res) < 0)
107 return;
110 static void
111 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
112 /* For wcschr: align here means align not in bytes,
113 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
114 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
116 size_t i;
117 CHAR *result;
118 CHAR *buf = (CHAR *) buf1;
119 align &= 127;
120 if ((align + len) * sizeof (CHAR) >= page_size)
121 return;
123 for (i = 0; i < len; ++i)
125 buf[align + i] = 32 + 23 * i % max_char;
126 if (buf[align + i] == seek_char)
127 buf[align + i] = seek_char + 1;
128 else if (buf[align + i] == 0)
129 buf[align + i] = 1;
131 buf[align + len] = 0;
133 if (pos < len)
135 buf[align + pos] = seek_char;
136 result = buf + align + pos;
138 else if (seek_char == 0)
139 result = buf + align + len;
140 else
141 result = NULLRET (buf + align + len);
143 FOR_EACH_IMPL (impl, 0)
144 do_one_test (impl, buf + align, seek_char, result);
147 static void
148 do_random_tests (void)
150 size_t i, j, n, align, pos, len;
151 int seek_char;
152 CHAR *result;
153 UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
155 for (n = 0; n < ITERATIONS; n++)
157 /* For wcschr: align here means align not in bytes, but in wchar_ts,
158 in bytes it will equal to align * (sizeof (wchar_t)). */
159 align = random () & 15;
160 pos = random () & 511;
161 seek_char = random () & 255;
162 if (pos + align >= 511)
163 pos = 510 - align - (random () & 7);
164 /* len for wcschr here isn't in bytes but it's number of wchar_t
165 symbols. */
166 len = random () & 511;
167 if ((pos == len && seek_char)
168 || (pos > len && (random () & 1)))
169 len = pos + 1 + (random () & 7);
170 if (len + align >= 512)
171 len = 511 - align - (random () & 7);
172 if (pos == len && seek_char)
173 len = pos + 1;
174 j = (pos > len ? pos : len) + align + 64;
175 if (j > 512)
176 j = 512;
178 for (i = 0; i < j; i++)
180 if (i == pos + align)
181 p[i] = seek_char;
182 else if (i == len + align)
183 p[i] = 0;
184 else
186 p[i] = random () & 255;
187 if (i < pos + align && p[i] == seek_char)
188 p[i] = seek_char + 13;
189 if (i < len + align && !p[i])
191 p[i] = seek_char - 13;
192 if (!p[i])
193 p[i] = 140;
198 if (pos <= len)
199 result = (CHAR *) (p + pos + align);
200 else if (seek_char == 0)
201 result = (CHAR *) (p + len + align);
202 else
203 result = NULLRET ((CHAR *) (p + len + align));
205 FOR_EACH_IMPL (impl, 1)
206 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
208 error (0, 0, "Iteration %zd - wrong result in function \
209 %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
210 n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
211 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
212 ret = 1;
217 static void
218 check1 (void)
220 CHAR s[] __attribute__((aligned(16))) = L ("\xff");
221 CHAR c = L ('\xfe');
222 CHAR *exp_result = simple_STRCHR (s, c);
224 FOR_EACH_IMPL (impl, 0)
225 check_result (impl, s, c, exp_result);
229 test_main (void)
231 size_t i;
233 test_init ();
235 check1 ();
237 printf ("%20s", "");
238 FOR_EACH_IMPL (impl, 0)
239 printf ("\t%s", impl->name);
240 putchar ('\n');
242 for (i = 1; i < 8; ++i)
244 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
245 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
248 for (i = 1; i < 8; ++i)
250 do_test (0, 16 << i, 4096, SMALL_CHAR, MIDDLE_CHAR);
251 do_test (i, 16 << i, 4096, SMALL_CHAR, MIDDLE_CHAR);
254 for (i = 1; i < 8; ++i)
256 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
257 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
260 for (i = 0; i < 8; ++i)
262 do_test (16 * i, 256, 512, SMALL_CHAR, MIDDLE_CHAR);
263 do_test (16 * i, 256, 512, SMALL_CHAR, BIG_CHAR);
266 for (i = 0; i < 32; ++i)
268 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
269 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
272 for (i = 1; i < 8; ++i)
274 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
275 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
278 for (i = 1; i < 8; ++i)
280 do_test (0, 16 << i, 4096, 0, MIDDLE_CHAR);
281 do_test (i, 16 << i, 4096, 0, MIDDLE_CHAR);
284 for (i = 1; i < 8; ++i)
286 do_test (i, 64, 256, 0, MIDDLE_CHAR);
287 do_test (i, 64, 256, 0, BIG_CHAR);
290 for (i = 0; i < 8; ++i)
292 do_test (16 * i, 256, 512, 0, MIDDLE_CHAR);
293 do_test (16 * i, 256, 512, 0, BIG_CHAR);
296 for (i = 0; i < 32; ++i)
298 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
299 do_test (0, i, i + 1, 0, BIG_CHAR);
302 do_random_tests ();
303 return ret;
306 #include <support/test-driver.c>