]>
Commit | Line | Data |
---|---|---|
58ef9ef7 | 1 | /* Test and measure strspn functions. |
568035b7 | 2 | Copyright (C) 1999-2013 Free Software Foundation, Inc. |
58ef9ef7 RM |
3 | This file is part of the GNU C Library. |
4 | Written by Jakub Jelinek <jakub@redhat.com>, 1999. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
58ef9ef7 RM |
19 | |
20 | #define TEST_MAIN | |
69f07e5f | 21 | #define TEST_NAME "strspn" |
58ef9ef7 RM |
22 | #include "test-string.h" |
23 | ||
24 | typedef size_t (*proto_t) (const char *, const char *); | |
25 | size_t simple_strspn (const char *, const char *); | |
e8c1660f | 26 | size_t stupid_strspn (const char *, const char *); |
58ef9ef7 | 27 | |
e8c1660f | 28 | IMPL (stupid_strspn, 0) |
58ef9ef7 RM |
29 | IMPL (simple_strspn, 0) |
30 | IMPL (strspn, 1) | |
31 | ||
32 | size_t | |
e8c1660f | 33 | simple_strspn (const char *s, const char *acc) |
58ef9ef7 RM |
34 | { |
35 | const char *r, *str = s; | |
36 | char c; | |
37 | ||
38 | while ((c = *s++) != '\0') | |
39 | { | |
e8c1660f | 40 | for (r = acc; *r != '\0'; ++r) |
58ef9ef7 RM |
41 | if (*r == c) |
42 | break; | |
43 | if (*r == '\0') | |
44 | return s - str - 1; | |
45 | } | |
46 | return s - str - 1; | |
47 | } | |
48 | ||
e8c1660f RM |
49 | size_t |
50 | stupid_strspn (const char *s, const char *acc) | |
51 | { | |
52 | size_t ns = strlen (s), nacc = strlen (acc); | |
53 | size_t i, j; | |
54 | ||
55 | for (i = 0; i < ns; ++i) | |
56 | { | |
57 | for (j = 0; j < nacc; ++j) | |
58 | if (s[i] == acc[j]) | |
59 | break; | |
60 | if (j == nacc) | |
61 | return i; | |
62 | } | |
63 | return i; | |
64 | } | |
65 | ||
58ef9ef7 RM |
66 | static void |
67 | do_one_test (impl_t *impl, const char *s, const char *acc, size_t exp_res) | |
68 | { | |
69 | size_t res = CALL (impl, s, acc); | |
70 | if (res != exp_res) | |
71 | { | |
72 | error (0, 0, "Wrong result in function %s %p %p", impl->name, | |
73 | (void *) res, (void *) exp_res); | |
74 | ret = 1; | |
75 | return; | |
76 | } | |
58ef9ef7 RM |
77 | } |
78 | ||
79 | static void | |
80 | do_test (size_t align, size_t pos, size_t len) | |
81 | { | |
82 | size_t i; | |
83 | char *acc, *s; | |
84 | ||
85 | align &= 7; | |
9372c958 | 86 | if (align + pos + 10 >= page_size || len > 240 || ! len) |
58ef9ef7 RM |
87 | return; |
88 | ||
0317eaec RM |
89 | acc = (char *) (buf2 + (random () & 255)); |
90 | s = (char *) (buf1 + align); | |
58ef9ef7 RM |
91 | |
92 | for (i = 0; i < len; ++i) | |
93 | { | |
94 | acc[i] = random () & 255; | |
95 | if (!acc[i]) | |
96 | acc[i] = random () & 255; | |
97 | if (!acc[i]) | |
98 | acc[i] = 1 + (random () & 127); | |
99 | } | |
100 | acc[len] = '\0'; | |
101 | ||
102 | for (i = 0; i < pos; ++i) | |
103 | s[i] = acc[random () % len]; | |
104 | s[pos] = random () & 255; | |
105 | if (strchr (acc, s[pos])) | |
106 | s[pos] = '\0'; | |
9372c958 RM |
107 | else |
108 | { | |
109 | for (i = pos + 1; i < pos + 10; ++i) | |
110 | s[i] = random () & 255; | |
111 | s[i] = '\0'; | |
112 | } | |
58ef9ef7 | 113 | |
58ef9ef7 RM |
114 | FOR_EACH_IMPL (impl, 0) |
115 | do_one_test (impl, s, acc, pos); | |
58ef9ef7 RM |
116 | } |
117 | ||
118 | static void | |
119 | do_random_tests (void) | |
120 | { | |
e8c1660f | 121 | size_t i, j, n, align, pos, alen, len; |
58ef9ef7 RM |
122 | unsigned char *p = buf1 + page_size - 512; |
123 | unsigned char *acc; | |
124 | ||
125 | for (n = 0; n < ITERATIONS; n++) | |
126 | { | |
127 | align = random () & 15; | |
128 | if (random () & 1) | |
e8c1660f | 129 | alen = random () & 63; |
58ef9ef7 | 130 | else |
e8c1660f RM |
131 | alen = random () & 15; |
132 | if (!alen) | |
58ef9ef7 RM |
133 | pos = 0; |
134 | else | |
135 | pos = random () & 511; | |
136 | if (pos + align >= 511) | |
137 | pos = 510 - align - (random () & 7); | |
e8c1660f RM |
138 | len = random () & 511; |
139 | if (len + align >= 512) | |
140 | len = 511 - align - (random () & 7); | |
141 | acc = buf2 + page_size - alen - 1 - (random () & 7); | |
142 | for (i = 0; i < alen; ++i) | |
58ef9ef7 RM |
143 | { |
144 | acc[i] = random () & 255; | |
145 | if (!acc[i]) | |
146 | acc[i] = random () & 255; | |
147 | if (!acc[i]) | |
148 | acc[i] = 1 + (random () & 127); | |
149 | } | |
150 | acc[i] = '\0'; | |
e8c1660f | 151 | j = (pos > len ? pos : len) + align + 64; |
58ef9ef7 RM |
152 | if (j > 512) |
153 | j = 512; | |
154 | ||
155 | for (i = 0; i < j; i++) | |
156 | { | |
e8c1660f RM |
157 | if (i == len + align) |
158 | p[i] = '\0'; | |
159 | else if (i == pos + align) | |
58ef9ef7 | 160 | { |
e8c1660f | 161 | p[i] = random () & 255; |
0317eaec | 162 | if (strchr ((char *) acc, p[i])) |
58ef9ef7 | 163 | p[i] = '\0'; |
58ef9ef7 RM |
164 | } |
165 | else if (i < align || i > pos + align) | |
166 | p[i] = random () & 255; | |
167 | else | |
e8c1660f | 168 | p[i] = acc [random () % alen]; |
58ef9ef7 RM |
169 | } |
170 | ||
171 | FOR_EACH_IMPL (impl, 1) | |
0317eaec RM |
172 | if (CALL (impl, (char *) (p + align), |
173 | (char *) acc) != (pos < len ? pos : len)) | |
58ef9ef7 | 174 | { |
e8c1660f RM |
175 | error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %zd != %zd", |
176 | n, impl->name, align, acc, alen, pos, len, | |
0317eaec RM |
177 | CALL (impl, (char *) (p + align), (char *) acc), |
178 | (pos < len ? pos : len)); | |
58ef9ef7 RM |
179 | ret = 1; |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | int | |
185 | test_main (void) | |
186 | { | |
187 | size_t i; | |
188 | ||
189 | test_init (); | |
190 | ||
191 | printf ("%32s", ""); | |
192 | FOR_EACH_IMPL (impl, 0) | |
193 | printf ("\t%s", impl->name); | |
194 | putchar ('\n'); | |
195 | ||
196 | for (i = 0; i < 32; ++i) | |
197 | { | |
198 | do_test (0, 512, i); | |
199 | do_test (i, 512, i); | |
200 | } | |
201 | ||
202 | for (i = 1; i < 8; ++i) | |
203 | { | |
204 | do_test (0, 16 << i, 4); | |
205 | do_test (i, 16 << i, 4); | |
206 | } | |
207 | ||
208 | for (i = 1; i < 8; ++i) | |
209 | do_test (i, 64, 10); | |
210 | ||
211 | for (i = 0; i < 64; ++i) | |
212 | do_test (0, i, 6); | |
213 | ||
214 | do_random_tests (); | |
215 | return ret; | |
216 | } | |
217 | ||
218 | #include "../test-skeleton.c" |