1 /* Test STRCHR functions.
2    Copyright (C) 1999-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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/>.  */
18 
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"
34 
35 #ifndef WIDE
36 # ifdef USE_FOR_STRCHRNUL
37 #  define STRCHR strchrnul
38 #  define stupid_STRCHR stupid_STRCHRNUL
39 #  define simple_STRCHR simple_STRCHRNUL
40 # else
41 #  define STRCHR strchr
42 # endif /* !USE_FOR_STRCHRNUL */
43 # define STRLEN strlen
44 # define CHAR char
45 # define BIG_CHAR CHAR_MAX
46 # define MIDDLE_CHAR 127
47 # define SMALL_CHAR 23
48 # define UCHAR unsigned char
49 # define L(s) s
50 #else
51 # include <wchar.h>
52 # ifdef USE_FOR_STRCHRNUL
53 #  define STRCHR wcschrnul
54 #  define stupid_STRCHR stupid_WCSCHRNUL
55 #  define simple_STRCHR simple_WCSCHRNUL
56 # else
57 #  define STRCHR wcschr
58 # endif /* !USE_FOR_STRCHRNUL */
59 # define STRLEN wcslen
60 # define CHAR wchar_t
61 # define BIG_CHAR WCHAR_MAX
62 # define MIDDLE_CHAR 1121
63 # define SMALL_CHAR 851
64 # define UCHAR wchar_t
65 # define L(s) L ## s
66 #endif /* WIDE */
67 
68 #ifdef USE_FOR_STRCHRNUL
69 # define NULLRET(endptr) endptr
70 #else
71 # define NULLRET(endptr) NULL
72 #endif /* !USE_FOR_STRCHRNUL */
73 
74 
75 typedef CHAR *(*proto_t) (const CHAR *, int);
76 
77 CHAR *
simple_STRCHR(const CHAR * s,int c)78 simple_STRCHR (const CHAR *s, int c)
79 {
80   for (; *s != (CHAR) c; ++s)
81     if (*s == '\0')
82       return NULLRET ((CHAR *) s);
83   return (CHAR *) s;
84 }
85 
86 CHAR *
stupid_STRCHR(const CHAR * s,int c)87 stupid_STRCHR (const CHAR *s, int c)
88 {
89   size_t n = STRLEN (s) + 1;
90 
91   while (n--)
92     if (*s++ == (CHAR) c)
93       return (CHAR *) s - 1;
94   return NULLRET ((CHAR *) s - 1);
95 }
96 
97 IMPL (stupid_STRCHR, 0)
98 IMPL (simple_STRCHR, 0)
99 IMPL (STRCHR, 1)
100 
101 static int
check_result(impl_t * impl,const CHAR * s,int c,const CHAR * exp_res)102 check_result (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
103 {
104   CHAR *res = CALL (impl, s, c);
105   if (res != exp_res)
106     {
107       error (0, 0, "Wrong result in function %s %#x %p %p", impl->name,
108 	     c, res, exp_res);
109       ret = 1;
110       return -1;
111     }
112   return 0;
113 }
114 
115 static void
do_one_test(impl_t * impl,const CHAR * s,int c,const CHAR * exp_res)116 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
117 {
118   if (check_result (impl, s, c, exp_res) < 0)
119     return;
120 }
121 
122 static void
do_test(size_t align,size_t pos,size_t len,int seek_char,int max_char)123 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
124 /* For wcschr: align here means align not in bytes,
125    but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
126    len for wcschr here isn't in bytes but it's number of wchar_t symbols.  */
127 {
128   size_t i;
129   CHAR *result;
130   CHAR *buf = (CHAR *) buf1;
131   align &= 127;
132   if ((align + len) * sizeof (CHAR) >= page_size)
133     return;
134 
135   for (i = 0; i < len; ++i)
136     {
137       buf[align + i] = 32 + 23 * i % max_char;
138       if (buf[align + i] == seek_char)
139 	buf[align + i] = seek_char + 1;
140       else if (buf[align + i] == 0)
141 	buf[align + i] = 1;
142     }
143   buf[align + len] = 0;
144 
145   if (pos < len)
146     {
147       buf[align + pos] = seek_char;
148       result = buf + align + pos;
149     }
150   else if (seek_char == 0)
151     result = buf + align + len;
152   else
153     result = NULLRET (buf + align + len);
154 
155   FOR_EACH_IMPL (impl, 0)
156     do_one_test (impl, buf + align, seek_char, result);
157 }
158 
159 static void
do_random_tests(void)160 do_random_tests (void)
161 {
162   size_t i, j, n, align, pos, len;
163   int seek_char;
164   CHAR *result;
165   UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
166 
167   for (n = 0; n < ITERATIONS; n++)
168     {
169       /* For wcschr: align here means align not in bytes, but in wchar_ts,
170 	 in bytes it will equal to align * (sizeof (wchar_t)).  */
171       align = random () & 15;
172       pos = random () & 511;
173       seek_char = random () & 255;
174       if (pos + align >= 511)
175 	pos = 510 - align - (random () & 7);
176       /* len for wcschr here isn't in bytes but it's number of wchar_t
177 	 symbols.  */
178       len = random () & 511;
179       if ((pos == len && seek_char)
180 	  || (pos > len && (random () & 1)))
181 	len = pos + 1 + (random () & 7);
182       if (len + align >= 512)
183 	len = 511 - align - (random () & 7);
184       if (pos == len && seek_char)
185 	len = pos + 1;
186       j = (pos > len ? pos : len) + align + 64;
187       if (j > 512)
188 	j = 512;
189 
190       for (i = 0; i < j; i++)
191 	{
192 	  if (i == pos + align)
193 	    p[i] = seek_char;
194 	  else if (i == len + align)
195 	    p[i] = 0;
196 	  else
197 	    {
198 	      p[i] = random () & 255;
199 	      if (i < pos + align && p[i] == seek_char)
200 		p[i] = seek_char + 13;
201 	      if (i < len + align && !p[i])
202 		{
203 		  p[i] = seek_char - 13;
204 		  if (!p[i])
205 		    p[i] = 140;
206 		}
207 	    }
208 	}
209 
210       if (pos <= len)
211 	result = (CHAR *) (p + pos + align);
212       else if (seek_char == 0)
213 	result = (CHAR *) (p + len + align);
214       else
215 	result = NULLRET ((CHAR *) (p + len + align));
216 
217       FOR_EACH_IMPL (impl, 1)
218 	if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
219 	  {
220 	    error (0, 0, "Iteration %zd - wrong result in function \
221 		   %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
222 		   n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
223 		   CALL (impl, (CHAR *) (p + align), seek_char), result, p);
224 	    ret = 1;
225 	  }
226     }
227 }
228 
229 static void
check1(void)230 check1 (void)
231 {
232   CHAR s[] __attribute__((aligned(16))) = L ("\xff");
233   CHAR c = L ('\xfe');
234   CHAR *exp_result = stupid_STRCHR (s, c);
235 
236   FOR_EACH_IMPL (impl, 0)
237     check_result (impl, s, c, exp_result);
238 }
239 
240 int
test_main(void)241 test_main (void)
242 {
243   size_t i;
244 
245   test_init ();
246 
247   check1 ();
248 
249   printf ("%20s", "");
250   FOR_EACH_IMPL (impl, 0)
251     printf ("\t%s", impl->name);
252   putchar ('\n');
253 
254   for (i = 1; i < 8; ++i)
255     {
256       do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
257       do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
258     }
259 
260   for (i = 1; i < 8; ++i)
261     {
262       do_test (0, 16 << i, 4096, SMALL_CHAR, MIDDLE_CHAR);
263       do_test (i, 16 << i, 4096, SMALL_CHAR, MIDDLE_CHAR);
264     }
265 
266   for (i = 1; i < 8; ++i)
267     {
268       do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
269       do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
270     }
271 
272   for (i = 0; i < 8; ++i)
273     {
274       do_test (16 * i, 256, 512, SMALL_CHAR, MIDDLE_CHAR);
275       do_test (16 * i, 256, 512, SMALL_CHAR, BIG_CHAR);
276     }
277 
278   for (i = 0; i < 32; ++i)
279     {
280       do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
281       do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
282     }
283 
284   for (i = 1; i < 8; ++i)
285     {
286       do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
287       do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
288     }
289 
290   for (i = 1; i < 8; ++i)
291     {
292       do_test (0, 16 << i, 4096, 0, MIDDLE_CHAR);
293       do_test (i, 16 << i, 4096, 0, MIDDLE_CHAR);
294     }
295 
296   for (i = 1; i < 8; ++i)
297     {
298       do_test (i, 64, 256, 0, MIDDLE_CHAR);
299       do_test (i, 64, 256, 0, BIG_CHAR);
300     }
301 
302   for (i = 0; i < 8; ++i)
303     {
304       do_test (16 * i, 256, 512, 0, MIDDLE_CHAR);
305       do_test (16 * i, 256, 512, 0, BIG_CHAR);
306     }
307 
308   for (i = 0; i < 32; ++i)
309     {
310       do_test (0, i, i + 1, 0, MIDDLE_CHAR);
311       do_test (0, i, i + 1, 0, BIG_CHAR);
312     }
313 
314   do_random_tests ();
315   return ret;
316 }
317 
318 #include <support/test-driver.c>
319