Add a testcase for BZ #14716
[glibc.git] / string / test-strspn.c
blob743bc0ae40fdda05a54bff8834a4f4156541dbe0
1 /* Test and measure strspn functions.
2 Copyright (C) 1999,2002,2003,2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #define TEST_MAIN
21 #include "test-string.h"
23 typedef size_t (*proto_t) (const char *, const char *);
24 size_t simple_strspn (const char *, const char *);
25 size_t stupid_strspn (const char *, const char *);
27 IMPL (stupid_strspn, 0)
28 IMPL (simple_strspn, 0)
29 IMPL (strspn, 1)
31 size_t
32 simple_strspn (const char *s, const char *acc)
34 const char *r, *str = s;
35 char c;
37 while ((c = *s++) != '\0')
39 for (r = acc; *r != '\0'; ++r)
40 if (*r == c)
41 break;
42 if (*r == '\0')
43 return s - str - 1;
45 return s - str - 1;
48 size_t
49 stupid_strspn (const char *s, const char *acc)
51 size_t ns = strlen (s), nacc = strlen (acc);
52 size_t i, j;
54 for (i = 0; i < ns; ++i)
56 for (j = 0; j < nacc; ++j)
57 if (s[i] == acc[j])
58 break;
59 if (j == nacc)
60 return i;
62 return i;
65 static void
66 do_one_test (impl_t *impl, const char *s, const char *acc, size_t exp_res)
68 size_t res = CALL (impl, s, acc);
69 if (res != exp_res)
71 error (0, 0, "Wrong result in function %s %p %p", impl->name,
72 (void *) res, (void *) exp_res);
73 ret = 1;
74 return;
77 if (HP_TIMING_AVAIL)
79 hp_timing_t start __attribute ((unused));
80 hp_timing_t stop __attribute ((unused));
81 hp_timing_t best_time = ~ (hp_timing_t) 0;
82 size_t i;
84 for (i = 0; i < 32; ++i)
86 HP_TIMING_NOW (start);
87 CALL (impl, s, acc);
88 HP_TIMING_NOW (stop);
89 HP_TIMING_BEST (best_time, start, stop);
92 printf ("\t%zd", (size_t) best_time);
96 static void
97 do_test (size_t align, size_t pos, size_t len)
99 size_t i;
100 char *acc, *s;
102 align &= 7;
103 if (align + pos + 10 >= page_size || len > 240 || ! len)
104 return;
106 acc = (char *) (buf2 + (random () & 255));
107 s = (char *) (buf1 + align);
109 for (i = 0; i < len; ++i)
111 acc[i] = random () & 255;
112 if (!acc[i])
113 acc[i] = random () & 255;
114 if (!acc[i])
115 acc[i] = 1 + (random () & 127);
117 acc[len] = '\0';
119 for (i = 0; i < pos; ++i)
120 s[i] = acc[random () % len];
121 s[pos] = random () & 255;
122 if (strchr (acc, s[pos]))
123 s[pos] = '\0';
124 else
126 for (i = pos + 1; i < pos + 10; ++i)
127 s[i] = random () & 255;
128 s[i] = '\0';
131 if (HP_TIMING_AVAIL)
132 printf ("Length %4zd, alignment %2zd, acc len %2zd:", pos, align, len);
134 FOR_EACH_IMPL (impl, 0)
135 do_one_test (impl, s, acc, pos);
137 if (HP_TIMING_AVAIL)
138 putchar ('\n');
141 static void
142 do_random_tests (void)
144 size_t i, j, n, align, pos, alen, len;
145 unsigned char *p = buf1 + page_size - 512;
146 unsigned char *acc;
148 for (n = 0; n < ITERATIONS; n++)
150 align = random () & 15;
151 if (random () & 1)
152 alen = random () & 63;
153 else
154 alen = random () & 15;
155 if (!alen)
156 pos = 0;
157 else
158 pos = random () & 511;
159 if (pos + align >= 511)
160 pos = 510 - align - (random () & 7);
161 len = random () & 511;
162 if (len + align >= 512)
163 len = 511 - align - (random () & 7);
164 acc = buf2 + page_size - alen - 1 - (random () & 7);
165 for (i = 0; i < alen; ++i)
167 acc[i] = random () & 255;
168 if (!acc[i])
169 acc[i] = random () & 255;
170 if (!acc[i])
171 acc[i] = 1 + (random () & 127);
173 acc[i] = '\0';
174 j = (pos > len ? pos : len) + align + 64;
175 if (j > 512)
176 j = 512;
178 for (i = 0; i < j; i++)
180 if (i == len + align)
181 p[i] = '\0';
182 else if (i == pos + align)
184 p[i] = random () & 255;
185 if (strchr ((char *) acc, p[i]))
186 p[i] = '\0';
188 else if (i < align || i > pos + align)
189 p[i] = random () & 255;
190 else
191 p[i] = acc [random () % alen];
194 FOR_EACH_IMPL (impl, 1)
195 if (CALL (impl, (char *) (p + align),
196 (char *) acc) != (pos < len ? pos : len))
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, (char *) (p + align), (char *) acc),
201 (pos < len ? pos : len));
202 ret = 1;
208 test_main (void)
210 size_t i;
212 test_init ();
214 printf ("%32s", "");
215 FOR_EACH_IMPL (impl, 0)
216 printf ("\t%s", impl->name);
217 putchar ('\n');
219 for (i = 0; i < 32; ++i)
221 do_test (0, 512, i);
222 do_test (i, 512, i);
225 for (i = 1; i < 8; ++i)
227 do_test (0, 16 << i, 4);
228 do_test (i, 16 << i, 4);
231 for (i = 1; i < 8; ++i)
232 do_test (i, 64, 10);
234 for (i = 0; i < 64; ++i)
235 do_test (0, i, 6);
237 do_random_tests ();
238 return ret;
241 #include "../test-skeleton.c"