i386: Regenerate libm-test-ulps for for gcc 7
[glibc.git] / string / test-strpbrk.c
blob4d680ab354ef18866e975a56afe63bb58786aefa
1 /* Test and measure strpbrk functions.
2 Copyright (C) 1999-2017 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 WIDE
21 # define CHAR char
22 # define UCHAR unsigned char
23 # define STRLEN strlen
24 # define STRCHR strchr
25 # define BIG_CHAR CHAR_MAX
26 # define SMALL_CHAR 127
27 #else
28 # include <wchar.h>
29 # define CHAR wchar_t
30 # define UCHAR wchar_t
31 # define STRLEN wcslen
32 # define STRCHR wcschr
33 # define BIG_CHAR WCHAR_MAX
34 # define SMALL_CHAR 1273
35 #endif /* WIDE */
37 #ifndef STRPBRK_RESULT
38 # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL)
39 # define RES_TYPE CHAR *
40 # define TEST_MAIN
41 # ifndef WIDE
42 # define TEST_NAME "strpbrk"
43 # else
44 # define TEST_NAME "wcspbrk"
45 # endif /* WIDE */
46 # include "test-string.h"
48 # ifndef WIDE
49 # define STRPBRK strpbrk
50 # define SIMPLE_STRPBRK simple_strpbrk
51 # define STUPID_STRPBRK stupid_strpbrk
52 # else
53 # include <wchar.h>
54 # define STRPBRK wcspbrk
55 # define SIMPLE_STRPBRK simple_wcspbrk
56 # define STUPID_STRPBRK stupid_wcspbrk
57 # endif /* WIDE */
59 typedef CHAR *(*proto_t) (const CHAR *, const CHAR *);
60 CHAR *SIMPLE_STRPBRK (const CHAR *, const CHAR *);
61 CHAR *STUPID_STRPBRK (const CHAR *, const CHAR *);
63 IMPL (STUPID_STRPBRK, 0)
64 IMPL (SIMPLE_STRPBRK, 0)
65 IMPL (STRPBRK, 1)
67 CHAR *
68 SIMPLE_STRPBRK (const CHAR *s, const CHAR *rej)
70 const CHAR *r;
71 CHAR c;
73 while ((c = *s++) != '\0')
74 for (r = rej; *r != '\0'; ++r)
75 if (*r == c)
76 return (CHAR *) s - 1;
77 return NULL;
80 CHAR *
81 STUPID_STRPBRK (const CHAR *s, const CHAR *rej)
83 size_t ns = STRLEN (s), nrej = STRLEN (rej);
84 size_t i, j;
86 for (i = 0; i < ns; ++i)
87 for (j = 0; j < nrej; ++j)
88 if (s[i] == rej[j])
89 return (CHAR *) s + i;
90 return NULL;
92 #endif /* !STRPBRK_RESULT */
94 static void
95 do_one_test (impl_t *impl, const CHAR *s, const CHAR *rej, RES_TYPE exp_res)
97 RES_TYPE res = CALL (impl, s, rej);
98 if (res != exp_res)
100 error (0, 0, "Wrong result in function %s %p %p", impl->name,
101 (void *) res, (void *) exp_res);
102 ret = 1;
103 return;
107 static void
108 do_test (size_t align, size_t pos, size_t len)
110 size_t i;
111 int c;
112 RES_TYPE result;
113 CHAR *rej, *s;
115 align &= 7;
116 if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240)
117 return;
119 rej = (CHAR *) (buf2) + (random () & 255);
120 s = (CHAR *) (buf1) + align;
122 for (i = 0; i < len; ++i)
124 rej[i] = random () & BIG_CHAR;
125 if (!rej[i])
126 rej[i] = random () & BIG_CHAR;
127 if (!rej[i])
128 rej[i] = 1 + (random () & SMALL_CHAR);
130 rej[len] = '\0';
131 for (c = 1; c <= BIG_CHAR; ++c)
132 if (STRCHR (rej, c) == NULL)
133 break;
135 for (i = 0; i < pos; ++i)
137 s[i] = random () & BIG_CHAR;
138 if (STRCHR (rej, s[i]))
140 s[i] = random () & BIG_CHAR;
141 if (STRCHR (rej, s[i]))
142 s[i] = c;
145 s[pos] = rej[random () % (len + 1)];
146 if (s[pos])
148 for (i = pos + 1; i < pos + 10; ++i)
149 s[i] = random () & BIG_CHAR;
150 s[i] = '\0';
152 result = STRPBRK_RESULT (s, pos);
154 FOR_EACH_IMPL (impl, 0)
155 do_one_test (impl, s, rej, result);
158 static void
159 do_random_tests (void)
161 size_t i, j, n, align, pos, len, rlen;
162 RES_TYPE result;
163 int c;
164 UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
165 UCHAR *rej;
167 for (n = 0; n < ITERATIONS; n++)
169 align = random () & 15;
170 pos = random () & 511;
171 if (pos + align >= 511)
172 pos = 510 - align - (random () & 7);
173 len = random () & 511;
174 if (pos >= len && (random () & 1))
175 len = pos + 1 + (random () & 7);
176 if (len + align >= 512)
177 len = 511 - align - (random () & 7);
178 if (random () & 1)
179 rlen = random () & 63;
180 else
181 rlen = random () & 15;
182 rej = (UCHAR *) (buf2 + page_size) - rlen - 1 - (random () & 7);
183 for (i = 0; i < rlen; ++i)
185 rej[i] = random () & BIG_CHAR;
186 if (!rej[i])
187 rej[i] = random () & BIG_CHAR;
188 if (!rej[i])
189 rej[i] = 1 + (random () & SMALL_CHAR);
191 rej[i] = '\0';
192 for (c = 1; c <= BIG_CHAR; ++c)
193 if (STRCHR ((CHAR *) rej, c) == NULL)
194 break;
195 j = (pos > len ? pos : len) + align + 64;
196 if (j > 512)
197 j = 512;
199 for (i = 0; i < j; i++)
201 if (i == len + align)
202 p[i] = '\0';
203 else if (i == pos + align)
204 p[i] = rej[random () % (rlen + 1)];
205 else if (i < align || i > pos + align)
206 p[i] = random () & BIG_CHAR;
207 else
209 p[i] = random () & BIG_CHAR;
210 if (STRCHR ((CHAR *) rej, p[i]))
212 p[i] = random () & BIG_CHAR;
213 if (STRCHR ((CHAR *) rej, p[i]))
214 p[i] = c;
219 result = STRPBRK_RESULT ((CHAR *) (p + align), pos < len ? pos : len);
221 FOR_EACH_IMPL (impl, 1)
222 if (CALL (impl, (CHAR *) (p + align), (CHAR *) rej) != result)
224 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %p != %p",
225 n, impl->name, align, rej, rlen, pos, len,
226 (void *) CALL (impl, (CHAR *) (p + align), (CHAR *) rej),
227 (void *) result);
228 ret = 1;
234 test_main (void)
236 size_t i;
238 test_init ();
240 printf ("%32s", "");
241 FOR_EACH_IMPL (impl, 0)
242 printf ("\t%s", impl->name);
243 putchar ('\n');
245 for (i = 0; i < 32; ++i)
247 do_test (0, 512, i);
248 do_test (i, 512, i);
251 for (i = 1; i < 8; ++i)
253 do_test (0, 16 << i, 4);
254 do_test (i, 16 << i, 4);
257 for (i = 1; i < 8; ++i)
258 do_test (i, 64, 10);
260 for (i = 0; i < 64; ++i)
261 do_test (0, i, 6);
263 do_random_tests ();
264 return ret;
267 #include <support/test-driver.c>