]> git.ipfire.org Git - thirdparty/glibc.git/blame - string/test-strspn.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / string / test-strspn.c
CommitLineData
58ef9ef7 1/* Test and measure strspn functions.
d614a753 2 Copyright (C) 1999-2020 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 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
58ef9ef7
RM
19
20#define TEST_MAIN
f1ffad98
SL
21#ifndef WIDE
22# define TEST_NAME "strspn"
23#else
24# define TEST_NAME "wcsspn"
25#endif /* WIDE */
58ef9ef7
RM
26#include "test-string.h"
27
f1ffad98
SL
28#ifndef WIDE
29# define STRSPN strspn
30# define CHAR char
31# define UCHAR unsigned char
32# define SIMPLE_STRSPN simple_strspn
33# define STUPID_STRSPN stupid_strspn
34# define STRLEN strlen
35# define STRCHR strchr
36# define BIG_CHAR CHAR_MAX
37# define SMALL_CHAR 127
38#else
39# include <wchar.h>
40# define STRSPN wcsspn
41# define CHAR wchar_t
42# define UCHAR wchar_t
43# define SIMPLE_STRSPN simple_wcsspn
44# define STUPID_STRSPN stupid_wcsspn
45# define STRLEN wcslen
46# define STRCHR wcschr
47# define BIG_CHAR WCHAR_MAX
48# define SMALL_CHAR 1273
49#endif /* WIDE */
50
51typedef size_t (*proto_t) (const CHAR *, const CHAR *);
52size_t SIMPLE_STRSPN (const CHAR *, const CHAR *);
53size_t STUPID_STRSPN (const CHAR *, const CHAR *);
54
55IMPL (STUPID_STRSPN, 0)
56IMPL (SIMPLE_STRSPN, 0)
57IMPL (STRSPN, 1)
58ef9ef7
RM
58
59size_t
f1ffad98 60SIMPLE_STRSPN (const CHAR *s, const CHAR *acc)
58ef9ef7 61{
f1ffad98
SL
62 const CHAR *r, *str = s;
63 CHAR c;
58ef9ef7
RM
64
65 while ((c = *s++) != '\0')
66 {
e8c1660f 67 for (r = acc; *r != '\0'; ++r)
58ef9ef7
RM
68 if (*r == c)
69 break;
70 if (*r == '\0')
71 return s - str - 1;
72 }
73 return s - str - 1;
74}
75
e8c1660f 76size_t
f1ffad98 77STUPID_STRSPN (const CHAR *s, const CHAR *acc)
e8c1660f 78{
f1ffad98 79 size_t ns = STRLEN (s), nacc = STRLEN (acc);
e8c1660f
RM
80 size_t i, j;
81
82 for (i = 0; i < ns; ++i)
83 {
84 for (j = 0; j < nacc; ++j)
85 if (s[i] == acc[j])
86 break;
87 if (j == nacc)
88 return i;
89 }
90 return i;
91}
92
58ef9ef7 93static void
f1ffad98 94do_one_test (impl_t *impl, const CHAR *s, const CHAR *acc, size_t exp_res)
58ef9ef7
RM
95{
96 size_t res = CALL (impl, s, acc);
97 if (res != exp_res)
98 {
99 error (0, 0, "Wrong result in function %s %p %p", impl->name,
100 (void *) res, (void *) exp_res);
101 ret = 1;
102 return;
103 }
58ef9ef7
RM
104}
105
106static void
107do_test (size_t align, size_t pos, size_t len)
108{
109 size_t i;
f1ffad98 110 CHAR *acc, *s;
58ef9ef7
RM
111
112 align &= 7;
f1ffad98 113 if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240 || ! len)
58ef9ef7
RM
114 return;
115
f1ffad98
SL
116 acc = (CHAR *) (buf2) + (random () & 255);
117 s = (CHAR *) (buf1) + align;
58ef9ef7
RM
118
119 for (i = 0; i < len; ++i)
120 {
f1ffad98 121 acc[i] = random () & BIG_CHAR;
58ef9ef7 122 if (!acc[i])
f1ffad98 123 acc[i] = random () & BIG_CHAR;
58ef9ef7 124 if (!acc[i])
f1ffad98 125 acc[i] = 1 + (random () & SMALL_CHAR);
58ef9ef7
RM
126 }
127 acc[len] = '\0';
128
129 for (i = 0; i < pos; ++i)
130 s[i] = acc[random () % len];
f1ffad98
SL
131 s[pos] = random () & BIG_CHAR;
132 if (STRCHR (acc, s[pos]))
58ef9ef7 133 s[pos] = '\0';
9372c958
RM
134 else
135 {
136 for (i = pos + 1; i < pos + 10; ++i)
f1ffad98 137 s[i] = random () & BIG_CHAR;
9372c958
RM
138 s[i] = '\0';
139 }
58ef9ef7 140
58ef9ef7
RM
141 FOR_EACH_IMPL (impl, 0)
142 do_one_test (impl, s, acc, pos);
58ef9ef7
RM
143}
144
145static void
146do_random_tests (void)
147{
e8c1660f 148 size_t i, j, n, align, pos, alen, len;
f1ffad98
SL
149 UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
150 UCHAR *acc;
58ef9ef7
RM
151
152 for (n = 0; n < ITERATIONS; n++)
153 {
154 align = random () & 15;
155 if (random () & 1)
e8c1660f 156 alen = random () & 63;
58ef9ef7 157 else
e8c1660f
RM
158 alen = random () & 15;
159 if (!alen)
58ef9ef7
RM
160 pos = 0;
161 else
162 pos = random () & 511;
163 if (pos + align >= 511)
164 pos = 510 - align - (random () & 7);
e8c1660f
RM
165 len = random () & 511;
166 if (len + align >= 512)
167 len = 511 - align - (random () & 7);
f1ffad98 168 acc = (UCHAR *) (buf2 + page_size) - alen - 1 - (random () & 7);
e8c1660f 169 for (i = 0; i < alen; ++i)
58ef9ef7 170 {
f1ffad98 171 acc[i] = random () & BIG_CHAR;
58ef9ef7 172 if (!acc[i])
f1ffad98 173 acc[i] = random () & BIG_CHAR;
58ef9ef7 174 if (!acc[i])
f1ffad98 175 acc[i] = 1 + (random () & SMALL_CHAR);
58ef9ef7
RM
176 }
177 acc[i] = '\0';
e8c1660f 178 j = (pos > len ? pos : len) + align + 64;
58ef9ef7
RM
179 if (j > 512)
180 j = 512;
181
182 for (i = 0; i < j; i++)
183 {
e8c1660f
RM
184 if (i == len + align)
185 p[i] = '\0';
186 else if (i == pos + align)
58ef9ef7 187 {
f1ffad98
SL
188 p[i] = random () & BIG_CHAR;
189 if (STRCHR ((CHAR *) acc, p[i]))
58ef9ef7 190 p[i] = '\0';
58ef9ef7
RM
191 }
192 else if (i < align || i > pos + align)
f1ffad98 193 p[i] = random () & BIG_CHAR;
58ef9ef7 194 else
e8c1660f 195 p[i] = acc [random () % alen];
58ef9ef7
RM
196 }
197
198 FOR_EACH_IMPL (impl, 1)
f1ffad98
SL
199 if (CALL (impl, (CHAR *) (p + align),
200 (CHAR *) acc) != (pos < len ? pos : len))
58ef9ef7 201 {
e8c1660f
RM
202 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %zd != %zd",
203 n, impl->name, align, acc, alen, pos, len,
f1ffad98 204 CALL (impl, (CHAR *) (p + align), (CHAR *) acc),
0317eaec 205 (pos < len ? pos : len));
58ef9ef7
RM
206 ret = 1;
207 }
208 }
209}
210
211int
212test_main (void)
213{
214 size_t i;
215
216 test_init ();
217
218 printf ("%32s", "");
219 FOR_EACH_IMPL (impl, 0)
220 printf ("\t%s", impl->name);
221 putchar ('\n');
222
223 for (i = 0; i < 32; ++i)
224 {
225 do_test (0, 512, i);
226 do_test (i, 512, i);
227 }
228
229 for (i = 1; i < 8; ++i)
230 {
231 do_test (0, 16 << i, 4);
232 do_test (i, 16 << i, 4);
233 }
234
235 for (i = 1; i < 8; ++i)
236 do_test (i, 64, 10);
237
238 for (i = 0; i < 64; ++i)
239 do_test (0, i, 6);
240
241 do_random_tests ();
242 return ret;
243}
244
fb82116f 245#include <support/test-driver.c>