96.1.1
by Marcus Shawcroft
Add strnlen tests from glibc. |
1 |
/* Test and measure strlen functions.
|
2 |
Copyright (C) 1999-2012 Free Software Foundation, Inc.
|
|
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
|
|
17 |
License along with the GNU C Library; if not, see
|
|
18 |
<http://www.gnu.org/licenses/>. */
|
|
19 |
||
20 |
#define TEST_MAIN
|
|
21 |
#define TEST_NAME "strnlen"
|
|
22 |
#include "test-string.h" |
|
23 |
||
24 |
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
|
25 |
||
26 |
typedef size_t (*proto_t) (const char *, size_t); |
|
27 |
size_t simple_strnlen (const char *, size_t); |
|
28 |
||
29 |
IMPL (simple_strnlen, 0) |
|
30 |
IMPL (strnlen, 1) |
|
31 |
||
32 |
size_t
|
|
33 |
simple_strnlen (const char *s, size_t maxlen) |
|
34 |
{
|
|
35 |
size_t i; |
|
36 |
||
37 |
for (i = 0; i < maxlen && s[i]; ++i); |
|
38 |
return i; |
|
39 |
}
|
|
40 |
||
41 |
static void |
|
42 |
do_one_test (impl_t *impl, const char *s, size_t maxlen, size_t exp_len) |
|
43 |
{
|
|
44 |
size_t len = CALL (impl, s, maxlen); |
|
45 |
if (len != exp_len) |
|
46 |
{
|
|
47 |
error (0, 0, "Wrong result in function %s %zd %zd", impl->name, |
|
48 |
len, exp_len); |
|
49 |
ret = 1; |
|
50 |
return; |
|
51 |
}
|
|
52 |
||
53 |
if (HP_TIMING_AVAIL) |
|
54 |
{
|
|
55 |
hp_timing_t start __attribute ((unused)); |
|
56 |
hp_timing_t stop __attribute ((unused)); |
|
57 |
hp_timing_t best_time = ~ (hp_timing_t) 0; |
|
58 |
size_t i; |
|
59 |
||
60 |
for (i = 0; i < 32; ++i) |
|
61 |
{
|
|
62 |
HP_TIMING_NOW (start); |
|
63 |
CALL (impl, s, maxlen); |
|
64 |
HP_TIMING_NOW (stop); |
|
65 |
HP_TIMING_BEST (best_time, start, stop); |
|
66 |
}
|
|
67 |
||
68 |
printf ("\t%zd", (size_t) best_time); |
|
69 |
}
|
|
70 |
}
|
|
71 |
||
72 |
static void |
|
73 |
do_test (size_t align, size_t len, size_t maxlen, int max_char) |
|
74 |
{
|
|
75 |
size_t i; |
|
76 |
||
77 |
align &= 7; |
|
78 |
if (align + len >= page_size) |
|
79 |
return; |
|
80 |
||
81 |
for (i = 0; i < len; ++i) |
|
82 |
buf1[align + i] = 1 + 7 * i % max_char; |
|
83 |
buf1[align + len] = 0; |
|
84 |
||
85 |
if (HP_TIMING_AVAIL) |
|
86 |
printf ("Length %4zd, alignment %2zd:", len, align); |
|
87 |
||
88 |
FOR_EACH_IMPL (impl, 0) |
|
89 |
do_one_test (impl, (char *) (buf1 + align), maxlen, MIN (len, maxlen)); |
|
90 |
||
91 |
if (HP_TIMING_AVAIL) |
|
92 |
putchar ('\n'); |
|
93 |
}
|
|
94 |
||
95 |
static void |
|
96 |
do_random_tests (void) |
|
97 |
{
|
|
98 |
size_t i, j, n, align, len; |
|
99 |
unsigned char *p = buf1 + page_size - 512; |
|
100 |
||
101 |
for (n = 0; n < ITERATIONS; n++) |
|
102 |
{
|
|
103 |
align = random () & 15; |
|
104 |
len = random () & 511; |
|
105 |
if (len + align > 510) |
|
106 |
len = 511 - align - (random () & 7); |
|
107 |
j = len + align + 64; |
|
108 |
if (j > 512) |
|
109 |
j = 512; |
|
110 |
||
111 |
for (i = 0; i < j; i++) |
|
112 |
{
|
|
113 |
if (i == len + align) |
|
114 |
p[i] = 0; |
|
115 |
else
|
|
116 |
{
|
|
117 |
p[i] = random () & 255; |
|
118 |
if (i >= align && i < len + align && !p[i]) |
|
119 |
p[i] = (random () & 127) + 1; |
|
120 |
}
|
|
121 |
}
|
|
122 |
||
123 |
FOR_EACH_IMPL (impl, 1) |
|
124 |
{
|
|
125 |
if (len > 0 |
|
126 |
&& CALL (impl, (char *) (p + align), len - 1) != len - 1) |
|
127 |
{
|
|
128 |
error (0, 0, "Iteration %zd (limited) - wrong result in function %s (%zd) %zd != %zd, p %p", |
|
129 |
n, impl->name, align, |
|
130 |
CALL (impl, (char *) (p + align), len - 1), len - 1, p); |
|
131 |
ret = 1; |
|
132 |
}
|
|
133 |
if (CALL (impl, (char *) (p + align), len) != len) |
|
134 |
{
|
|
135 |
error (0, 0, "Iteration %zd (exact) - wrong result in function %s (%zd) %zd != %zd, p %p", |
|
136 |
n, impl->name, align, |
|
137 |
CALL (impl, (char *) (p + align), len), len, p); |
|
138 |
ret = 1; |
|
139 |
}
|
|
140 |
if (CALL (impl, (char *) (p + align), len + 1) != len) |
|
141 |
{
|
|
142 |
error (0, 0, "Iteration %zd (long) - wrong result in function %s (%zd) %zd != %zd, p %p", |
|
143 |
n, impl->name, align, |
|
144 |
CALL (impl, (char *) (p + align), len + 1), len, p); |
|
145 |
ret = 1; |
|
146 |
}
|
|
147 |
}
|
|
148 |
}
|
|
149 |
}
|
|
150 |
||
151 |
int
|
|
152 |
test_main (void) |
|
153 |
{
|
|
154 |
size_t i; |
|
155 |
||
156 |
test_init (); |
|
157 |
||
158 |
printf ("%20s", ""); |
|
159 |
FOR_EACH_IMPL (impl, 0) |
|
160 |
printf ("\t%s", impl->name); |
|
161 |
putchar ('\n'); |
|
162 |
||
163 |
for (i = 1; i < 8; ++i) |
|
164 |
{
|
|
165 |
do_test (0, i, i - 1, 127); |
|
166 |
do_test (0, i, i, 127); |
|
167 |
do_test (0, i, i + 1, 127); |
|
168 |
}
|
|
169 |
||
170 |
for (i = 1; i < 8; ++i) |
|
171 |
{
|
|
172 |
do_test (i, i, i - 1, 127); |
|
173 |
do_test (i, i, i, 127); |
|
174 |
do_test (i, i, i + 1, 127); |
|
175 |
}
|
|
176 |
||
177 |
for (i = 2; i <= 10; ++i) |
|
178 |
{
|
|
179 |
do_test (0, 1 << i, 5000, 127); |
|
180 |
do_test (1, 1 << i, 5000, 127); |
|
181 |
}
|
|
182 |
||
183 |
for (i = 1; i < 8; ++i) |
|
184 |
do_test (0, i, 5000, 255); |
|
185 |
||
186 |
for (i = 1; i < 8; ++i) |
|
187 |
do_test (i, i, 5000, 255); |
|
188 |
||
189 |
for (i = 2; i <= 10; ++i) |
|
190 |
{
|
|
191 |
do_test (0, 1 << i, 5000, 255); |
|
192 |
do_test (1, 1 << i, 5000, 255); |
|
193 |
}
|
|
194 |
||
195 |
do_random_tests (); |
|
196 |
return ret; |
|
197 |
}
|
|
198 |
||
199 |
#include "test-skeleton.c" |