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