1 | /* Test and measure strspn functions.
|
---|
2 | Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
|
---|
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
|
---|
17 | License along with the GNU C Library; if not, write to the Free
|
---|
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA. */
|
---|
20 |
|
---|
21 | #define TEST_MAIN
|
---|
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 *);
|
---|
26 | size_t stupid_strspn (const char *, const char *);
|
---|
27 |
|
---|
28 | IMPL (stupid_strspn, 0)
|
---|
29 | IMPL (simple_strspn, 0)
|
---|
30 | IMPL (strspn, 1)
|
---|
31 |
|
---|
32 | size_t
|
---|
33 | simple_strspn (const char *s, const char *acc)
|
---|
34 | {
|
---|
35 | const char *r, *str = s;
|
---|
36 | char c;
|
---|
37 |
|
---|
38 | while ((c = *s++) != '\0')
|
---|
39 | {
|
---|
40 | for (r = acc; *r != '\0'; ++r)
|
---|
41 | if (*r == c)
|
---|
42 | break;
|
---|
43 | if (*r == '\0')
|
---|
44 | return s - str - 1;
|
---|
45 | }
|
---|
46 | return s - str - 1;
|
---|
47 | }
|
---|
48 |
|
---|
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 |
|
---|
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 | }
|
---|
77 |
|
---|
78 | if (HP_TIMING_AVAIL)
|
---|
79 | {
|
---|
80 | hp_timing_t start __attribute ((unused));
|
---|
81 | hp_timing_t stop __attribute ((unused));
|
---|
82 | hp_timing_t best_time = ~ (hp_timing_t) 0;
|
---|
83 | size_t i;
|
---|
84 |
|
---|
85 | for (i = 0; i < 32; ++i)
|
---|
86 | {
|
---|
87 | HP_TIMING_NOW (start);
|
---|
88 | CALL (impl, s, acc);
|
---|
89 | HP_TIMING_NOW (stop);
|
---|
90 | HP_TIMING_BEST (best_time, start, stop);
|
---|
91 | }
|
---|
92 |
|
---|
93 | printf ("\t%zd", (size_t) best_time);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | static void
|
---|
98 | do_test (size_t align, size_t pos, size_t len)
|
---|
99 | {
|
---|
100 | size_t i;
|
---|
101 | char *acc, *s;
|
---|
102 |
|
---|
103 | align &= 7;
|
---|
104 | if (align + pos + 10 >= page_size || len > 240 || ! len)
|
---|
105 | return;
|
---|
106 |
|
---|
107 | acc = buf2 + (random () & 255);
|
---|
108 | s = buf1 + align;
|
---|
109 |
|
---|
110 | for (i = 0; i < len; ++i)
|
---|
111 | {
|
---|
112 | acc[i] = random () & 255;
|
---|
113 | if (!acc[i])
|
---|
114 | acc[i] = random () & 255;
|
---|
115 | if (!acc[i])
|
---|
116 | acc[i] = 1 + (random () & 127);
|
---|
117 | }
|
---|
118 | acc[len] = '\0';
|
---|
119 |
|
---|
120 | for (i = 0; i < pos; ++i)
|
---|
121 | s[i] = acc[random () % len];
|
---|
122 | s[pos] = random () & 255;
|
---|
123 | if (strchr (acc, s[pos]))
|
---|
124 | s[pos] = '\0';
|
---|
125 | else
|
---|
126 | {
|
---|
127 | for (i = pos + 1; i < pos + 10; ++i)
|
---|
128 | s[i] = random () & 255;
|
---|
129 | s[i] = '\0';
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (HP_TIMING_AVAIL)
|
---|
133 | printf ("Length %4zd, alignment %2zd, acc len %2zd:", pos, align, len);
|
---|
134 |
|
---|
135 | FOR_EACH_IMPL (impl, 0)
|
---|
136 | do_one_test (impl, s, acc, pos);
|
---|
137 |
|
---|
138 | if (HP_TIMING_AVAIL)
|
---|
139 | putchar ('\n');
|
---|
140 | }
|
---|
141 |
|
---|
142 | static void
|
---|
143 | do_random_tests (void)
|
---|
144 | {
|
---|
145 | size_t i, j, n, align, pos, alen, len;
|
---|
146 | unsigned char *p = buf1 + page_size - 512;
|
---|
147 | unsigned char *acc;
|
---|
148 |
|
---|
149 | for (n = 0; n < ITERATIONS; n++)
|
---|
150 | {
|
---|
151 | align = random () & 15;
|
---|
152 | if (random () & 1)
|
---|
153 | alen = random () & 63;
|
---|
154 | else
|
---|
155 | alen = random () & 15;
|
---|
156 | if (!alen)
|
---|
157 | pos = 0;
|
---|
158 | else
|
---|
159 | pos = random () & 511;
|
---|
160 | if (pos + align >= 511)
|
---|
161 | pos = 510 - align - (random () & 7);
|
---|
162 | len = random () & 511;
|
---|
163 | if (len + align >= 512)
|
---|
164 | len = 511 - align - (random () & 7);
|
---|
165 | acc = buf2 + page_size - alen - 1 - (random () & 7);
|
---|
166 | for (i = 0; i < alen; ++i)
|
---|
167 | {
|
---|
168 | acc[i] = random () & 255;
|
---|
169 | if (!acc[i])
|
---|
170 | acc[i] = random () & 255;
|
---|
171 | if (!acc[i])
|
---|
172 | acc[i] = 1 + (random () & 127);
|
---|
173 | }
|
---|
174 | acc[i] = '\0';
|
---|
175 | j = (pos > len ? pos : len) + align + 64;
|
---|
176 | if (j > 512)
|
---|
177 | j = 512;
|
---|
178 |
|
---|
179 | for (i = 0; i < j; i++)
|
---|
180 | {
|
---|
181 | if (i == len + align)
|
---|
182 | p[i] = '\0';
|
---|
183 | else if (i == pos + align)
|
---|
184 | {
|
---|
185 | p[i] = random () & 255;
|
---|
186 | if (strchr (acc, p[i]))
|
---|
187 | p[i] = '\0';
|
---|
188 | }
|
---|
189 | else if (i < align || i > pos + align)
|
---|
190 | p[i] = random () & 255;
|
---|
191 | else
|
---|
192 | p[i] = acc [random () % alen];
|
---|
193 | }
|
---|
194 |
|
---|
195 | FOR_EACH_IMPL (impl, 1)
|
---|
196 | if (CALL (impl, p + align, acc) != (pos < len ? pos : len))
|
---|
197 | {
|
---|
198 | error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %zd != %zd",
|
---|
199 | n, impl->name, align, acc, alen, pos, len,
|
---|
200 | CALL (impl, p + align, acc), (pos < len ? pos : len));
|
---|
201 | ret = 1;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | int
|
---|
207 | test_main (void)
|
---|
208 | {
|
---|
209 | size_t i;
|
---|
210 |
|
---|
211 | test_init ();
|
---|
212 |
|
---|
213 | printf ("%32s", "");
|
---|
214 | FOR_EACH_IMPL (impl, 0)
|
---|
215 | printf ("\t%s", impl->name);
|
---|
216 | putchar ('\n');
|
---|
217 |
|
---|
218 | for (i = 0; i < 32; ++i)
|
---|
219 | {
|
---|
220 | do_test (0, 512, i);
|
---|
221 | do_test (i, 512, i);
|
---|
222 | }
|
---|
223 |
|
---|
224 | for (i = 1; i < 8; ++i)
|
---|
225 | {
|
---|
226 | do_test (0, 16 << i, 4);
|
---|
227 | do_test (i, 16 << i, 4);
|
---|
228 | }
|
---|
229 |
|
---|
230 | for (i = 1; i < 8; ++i)
|
---|
231 | do_test (i, 64, 10);
|
---|
232 |
|
---|
233 | for (i = 0; i < 64; ++i)
|
---|
234 | do_test (0, i, 6);
|
---|
235 |
|
---|
236 | do_random_tests ();
|
---|
237 | return ret;
|
---|
238 | }
|
---|
239 |
|
---|
240 | #include "../test-skeleton.c"
|
---|