1 | /* Test and measure strpbrk 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 | #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 | # define STUPID_STRPBRK stupid_strpbrk |
51 | # else |
52 | # include <wchar.h> |
53 | # define STRPBRK wcspbrk |
54 | # define SIMPLE_STRPBRK simple_wcspbrk |
55 | # define STUPID_STRPBRK stupid_wcspbrk |
56 | # endif /* WIDE */ |
57 | |
58 | typedef CHAR *(*proto_t) (const CHAR *, const CHAR *); |
59 | CHAR *SIMPLE_STRPBRK (const CHAR *, const CHAR *); |
60 | CHAR *STUPID_STRPBRK (const CHAR *, const CHAR *); |
61 | |
62 | IMPL (STUPID_STRPBRK, 0) |
63 | IMPL (SIMPLE_STRPBRK, 0) |
64 | IMPL (STRPBRK, 1) |
65 | |
66 | CHAR * |
67 | SIMPLE_STRPBRK (const CHAR *s, const CHAR *rej) |
68 | { |
69 | const CHAR *r; |
70 | CHAR c; |
71 | |
72 | while ((c = *s++) != '\0') |
73 | for (r = rej; *r != '\0'; ++r) |
74 | if (*r == c) |
75 | return (CHAR *) s - 1; |
76 | return NULL; |
77 | } |
78 | |
79 | CHAR * |
80 | STUPID_STRPBRK (const CHAR *s, const CHAR *rej) |
81 | { |
82 | size_t ns = STRLEN (s), nrej = STRLEN (rej); |
83 | size_t i, j; |
84 | |
85 | for (i = 0; i < ns; ++i) |
86 | for (j = 0; j < nrej; ++j) |
87 | if (s[i] == rej[j]) |
88 | return (CHAR *) s + i; |
89 | return NULL; |
90 | } |
91 | #endif /* !STRPBRK_RESULT */ |
92 | |
93 | static void |
94 | do_one_test (impl_t *impl, const CHAR *s, const CHAR *rej, RES_TYPE exp_res) |
95 | { |
96 | RES_TYPE res = CALL (impl, s, rej); |
97 | if (res != exp_res) |
98 | { |
99 | error (status: 0, errnum: 0, format: "Wrong result in function %s %p %p" , impl->name, |
100 | (void *) res, (void *) exp_res); |
101 | ret = 1; |
102 | return; |
103 | } |
104 | } |
105 | |
106 | static void |
107 | do_test (size_t align, size_t pos, size_t len) |
108 | { |
109 | size_t i; |
110 | int c; |
111 | RES_TYPE result; |
112 | CHAR *rej, *s; |
113 | |
114 | align &= 7; |
115 | if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240) |
116 | return; |
117 | |
118 | rej = (CHAR *) (buf2) + (random () & 255); |
119 | s = (CHAR *) (buf1) + align; |
120 | |
121 | for (i = 0; i < len; ++i) |
122 | { |
123 | rej[i] = random () & BIG_CHAR; |
124 | if (!rej[i]) |
125 | rej[i] = random () & BIG_CHAR; |
126 | if (!rej[i]) |
127 | rej[i] = 1 + (random () & SMALL_CHAR); |
128 | } |
129 | rej[len] = '\0'; |
130 | for (c = 1; c <= BIG_CHAR; ++c) |
131 | if (STRCHR (rej, c) == NULL) |
132 | break; |
133 | |
134 | for (i = 0; i < pos; ++i) |
135 | { |
136 | s[i] = random () & BIG_CHAR; |
137 | if (STRCHR (rej, s[i])) |
138 | { |
139 | s[i] = random () & BIG_CHAR; |
140 | if (STRCHR (rej, s[i])) |
141 | s[i] = c; |
142 | } |
143 | } |
144 | s[pos] = rej[random () % (len + 1)]; |
145 | if (s[pos]) |
146 | { |
147 | for (i = pos + 1; i < pos + 10; ++i) |
148 | s[i] = random () & BIG_CHAR; |
149 | s[i] = '\0'; |
150 | } |
151 | result = STRPBRK_RESULT (s, pos); |
152 | |
153 | FOR_EACH_IMPL (impl, 0) |
154 | do_one_test (impl, s, rej, exp_res: result); |
155 | } |
156 | |
157 | static void |
158 | do_random_tests (void) |
159 | { |
160 | size_t i, j, n, align, pos, len, rlen; |
161 | RES_TYPE result; |
162 | int c; |
163 | UCHAR *p = (UCHAR *) (buf1 + page_size) - 512; |
164 | UCHAR *rej; |
165 | |
166 | for (n = 0; n < ITERATIONS; n++) |
167 | { |
168 | align = random () & 15; |
169 | pos = random () & 511; |
170 | if (pos + align >= 511) |
171 | pos = 510 - align - (random () & 7); |
172 | len = random () & 511; |
173 | if (pos >= len && (random () & 1)) |
174 | len = pos + 1 + (random () & 7); |
175 | if (len + align >= 512) |
176 | len = 511 - align - (random () & 7); |
177 | if (random () & 1) |
178 | rlen = random () & 63; |
179 | else |
180 | rlen = random () & 15; |
181 | rej = (UCHAR *) (buf2 + page_size) - rlen - 1 - (random () & 7); |
182 | for (i = 0; i < rlen; ++i) |
183 | { |
184 | rej[i] = random () & BIG_CHAR; |
185 | if (!rej[i]) |
186 | rej[i] = random () & BIG_CHAR; |
187 | if (!rej[i]) |
188 | rej[i] = 1 + (random () & SMALL_CHAR); |
189 | } |
190 | rej[i] = '\0'; |
191 | for (c = 1; c <= BIG_CHAR; ++c) |
192 | if (STRCHR ((CHAR *) rej, c) == NULL) |
193 | break; |
194 | j = (pos > len ? pos : len) + align + 64; |
195 | if (j > 512) |
196 | j = 512; |
197 | |
198 | for (i = 0; i < j; i++) |
199 | { |
200 | if (i == len + align) |
201 | p[i] = '\0'; |
202 | else if (i == pos + align) |
203 | p[i] = rej[random () % (rlen + 1)]; |
204 | else if (i < align || i > pos + align) |
205 | p[i] = random () & BIG_CHAR; |
206 | else |
207 | { |
208 | p[i] = random () & BIG_CHAR; |
209 | if (STRCHR ((CHAR *) rej, p[i])) |
210 | { |
211 | p[i] = random () & BIG_CHAR; |
212 | if (STRCHR ((CHAR *) rej, p[i])) |
213 | p[i] = c; |
214 | } |
215 | } |
216 | } |
217 | |
218 | result = STRPBRK_RESULT ((CHAR *) (p + align), pos < len ? pos : len); |
219 | |
220 | FOR_EACH_IMPL (impl, 1) |
221 | if (CALL (impl, (CHAR *) (p + align), (CHAR *) rej) != result) |
222 | { |
223 | error (status: 0, errnum: 0, format: "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %p != %p" , |
224 | n, impl->name, align, rej, rlen, pos, len, |
225 | (void *) CALL (impl, (CHAR *) (p + align), (CHAR *) rej), |
226 | (void *) result); |
227 | ret = 1; |
228 | } |
229 | } |
230 | } |
231 | |
232 | int |
233 | test_main (void) |
234 | { |
235 | size_t i; |
236 | |
237 | test_init (); |
238 | |
239 | printf (format: "%32s" , "" ); |
240 | FOR_EACH_IMPL (impl, 0) |
241 | printf (format: "\t%s" , impl->name); |
242 | putchar (c: '\n'); |
243 | |
244 | for (i = 0; i < 32; ++i) |
245 | { |
246 | do_test (align: 0, pos: 512, len: i); |
247 | do_test (align: i, pos: 512, len: i); |
248 | } |
249 | |
250 | for (i = 1; i < 8; ++i) |
251 | { |
252 | do_test (align: 0, pos: 16 << i, len: 4); |
253 | do_test (align: i, pos: 16 << i, len: 4); |
254 | } |
255 | |
256 | for (i = 1; i < 8; ++i) |
257 | do_test (align: i, pos: 64, len: 10); |
258 | |
259 | for (i = 0; i < 64; ++i) |
260 | do_test (align: 0, pos: i, len: 6); |
261 | |
262 | do_random_tests (); |
263 | return ret; |
264 | } |
265 | |
266 | #include <support/test-driver.c> |
267 | |