1 /* Test and measure strncat functions.
2 Copyright (C) 2011 Free Software Foundation, Inc.
3 Contributed by Intel Corporation.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 #include "test-string.h"
22 typedef char *(*proto_t
) (char *, const char *, size_t);
23 char *stupid_strncat (char *, const char *, size_t);
24 char *simple_strncat (char *, const char *, size_t);
26 IMPL (stupid_strncat
, 0)
30 stupid_strncat (char *dst
, const char *src
, size_t n
)
33 while (*dst
++ != '\0');
36 if ( (*dst
++ = *src
++) == '\0')
43 do_one_test (impl_t
*impl
, char *dst
, const char *src
, size_t n
)
45 size_t k
= strlen (dst
);
46 if (CALL (impl
, dst
, src
, n
) != dst
)
48 error (0, 0, "Wrong result in function %s %p != %p", impl
->name
,
49 CALL (impl
, dst
, src
, n
), dst
);
54 size_t len
= strlen (src
);
55 if (memcmp (dst
+ k
, src
, len
+ 1 > n
? n
: len
+ 1) != 0)
57 error (0, 0, "Incorrect cancatination in function %s",
62 if (n
< len
&& dst
[k
+ n
] != '\0')
64 error (0, 0, "There is no zero in the end of output string in %s",
71 hp_timing_t start
__attribute ((unused
));
72 hp_timing_t stop
__attribute ((unused
));
73 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
76 for (i
= 0; i
< 32; ++i
)
79 HP_TIMING_NOW (start
);
80 CALL (impl
, dst
, src
, n
);
82 HP_TIMING_BEST (best_time
, start
, stop
);
85 printf ("\t%zd", (size_t) best_time
);
90 do_test (size_t align1
, size_t align2
, size_t len1
, size_t len2
,
91 size_t n
, int max_char
)
97 if (align1
+ len1
>= page_size
)
99 if (align1
+ n
> page_size
)
102 if (align2
+ len1
+ len2
>= page_size
)
104 if (align2
+ len1
+ n
> page_size
)
106 s1
= (char *) (buf1
+ align1
);
107 s2
= (char *) (buf2
+ align2
);
109 for (i
= 0; i
< len1
; ++i
)
110 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
113 for (i
= 0; i
< len2
; i
++)
114 s2
[i
] = 32 + 23 * i
% (max_char
- 32);
117 printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
118 len1
, len2
, align1
, align2
, n
);
120 FOR_EACH_IMPL (impl
, 0)
123 do_one_test (impl
, s2
, s1
, n
);
131 do_random_tests (void)
133 size_t i
, j
, n
, align1
, align2
, len1
, len2
, N
;
134 unsigned char *p1
= buf1
+ page_size
- 512;
135 unsigned char *p2
= buf2
+ page_size
- 512;
137 fprintf (stdout
, "Number of iterations in random test = %zd\n",
139 for (n
= 0; n
< ITERATIONS
; n
++)
142 align1
= random () & 31;
144 align2
= random () & 31;
146 align2
= align1
+ (random () & 24);
147 len1
= random () & 511;
148 if (len1
+ align2
> 512)
149 len2
= random () & 7;
151 len2
= (512 - len1
- align2
) * (random () & (1024 * 1024 - 1))
154 if (align2
+ len2
> j
)
157 len1
= 510 - j
- (random () & 7);
160 if (align1
+ len1
< 512 - 8)
162 j
= 510 - align1
- len1
- (random () & 31);
163 if (j
> 0 && j
< 512)
166 j
= len1
+ align1
+ 64;
169 for (i
= 0; i
< j
; i
++)
171 if (i
== len1
+ align1
)
175 p1
[i
] = random () & 255;
176 if (i
>= align1
&& i
< len1
+ align1
&& !p1
[i
])
177 p1
[i
] = (random () & 127) + 3;
180 for (i
= 0; i
< len2
; i
++)
182 buf1
[i
] = random () & 255;
184 buf1
[i
] = (random () & 127) + 3;
188 FOR_EACH_IMPL (impl
, 1)
190 memset (p2
- 64, '\1', align2
+ 64);
191 memset (p2
+ align2
+ len2
+ 1, '\1', 512 - align2
- len2
- 1);
192 memcpy (p2
+ align2
, buf1
, len2
+ 1);
193 res
= (unsigned char *) CALL (impl
, (char *) (p2
+ align2
),
194 (char *) (p1
+ align1
), N
);
195 if (res
!= p2
+ align2
)
197 error (0, 0, "Iteration %zd - wrong result in function %s "
198 "(%zd, %zd, %zd, %zd, %zd) %p != %p",
199 n
, impl
->name
, align1
, align2
, len1
, len2
, N
,
203 for (j
= 0; j
< align2
+ 64; ++j
)
205 if (p2
[j
- 64] != '\1')
207 error (0, 0, "Iteration %zd - garbage before dst, %s "
208 "%zd, %zd, %zd, %zd, %zd)",
209 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
214 if (memcmp (p2
+ align2
, buf1
, len2
))
216 error (0, 0, "Iteration %zd - garbage in string before, %s "
217 "(%zd, %zd, %zd, %zd, %zd)",
218 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
223 j
= align2
+ N
+ 1 + len2
;
225 j
= align2
+ len1
+ 1 + len2
;
230 error (0, 0, "Iteration %zd - garbage after, %s "
231 "(%zd, %zd, %zd, %zd, %zd)",
232 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
239 if (p2
[align2
+ N
+ len2
] != '\0')
241 error (0, 0, "Iteration %zd - there is no zero at the "
242 "end of output string, %s (%zd, %zd, %zd, %zd, %zd)",
243 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
247 if (memcmp (p1
+ align1
, p2
+ align2
+ len2
,
248 (len1
+ 1) > N
? N
: len1
+ 1))
250 error (0, 0, "Iteration %zd - different strings, %s "
251 "(%zd, %zd, %zd, %zd, %zd)",
252 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
267 FOR_EACH_IMPL (impl
, 0)
268 printf ("\t%s", impl
->name
);
271 for (n
= 2; n
<= 2048; n
*=4)
273 do_test (0, 2, 2, 2, n
, 127);
274 do_test (0, 0, 4, 4, n
, 127);
275 do_test (4, 0, 4, 4, n
, 255);
276 do_test (0, 0, 8, 8, n
, 127);
277 do_test (0, 8, 8, 8, n
, 127);
279 for (i
= 1; i
< 8; ++i
)
281 do_test (0, 0, 8 << i
, 8 << i
, n
, 127);
282 do_test (8 - i
, 2 * i
, 8 << i
, 8 << i
, n
, 127);
283 do_test (0, 0, 8 << i
, 2 << i
, n
, 127);
284 do_test (8 - i
, 2 * i
, 8 << i
, 2 << i
, n
, 127);
287 for (i
= 1; i
< 8; ++i
)
289 do_test (i
, 2 * i
, 8 << i
, 1, n
, 127);
290 do_test (2 * i
, i
, 8 << i
, 1, n
, 255);
291 do_test (i
, i
, 8 << i
, 10, n
, 127);