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