1 /* Test and measure STRCHR functions.
2    Copyright (C) 1999-2022 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 #ifdef WIDE
21 # define TEST_NAME "wcsrchr"
22 #else
23 # define TEST_NAME "strrchr"
24 #endif
25 #include "test-string.h"
26 
27 #ifdef WIDE
28 # include <wchar.h>
29 # define SIMPLE_STRRCHR simple_wcsrchr
30 # define STRRCHR wcsrchr
31 # define CHAR wchar_t
32 # define UCHAR wchar_t
33 # define BIG_CHAR WCHAR_MAX
34 # define SMALL_CHAR 1273
35 #else
36 # define SIMPLE_STRRCHR simple_strrchr
37 # define STRRCHR strrchr
38 # define CHAR char
39 # define UCHAR unsigned char
40 # define BIG_CHAR CHAR_MAX
41 # define SMALL_CHAR 127
42 #endif
43 
44 typedef CHAR *(*proto_t) (const CHAR *, int);
45 
46 IMPL (STRRCHR, 1)
47 
48 /* Naive implementation to verify results.  */
49 CHAR *
SIMPLE_STRRCHR(const CHAR * s,int c)50 SIMPLE_STRRCHR (const CHAR *s, int c)
51 {
52   const CHAR *ret = NULL;
53 
54   for (; *s != '\0'; ++s)
55     if (*s == (CHAR) c)
56       ret = s;
57 
58   return (CHAR *) (c == '\0' ? s : ret);
59 }
60 
61 static void
do_one_test(impl_t * impl,const CHAR * s,int c,CHAR * exp_res)62 do_one_test (impl_t *impl, const CHAR *s, int c, CHAR *exp_res)
63 {
64   CHAR *res = CALL (impl, s, c);
65   if (res != exp_res)
66     {
67       error (0, 0, "Wrong result in function %s %p %p", impl->name,
68 	     res, exp_res);
69       ret = 1;
70       return;
71     }
72 }
73 
74 static void
do_test(size_t align,size_t pos,size_t len,int seek_char,int max_char)75 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
76 /* For wcsrchr: align here means align not in bytes,
77    but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
78    len for wcschr here isn't in bytes but it's number of wchar_t symbols.  */
79 {
80   size_t i;
81   CHAR *result;
82   CHAR *buf = (CHAR *) buf1;
83 
84   align &= 7;
85   if ( (align + len) * sizeof (CHAR) >= page_size)
86     return;
87 
88   for (i = 0; i < len; ++i)
89     {
90       buf[align + i] = (random () * random ()) & max_char;
91       if (!buf[align + i])
92 	buf[align + i] = (random () * random ()) & max_char;
93       if (!buf[align + i])
94 	buf[align + i] = 1;
95       if ((i > pos || pos >= len) && buf[align + i] == seek_char)
96 	buf[align + i] = seek_char + 10 + (random () & 15);
97     }
98   buf[align + len] = 0;
99 
100   if (pos < len)
101     {
102       buf[align + pos] = seek_char;
103       result = (CHAR *) (buf + align + pos);
104     }
105   else if (seek_char == 0)
106     result = (CHAR *) (buf + align + len);
107   else
108     result = NULL;
109 
110   FOR_EACH_IMPL (impl, 0)
111     do_one_test (impl, (CHAR *) (buf + align), seek_char, result);
112 }
113 
114 static void
do_random_tests(void)115 do_random_tests (void)
116 {
117   size_t i, j, n, align, pos, len;
118   int seek_char;
119   CHAR *result;
120   UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
121 
122   for (n = 0; n < ITERATIONS; n++)
123     {
124       align = random () & (63 / sizeof (CHAR));
125       /* For wcsrchr: align here means align not in bytes, but in wchar_ts,
126 	 in bytes it will equal to align * (sizeof (wchar_t)).
127 	 For strrchr we need to check all alignments from 0 to 63 since
128 	 some assembly implementations have separate prolog for alignments
129 	 more 48. */
130       pos = random () & 511;
131       if (pos + align >= 511)
132 	pos = 510 - align - (random () & 7);
133       len = random () & 511;
134       /* len for wcschr here isn't in bytes but it's number of wchar_t
135 	 symbols.  */
136       if (pos >= len)
137 	len = pos + (random () & 7);
138       if (len + align >= 512)
139 	len = 511 - align - (random () & 7);
140       seek_char = random () & 255;
141       if (seek_char && pos == len)
142 	{
143 	  if (pos)
144 	    --pos;
145 	  else
146 	    ++len;
147 	}
148       j = len + align + 64;
149       if (j > 512)
150 	j = 512;
151 
152       for (i = 0; i < j; i++)
153 	{
154 	  if (i == pos + align)
155 	    p[i] = seek_char;
156 	  else if (i == len + align)
157 	    p[i] = 0;
158 	  else
159 	    {
160 	      p[i] = random () & 255;
161 	      if (((i > pos + align && i < len + align) || pos > len)
162 		  && p[i] == seek_char)
163 		p[i] = seek_char + 13;
164 	      if (i < len + align && !p[i])
165 		{
166 		  p[i] = seek_char - 13;
167 		  if (!p[i])
168 		    p[i] = 140;
169 		}
170 	    }
171 	}
172 
173       if (pos <= len)
174 	result = (CHAR *) (p + pos + align);
175       else if (seek_char == 0)
176 	result = (CHAR *) (p + len + align);
177       else
178 	result = NULL;
179 
180       FOR_EACH_IMPL (impl, 1)
181 	if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
182 	  {
183 	    error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
184 		   n, impl->name, align, seek_char, len, pos,
185 		   CALL (impl, (CHAR *) (p + align), seek_char), result, p);
186 	    ret = 1;
187 	  }
188     }
189 }
190 
191 int
test_main(void)192 test_main (void)
193 {
194   size_t i;
195 
196   test_init ();
197 
198   printf ("%20s", "");
199   FOR_EACH_IMPL (impl, 0)
200     printf ("\t%s", impl->name);
201   putchar ('\n');
202 
203   for (i = 1; i < 8; ++i)
204     {
205       do_test (0, 16 << i, 2048, 23, SMALL_CHAR);
206       do_test (i, 16 << i, 2048, 23, SMALL_CHAR);
207     }
208 
209   for (i = 1; i < 8; ++i)
210     {
211       do_test (i, 64, 256, 23, SMALL_CHAR);
212       do_test (i, 64, 256, 23, BIG_CHAR);
213     }
214 
215   for (i = 0; i < 32; ++i)
216     {
217       do_test (0, i, i + 1, 23, SMALL_CHAR);
218       do_test (0, i, i + 1, 23, BIG_CHAR);
219     }
220 
221   for (i = 1; i < 8; ++i)
222     {
223       do_test (0, 16 << i, 2048, 0, SMALL_CHAR);
224       do_test (i, 16 << i, 2048, 0, SMALL_CHAR);
225     }
226 
227   for (i = 1; i < 8; ++i)
228     {
229       do_test (i, 64, 256, 0, SMALL_CHAR);
230       do_test (i, 64, 256, 0, BIG_CHAR);
231     }
232 
233   for (i = 0; i < 32; ++i)
234     {
235       do_test (0, i, i + 1, 0, SMALL_CHAR);
236       do_test (0, i, i + 1, 0, BIG_CHAR);
237     }
238 
239   do_random_tests ();
240   return ret;
241 }
242 
243 #include <support/test-driver.c>
244