Add a testase for BZ #14602
[glibc.git] / string / test-strpbrk.c
blob263a7fdfb65e56280b5b7cf6d1c36c89774faa86
1 /* Test and measure strpbrk 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 #ifndef STRPBRK_RESULT
21 # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL)
22 # define RES_TYPE char *
23 # define TEST_MAIN
24 # include "test-string.h"
26 typedef char *(*proto_t) (const char *, const char *);
27 char *simple_strpbrk (const char *, const char *);
28 char *stupid_strpbrk (const char *, const char *);
30 IMPL (stupid_strpbrk, 0)
31 IMPL (simple_strpbrk, 0)
32 IMPL (strpbrk, 1)
34 char *
35 simple_strpbrk (const char *s, const char *rej)
37 const char *r;
38 char c;
40 while ((c = *s++) != '\0')
41 for (r = rej; *r != '\0'; ++r)
42 if (*r == c)
43 return (char *) s - 1;
44 return NULL;
47 char *
48 stupid_strpbrk (const char *s, const char *rej)
50 size_t ns = strlen (s), nrej = strlen (rej);
51 size_t i, j;
53 for (i = 0; i < ns; ++i)
54 for (j = 0; j < nrej; ++j)
55 if (s[i] == rej[j])
56 return (char *) s + i;
57 return NULL;
59 #endif
61 static void
62 do_one_test (impl_t *impl, const char *s, const char *rej, RES_TYPE exp_res)
64 RES_TYPE res = CALL (impl, s, rej);
65 if (res != exp_res)
67 error (0, 0, "Wrong result in function %s %p %p", impl->name,
68 (void *) res, (void *) exp_res);
69 ret = 1;
70 return;
73 if (HP_TIMING_AVAIL)
75 hp_timing_t start __attribute ((unused));
76 hp_timing_t stop __attribute ((unused));
77 hp_timing_t best_time = ~ (hp_timing_t) 0;
78 size_t i;
80 for (i = 0; i < 32; ++i)
82 HP_TIMING_NOW (start);
83 CALL (impl, s, rej);
84 HP_TIMING_NOW (stop);
85 HP_TIMING_BEST (best_time, start, stop);
88 printf ("\t%zd", (size_t) best_time);
92 static void
93 do_test (size_t align, size_t pos, size_t len)
95 size_t i;
96 int c;
97 RES_TYPE result;
98 char *rej, *s;
100 align &= 7;
101 if (align + pos + 10 >= page_size || len > 240)
102 return;
104 rej = (char *) (buf2 + (random () & 255));
105 s = (char *) (buf1 + align);
107 for (i = 0; i < len; ++i)
109 rej[i] = random () & 255;
110 if (!rej[i])
111 rej[i] = random () & 255;
112 if (!rej[i])
113 rej[i] = 1 + (random () & 127);
115 rej[len] = '\0';
116 for (c = 1; c <= 255; ++c)
117 if (strchr (rej, c) == NULL)
118 break;
120 for (i = 0; i < pos; ++i)
122 s[i] = random () & 255;
123 if (strchr (rej, s[i]))
125 s[i] = random () & 255;
126 if (strchr (rej, s[i]))
127 s[i] = c;
130 s[pos] = rej[random () % (len + 1)];
131 if (s[pos])
133 for (i = pos + 1; i < pos + 10; ++i)
134 s[i] = random () & 255;
135 s[i] = '\0';
137 result = STRPBRK_RESULT (s, pos);
139 if (HP_TIMING_AVAIL)
140 printf ("Length %4zd, alignment %2zd, rej len %2zd:", pos, align, len);
142 FOR_EACH_IMPL (impl, 0)
143 do_one_test (impl, s, rej, result);
145 if (HP_TIMING_AVAIL)
146 putchar ('\n');
149 static void
150 do_random_tests (void)
152 size_t i, j, n, align, pos, len, rlen;
153 RES_TYPE result;
154 int c;
155 unsigned char *p = buf1 + page_size - 512;
156 unsigned char *rej;
158 for (n = 0; n < ITERATIONS; n++)
160 align = random () & 15;
161 pos = random () & 511;
162 if (pos + align >= 511)
163 pos = 510 - align - (random () & 7);
164 len = random () & 511;
165 if (pos >= len && (random () & 1))
166 len = pos + 1 + (random () & 7);
167 if (len + align >= 512)
168 len = 511 - align - (random () & 7);
169 if (random () & 1)
170 rlen = random () & 63;
171 else
172 rlen = random () & 15;
173 rej = buf2 + page_size - rlen - 1 - (random () & 7);
174 for (i = 0; i < rlen; ++i)
176 rej[i] = random () & 255;
177 if (!rej[i])
178 rej[i] = random () & 255;
179 if (!rej[i])
180 rej[i] = 1 + (random () & 127);
182 rej[i] = '\0';
183 for (c = 1; c <= 255; ++c)
184 if (strchr ((char *) rej, c) == NULL)
185 break;
186 j = (pos > len ? pos : len) + align + 64;
187 if (j > 512)
188 j = 512;
190 for (i = 0; i < j; i++)
192 if (i == len + align)
193 p[i] = '\0';
194 else if (i == pos + align)
195 p[i] = rej[random () % (rlen + 1)];
196 else if (i < align || i > pos + align)
197 p[i] = random () & 255;
198 else
200 p[i] = random () & 255;
201 if (strchr ((char *) rej, p[i]))
203 p[i] = random () & 255;
204 if (strchr ((char *) rej, p[i]))
205 p[i] = c;
210 result = STRPBRK_RESULT ((char *) (p + align), pos < len ? pos : len);
212 FOR_EACH_IMPL (impl, 1)
213 if (CALL (impl, (char *) (p + align), (char *) rej) != result)
215 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %p != %p",
216 n, impl->name, align, rej, rlen, pos, len,
217 (void *) CALL (impl, (char *) (p + align), (char *) rej),
218 (void *) result);
219 ret = 1;
225 test_main (void)
227 size_t i;
229 test_init ();
231 printf ("%32s", "");
232 FOR_EACH_IMPL (impl, 0)
233 printf ("\t%s", impl->name);
234 putchar ('\n');
236 for (i = 0; i < 32; ++i)
238 do_test (0, 512, i);
239 do_test (i, 512, i);
242 for (i = 1; i < 8; ++i)
244 do_test (0, 16 << i, 4);
245 do_test (i, 16 << i, 4);
248 for (i = 1; i < 8; ++i)
249 do_test (i, 64, 10);
251 for (i = 0; i < 64; ++i)
252 do_test (0, i, 6);
254 do_random_tests ();
255 return ret;
258 #include "../test-skeleton.c"