1 /* Test and measure strspn functions.
2 Copyright (C) 1999-2020 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 <https://www.gnu.org/licenses/>. */
22 # define TEST_NAME "strspn"
24 # define TEST_NAME "wcsspn"
26 #include "test-string.h"
29 # define STRSPN strspn
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
40 # define STRSPN wcsspn
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
51 typedef size_t (*proto_t
) (const CHAR
*, const CHAR
*);
52 size_t SIMPLE_STRSPN (const CHAR
*, const CHAR
*);
53 size_t STUPID_STRSPN (const CHAR
*, const CHAR
*);
55 IMPL (STUPID_STRSPN
, 0)
56 IMPL (SIMPLE_STRSPN
, 0)
60 SIMPLE_STRSPN (const CHAR
*s
, const CHAR
*acc
)
62 const CHAR
*r
, *str
= s
;
65 while ((c
= *s
++) != '\0')
67 for (r
= acc
; *r
!= '\0'; ++r
)
77 STUPID_STRSPN (const CHAR
*s
, const CHAR
*acc
)
79 size_t ns
= STRLEN (s
), nacc
= STRLEN (acc
);
82 for (i
= 0; i
< ns
; ++i
)
84 for (j
= 0; j
< nacc
; ++j
)
94 do_one_test (impl_t
*impl
, const CHAR
*s
, const CHAR
*acc
, size_t exp_res
)
96 size_t res
= CALL (impl
, s
, acc
);
99 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
100 (void *) res
, (void *) exp_res
);
107 do_test (size_t align
, size_t pos
, size_t len
)
113 if ((align
+ pos
+ 10) * sizeof (CHAR
) >= page_size
|| len
> 240 || ! len
)
116 acc
= (CHAR
*) (buf2
) + (random () & 255);
117 s
= (CHAR
*) (buf1
) + align
;
119 for (i
= 0; i
< len
; ++i
)
121 acc
[i
] = random () & BIG_CHAR
;
123 acc
[i
] = random () & BIG_CHAR
;
125 acc
[i
] = 1 + (random () & SMALL_CHAR
);
129 for (i
= 0; i
< pos
; ++i
)
130 s
[i
] = acc
[random () % len
];
131 s
[pos
] = random () & BIG_CHAR
;
132 if (STRCHR (acc
, s
[pos
]))
136 for (i
= pos
+ 1; i
< pos
+ 10; ++i
)
137 s
[i
] = random () & BIG_CHAR
;
141 FOR_EACH_IMPL (impl
, 0)
142 do_one_test (impl
, s
, acc
, pos
);
146 do_random_tests (void)
148 size_t i
, j
, n
, align
, pos
, alen
, len
;
149 UCHAR
*p
= (UCHAR
*) (buf1
+ page_size
) - 512;
152 for (n
= 0; n
< ITERATIONS
; n
++)
154 align
= random () & 15;
156 alen
= random () & 63;
158 alen
= random () & 15;
162 pos
= random () & 511;
163 if (pos
+ align
>= 511)
164 pos
= 510 - align
- (random () & 7);
165 len
= random () & 511;
166 if (len
+ align
>= 512)
167 len
= 511 - align
- (random () & 7);
168 acc
= (UCHAR
*) (buf2
+ page_size
) - alen
- 1 - (random () & 7);
169 for (i
= 0; i
< alen
; ++i
)
171 acc
[i
] = random () & BIG_CHAR
;
173 acc
[i
] = random () & BIG_CHAR
;
175 acc
[i
] = 1 + (random () & SMALL_CHAR
);
178 j
= (pos
> len
? pos
: len
) + align
+ 64;
182 for (i
= 0; i
< j
; i
++)
184 if (i
== len
+ align
)
186 else if (i
== pos
+ align
)
188 p
[i
] = random () & BIG_CHAR
;
189 if (STRCHR ((CHAR
*) acc
, p
[i
]))
192 else if (i
< align
|| i
> pos
+ align
)
193 p
[i
] = random () & BIG_CHAR
;
195 p
[i
] = acc
[random () % alen
];
198 FOR_EACH_IMPL (impl
, 1)
199 if (CALL (impl
, (CHAR
*) (p
+ align
),
200 (CHAR
*) acc
) != (pos
< len
? pos
: len
))
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
,
204 CALL (impl
, (CHAR
*) (p
+ align
), (CHAR
*) acc
),
205 (pos
< len
? pos
: len
));
219 FOR_EACH_IMPL (impl
, 0)
220 printf ("\t%s", impl
->name
);
223 for (i
= 0; i
< 32; ++i
)
229 for (i
= 1; i
< 8; ++i
)
231 do_test (0, 16 << i
, 4);
232 do_test (i
, 16 << i
, 4);
235 for (i
= 1; i
< 8; ++i
)
238 for (i
= 0; i
< 64; ++i
)
245 #include <support/test-driver.c>