1 /* Test and measure strpbrk functions.
2 Copyright (C) 1999-2019 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/>. */
22 # define UCHAR unsigned char
23 # define STRLEN strlen
24 # define STRCHR strchr
25 # define BIG_CHAR CHAR_MAX
26 # define SMALL_CHAR 127
30 # define UCHAR wchar_t
31 # define STRLEN wcslen
32 # define STRCHR wcschr
33 # define BIG_CHAR WCHAR_MAX
34 # define SMALL_CHAR 1273
37 #ifndef STRPBRK_RESULT
38 # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL)
39 # define RES_TYPE CHAR *
42 # define TEST_NAME "strpbrk"
44 # define TEST_NAME "wcspbrk"
46 # include "test-string.h"
49 # define STRPBRK strpbrk
50 # define SIMPLE_STRPBRK simple_strpbrk
51 # define STUPID_STRPBRK stupid_strpbrk
54 # define STRPBRK wcspbrk
55 # define SIMPLE_STRPBRK simple_wcspbrk
56 # define STUPID_STRPBRK stupid_wcspbrk
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)
68 SIMPLE_STRPBRK (const CHAR
*s
, const CHAR
*rej
)
73 while ((c
= *s
++) != '\0')
74 for (r
= rej
; *r
!= '\0'; ++r
)
76 return (CHAR
*) s
- 1;
81 STUPID_STRPBRK (const CHAR
*s
, const CHAR
*rej
)
83 size_t ns
= STRLEN (s
), nrej
= STRLEN (rej
);
86 for (i
= 0; i
< ns
; ++i
)
87 for (j
= 0; j
< nrej
; ++j
)
89 return (CHAR
*) s
+ i
;
92 #endif /* !STRPBRK_RESULT */
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
);
100 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
101 (void *) res
, (void *) exp_res
);
108 do_test (size_t align
, size_t pos
, size_t len
)
116 if ((align
+ pos
+ 10) * sizeof (CHAR
) >= page_size
|| len
> 240)
119 rej
= (CHAR
*) (buf2
) + (random () & 255);
120 s
= (CHAR
*) (buf1
) + align
;
122 for (i
= 0; i
< len
; ++i
)
124 rej
[i
] = random () & BIG_CHAR
;
126 rej
[i
] = random () & BIG_CHAR
;
128 rej
[i
] = 1 + (random () & SMALL_CHAR
);
131 for (c
= 1; c
<= BIG_CHAR
; ++c
)
132 if (STRCHR (rej
, c
) == NULL
)
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
]))
145 s
[pos
] = rej
[random () % (len
+ 1)];
148 for (i
= pos
+ 1; i
< pos
+ 10; ++i
)
149 s
[i
] = random () & BIG_CHAR
;
152 result
= STRPBRK_RESULT (s
, pos
);
154 FOR_EACH_IMPL (impl
, 0)
155 do_one_test (impl
, s
, rej
, result
);
159 do_random_tests (void)
161 size_t i
, j
, n
, align
, pos
, len
, rlen
;
164 UCHAR
*p
= (UCHAR
*) (buf1
+ page_size
) - 512;
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);
179 rlen
= random () & 63;
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
;
187 rej
[i
] = random () & BIG_CHAR
;
189 rej
[i
] = 1 + (random () & SMALL_CHAR
);
192 for (c
= 1; c
<= BIG_CHAR
; ++c
)
193 if (STRCHR ((CHAR
*) rej
, c
) == NULL
)
195 j
= (pos
> len
? pos
: len
) + align
+ 64;
199 for (i
= 0; i
< j
; i
++)
201 if (i
== len
+ align
)
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
;
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
]))
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
),
241 FOR_EACH_IMPL (impl
, 0)
242 printf ("\t%s", impl
->name
);
245 for (i
= 0; i
< 32; ++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
)
260 for (i
= 0; i
< 64; ++i
)
267 #include <support/test-driver.c>