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