1 /* Test and measure strncat functions.
2 Copyright (C) 2011-2012 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 #define TEST_NAME "strncat"
21 #include "test-string.h"
23 typedef char *(*proto_t
) (char *, const char *, size_t);
24 char *stupid_strncat (char *, const char *, size_t);
25 char *simple_strncat (char *, const char *, size_t);
27 IMPL (stupid_strncat
, 0)
31 stupid_strncat (char *dst
, const char *src
, size_t n
)
34 while (*dst
++ != '\0');
37 if ( (*dst
++ = *src
++) == '\0')
44 do_one_test (impl_t
*impl
, char *dst
, const char *src
, size_t n
)
46 size_t k
= strlen (dst
);
47 if (CALL (impl
, dst
, src
, n
) != dst
)
49 error (0, 0, "Wrong result in function %s %p != %p", impl
->name
,
50 CALL (impl
, dst
, src
, n
), dst
);
55 size_t len
= strlen (src
);
56 if (memcmp (dst
+ k
, src
, len
+ 1 > n
? n
: len
+ 1) != 0)
58 error (0, 0, "Incorrect cancatination in function %s",
63 if (n
< len
&& dst
[k
+ n
] != '\0')
65 error (0, 0, "There is no zero in the end of output string in %s",
72 hp_timing_t start
__attribute ((unused
));
73 hp_timing_t stop
__attribute ((unused
));
74 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
77 for (i
= 0; i
< 32; ++i
)
80 HP_TIMING_NOW (start
);
81 CALL (impl
, dst
, src
, n
);
83 HP_TIMING_BEST (best_time
, start
, stop
);
86 printf ("\t%zd", (size_t) best_time
);
91 do_test (size_t align1
, size_t align2
, size_t len1
, size_t len2
,
92 size_t n
, int max_char
)
98 if (align1
+ len1
>= page_size
)
100 if (align1
+ n
> page_size
)
103 if (align2
+ len1
+ len2
>= page_size
)
105 if (align2
+ len1
+ n
> page_size
)
107 s1
= (char *) (buf1
+ align1
);
108 s2
= (char *) (buf2
+ align2
);
110 for (i
= 0; i
< len1
; ++i
)
111 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
114 for (i
= 0; i
< len2
; i
++)
115 s2
[i
] = 32 + 23 * i
% (max_char
- 32);
118 printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
119 len1
, len2
, align1
, align2
, n
);
121 FOR_EACH_IMPL (impl
, 0)
124 do_one_test (impl
, s2
, s1
, n
);
132 do_random_tests (void)
134 size_t i
, j
, n
, align1
, align2
, len1
, len2
, N
;
135 unsigned char *p1
= buf1
+ page_size
- 512;
136 unsigned char *p2
= buf2
+ page_size
- 512;
138 fprintf (stdout
, "Number of iterations in random test = %zd\n",
140 for (n
= 0; n
< ITERATIONS
; n
++)
143 align1
= random () & 31;
145 align2
= random () & 31;
147 align2
= align1
+ (random () & 24);
148 len1
= random () & 511;
149 if (len1
+ align2
> 512)
150 len2
= random () & 7;
152 len2
= (512 - len1
- align2
) * (random () & (1024 * 1024 - 1))
155 if (align2
+ len2
> j
)
158 len1
= 510 - j
- (random () & 7);
161 if (align1
+ len1
< 512 - 8)
163 j
= 510 - align1
- len1
- (random () & 31);
164 if (j
> 0 && j
< 512)
167 j
= len1
+ align1
+ 64;
170 for (i
= 0; i
< j
; i
++)
172 if (i
== len1
+ align1
)
176 p1
[i
] = random () & 255;
177 if (i
>= align1
&& i
< len1
+ align1
&& !p1
[i
])
178 p1
[i
] = (random () & 127) + 3;
181 for (i
= 0; i
< len2
; i
++)
183 buf1
[i
] = random () & 255;
185 buf1
[i
] = (random () & 127) + 3;
189 FOR_EACH_IMPL (impl
, 1)
191 memset (p2
- 64, '\1', align2
+ 64);
192 memset (p2
+ align2
+ len2
+ 1, '\1', 512 - align2
- len2
- 1);
193 memcpy (p2
+ align2
, buf1
, len2
+ 1);
194 res
= (unsigned char *) CALL (impl
, (char *) (p2
+ align2
),
195 (char *) (p1
+ align1
), N
);
196 if (res
!= p2
+ align2
)
198 error (0, 0, "Iteration %zd - wrong result in function %s "
199 "(%zd, %zd, %zd, %zd, %zd) %p != %p",
200 n
, impl
->name
, align1
, align2
, len1
, len2
, N
,
204 for (j
= 0; j
< align2
+ 64; ++j
)
206 if (p2
[j
- 64] != '\1')
208 error (0, 0, "Iteration %zd - garbage before dst, %s "
209 "%zd, %zd, %zd, %zd, %zd)",
210 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
215 if (memcmp (p2
+ align2
, buf1
, len2
))
217 error (0, 0, "Iteration %zd - garbage in string before, %s "
218 "(%zd, %zd, %zd, %zd, %zd)",
219 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
224 j
= align2
+ N
+ 1 + len2
;
226 j
= align2
+ len1
+ 1 + len2
;
231 error (0, 0, "Iteration %zd - garbage after, %s "
232 "(%zd, %zd, %zd, %zd, %zd)",
233 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
240 if (p2
[align2
+ N
+ len2
] != '\0')
242 error (0, 0, "Iteration %zd - there is no zero at the "
243 "end of output string, %s (%zd, %zd, %zd, %zd, %zd)",
244 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
248 if (memcmp (p1
+ align1
, p2
+ align2
+ len2
,
249 (len1
+ 1) > N
? N
: len1
+ 1))
251 error (0, 0, "Iteration %zd - different strings, %s "
252 "(%zd, %zd, %zd, %zd, %zd)",
253 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
268 FOR_EACH_IMPL (impl
, 0)
269 printf ("\t%s", impl
->name
);
272 for (n
= 2; n
<= 2048; n
*=4)
274 do_test (0, 2, 2, 2, n
, 127);
275 do_test (0, 0, 4, 4, n
, 127);
276 do_test (4, 0, 4, 4, n
, 255);
277 do_test (0, 0, 8, 8, n
, 127);
278 do_test (0, 8, 8, 8, n
, 127);
280 for (i
= 1; i
< 8; ++i
)
282 do_test (0, 0, 8 << i
, 8 << i
, n
, 127);
283 do_test (8 - i
, 2 * i
, 8 << i
, 8 << i
, n
, 127);
284 do_test (0, 0, 8 << i
, 2 << i
, n
, 127);
285 do_test (8 - i
, 2 * i
, 8 << i
, 2 << i
, n
, 127);
288 for (i
= 1; i
< 8; ++i
)
290 do_test (i
, 2 * i
, 8 << i
, 1, n
, 127);
291 do_test (2 * i
, i
, 8 << i
, 1, n
, 255);
292 do_test (i
, i
, 8 << i
, 10, n
, 127);