1 /* Test and measure strspn functions.
2 Copyright (C) 1999-2014 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/>. */
21 #define TEST_NAME "strspn"
22 #include "test-string.h"
24 typedef size_t (*proto_t
) (const char *, const char *);
25 size_t simple_strspn (const char *, const char *);
26 size_t stupid_strspn (const char *, const char *);
28 IMPL (stupid_strspn
, 0)
29 IMPL (simple_strspn
, 0)
33 simple_strspn (const char *s
, const char *acc
)
35 const char *r
, *str
= s
;
38 while ((c
= *s
++) != '\0')
40 for (r
= acc
; *r
!= '\0'; ++r
)
50 stupid_strspn (const char *s
, const char *acc
)
52 size_t ns
= strlen (s
), nacc
= strlen (acc
);
55 for (i
= 0; i
< ns
; ++i
)
57 for (j
= 0; j
< nacc
; ++j
)
67 do_one_test (impl_t
*impl
, const char *s
, const char *acc
, size_t exp_res
)
69 size_t res
= CALL (impl
, s
, acc
);
72 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
73 (void *) res
, (void *) exp_res
);
80 do_test (size_t align
, size_t pos
, size_t len
)
86 if (align
+ pos
+ 10 >= page_size
|| len
> 240 || ! len
)
89 acc
= (char *) (buf2
+ (random () & 255));
90 s
= (char *) (buf1
+ align
);
92 for (i
= 0; i
< len
; ++i
)
94 acc
[i
] = random () & 255;
96 acc
[i
] = random () & 255;
98 acc
[i
] = 1 + (random () & 127);
102 for (i
= 0; i
< pos
; ++i
)
103 s
[i
] = acc
[random () % len
];
104 s
[pos
] = random () & 255;
105 if (strchr (acc
, s
[pos
]))
109 for (i
= pos
+ 1; i
< pos
+ 10; ++i
)
110 s
[i
] = random () & 255;
114 FOR_EACH_IMPL (impl
, 0)
115 do_one_test (impl
, s
, acc
, pos
);
119 do_random_tests (void)
121 size_t i
, j
, n
, align
, pos
, alen
, len
;
122 unsigned char *p
= buf1
+ page_size
- 512;
125 for (n
= 0; n
< ITERATIONS
; n
++)
127 align
= random () & 15;
129 alen
= random () & 63;
131 alen
= random () & 15;
135 pos
= random () & 511;
136 if (pos
+ align
>= 511)
137 pos
= 510 - align
- (random () & 7);
138 len
= random () & 511;
139 if (len
+ align
>= 512)
140 len
= 511 - align
- (random () & 7);
141 acc
= buf2
+ page_size
- alen
- 1 - (random () & 7);
142 for (i
= 0; i
< alen
; ++i
)
144 acc
[i
] = random () & 255;
146 acc
[i
] = random () & 255;
148 acc
[i
] = 1 + (random () & 127);
151 j
= (pos
> len
? pos
: len
) + align
+ 64;
155 for (i
= 0; i
< j
; i
++)
157 if (i
== len
+ align
)
159 else if (i
== pos
+ align
)
161 p
[i
] = random () & 255;
162 if (strchr ((char *) acc
, p
[i
]))
165 else if (i
< align
|| i
> pos
+ align
)
166 p
[i
] = random () & 255;
168 p
[i
] = acc
[random () % alen
];
171 FOR_EACH_IMPL (impl
, 1)
172 if (CALL (impl
, (char *) (p
+ align
),
173 (char *) acc
) != (pos
< len
? pos
: len
))
175 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %zd != %zd",
176 n
, impl
->name
, align
, acc
, alen
, pos
, len
,
177 CALL (impl
, (char *) (p
+ align
), (char *) acc
),
178 (pos
< len
? pos
: len
));
192 FOR_EACH_IMPL (impl
, 0)
193 printf ("\t%s", impl
->name
);
196 for (i
= 0; i
< 32; ++i
)
202 for (i
= 1; i
< 8; ++i
)
204 do_test (0, 16 << i
, 4);
205 do_test (i
, 16 << i
, 4);
208 for (i
= 1; i
< 8; ++i
)
211 for (i
= 0; i
< 64; ++i
)
218 #include "../test-skeleton.c"