Add sysdeps/ieee754/soft-fp.
[glibc.git] / string / test-strchr.c
blobde9a274f7bff781cce7f226386e3190194b0c791
1 /* Test STRCHR functions.
2 Copyright (C) 1999-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5 Added wcschr support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, 2011
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 #define TEST_MAIN
22 #ifndef WIDE
23 # ifdef USE_FOR_STRCHRNUL
24 # define TEST_NAME "strchrnul"
25 # else
26 # define TEST_NAME "strchr"
27 # endif /* !USE_FOR_STRCHRNUL */
28 #else
29 # ifdef USE_FOR_STRCHRNUL
30 # define TEST_NAME "wcschrnul"
31 # else
32 # define TEST_NAME "wcschr"
33 # endif /* !USE_FOR_STRCHRNUL */
34 #endif /* WIDE */
35 #include "test-string.h"
37 #ifndef WIDE
38 # ifdef USE_FOR_STRCHRNUL
39 # define STRCHR strchrnul
40 # define stupid_STRCHR stupid_STRCHRNUL
41 # define simple_STRCHR simple_STRCHRNUL
42 # else
43 # define STRCHR strchr
44 # endif /* !USE_FOR_STRCHRNUL */
45 # define STRLEN strlen
46 # define CHAR char
47 # define BIG_CHAR CHAR_MAX
48 # define MIDDLE_CHAR 127
49 # define SMALL_CHAR 23
50 # define UCHAR unsigned char
51 # define L(s) s
52 #else
53 # include <wchar.h>
54 # ifdef USE_FOR_STRCHRNUL
55 # define STRCHR wcschrnul
56 # define stupid_STRCHR stupid_WCSCHRNUL
57 # define simple_STRCHR simple_WCSCHRNUL
58 # else
59 # define STRCHR wcschr
60 # endif /* !USE_FOR_STRCHRNUL */
61 # define STRLEN wcslen
62 # define CHAR wchar_t
63 # define BIG_CHAR WCHAR_MAX
64 # define MIDDLE_CHAR 1121
65 # define SMALL_CHAR 851
66 # define UCHAR wchar_t
67 # define L(s) L ## s
68 #endif /* WIDE */
70 #ifdef USE_FOR_STRCHRNUL
71 # define NULLRET(endptr) endptr
72 #else
73 # define NULLRET(endptr) NULL
74 #endif /* !USE_FOR_STRCHRNUL */
77 typedef CHAR *(*proto_t) (const CHAR *, int);
79 CHAR *
80 simple_STRCHR (const CHAR *s, int c)
82 for (; *s != (CHAR) c; ++s)
83 if (*s == '\0')
84 return NULLRET ((CHAR *) s);
85 return (CHAR *) s;
88 CHAR *
89 stupid_STRCHR (const CHAR *s, int c)
91 size_t n = STRLEN (s) + 1;
93 while (n--)
94 if (*s++ == (CHAR) c)
95 return (CHAR *) s - 1;
96 return NULLRET ((CHAR *) s - 1);
99 IMPL (stupid_STRCHR, 0)
100 IMPL (simple_STRCHR, 0)
101 IMPL (STRCHR, 1)
103 static int
104 check_result (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
106 CHAR *res = CALL (impl, s, c);
107 if (res != exp_res)
109 error (0, 0, "Wrong result in function %s %#x %p %p", impl->name,
110 c, res, exp_res);
111 ret = 1;
112 return -1;
114 return 0;
117 static void
118 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
120 if (check_result (impl, s, c, exp_res) < 0)
121 return;
124 static void
125 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
126 /* For wcschr: align here means align not in bytes,
127 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
128 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
130 size_t i;
131 CHAR *result;
132 CHAR *buf = (CHAR *) buf1;
133 align &= 15;
134 if ((align + len) * sizeof (CHAR) >= page_size)
135 return;
137 for (i = 0; i < len; ++i)
139 buf[align + i] = 32 + 23 * i % max_char;
140 if (buf[align + i] == seek_char)
141 buf[align + i] = seek_char + 1;
142 else if (buf[align + i] == 0)
143 buf[align + i] = 1;
145 buf[align + len] = 0;
147 if (pos < len)
149 buf[align + pos] = seek_char;
150 result = buf + align + pos;
152 else if (seek_char == 0)
153 result = buf + align + len;
154 else
155 result = NULLRET (buf + align + len);
157 FOR_EACH_IMPL (impl, 0)
158 do_one_test (impl, buf + align, seek_char, result);
161 static void
162 do_random_tests (void)
164 size_t i, j, n, align, pos, len;
165 int seek_char;
166 CHAR *result;
167 UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
169 for (n = 0; n < ITERATIONS; n++)
171 /* For wcschr: align here means align not in bytes, but in wchar_ts,
172 in bytes it will equal to align * (sizeof (wchar_t)). */
173 align = random () & 15;
174 pos = random () & 511;
175 seek_char = random () & 255;
176 if (pos + align >= 511)
177 pos = 510 - align - (random () & 7);
178 /* len for wcschr here isn't in bytes but it's number of wchar_t
179 symbols. */
180 len = random () & 511;
181 if ((pos == len && seek_char)
182 || (pos > len && (random () & 1)))
183 len = pos + 1 + (random () & 7);
184 if (len + align >= 512)
185 len = 511 - align - (random () & 7);
186 if (pos == len && seek_char)
187 len = pos + 1;
188 j = (pos > len ? pos : len) + align + 64;
189 if (j > 512)
190 j = 512;
192 for (i = 0; i < j; i++)
194 if (i == pos + align)
195 p[i] = seek_char;
196 else if (i == len + align)
197 p[i] = 0;
198 else
200 p[i] = random () & 255;
201 if (i < pos + align && p[i] == seek_char)
202 p[i] = seek_char + 13;
203 if (i < len + align && !p[i])
205 p[i] = seek_char - 13;
206 if (!p[i])
207 p[i] = 140;
212 if (pos <= len)
213 result = (CHAR *) (p + pos + align);
214 else if (seek_char == 0)
215 result = (CHAR *) (p + len + align);
216 else
217 result = NULLRET ((CHAR *) (p + len + align));
219 FOR_EACH_IMPL (impl, 1)
220 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
222 error (0, 0, "Iteration %zd - wrong result in function \
223 %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
224 n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
225 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
226 ret = 1;
231 static void
232 check1 (void)
234 CHAR s[] __attribute__((aligned(16))) = L ("\xff");
235 CHAR c = L ('\xfe');
236 CHAR *exp_result = stupid_STRCHR (s, c);
238 FOR_EACH_IMPL (impl, 0)
239 check_result (impl, s, c, exp_result);
243 test_main (void)
245 size_t i;
247 test_init ();
249 check1 ();
251 printf ("%20s", "");
252 FOR_EACH_IMPL (impl, 0)
253 printf ("\t%s", impl->name);
254 putchar ('\n');
256 for (i = 1; i < 8; ++i)
258 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
259 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
262 for (i = 1; i < 8; ++i)
264 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
265 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
268 for (i = 0; i < 32; ++i)
270 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
271 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
274 for (i = 1; i < 8; ++i)
276 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
277 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
280 for (i = 1; i < 8; ++i)
282 do_test (i, 64, 256, 0, MIDDLE_CHAR);
283 do_test (i, 64, 256, 0, BIG_CHAR);
286 for (i = 0; i < 32; ++i)
288 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
289 do_test (0, i, i + 1, 0, BIG_CHAR);
292 do_random_tests ();
293 return ret;
296 #include <support/test-driver.c>