1 /* Test and measure strncat functions.
2 Copyright (C) 2011-2014 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",
73 do_test (size_t align1
, size_t align2
, size_t len1
, size_t len2
,
74 size_t n
, int max_char
)
80 if (align1
+ len1
>= page_size
)
82 if (align1
+ n
> page_size
)
85 if (align2
+ len1
+ len2
>= page_size
)
87 if (align2
+ len1
+ n
> page_size
)
89 s1
= (char *) (buf1
+ align1
);
90 s2
= (char *) (buf2
+ align2
);
92 for (i
= 0; i
< len1
; ++i
)
93 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
96 for (i
= 0; i
< len2
; i
++)
97 s2
[i
] = 32 + 23 * i
% (max_char
- 32);
99 FOR_EACH_IMPL (impl
, 0)
102 do_one_test (impl
, s2
, s1
, n
);
107 do_random_tests (void)
109 size_t i
, j
, n
, align1
, align2
, len1
, len2
, N
;
110 unsigned char *p1
= buf1
+ page_size
- 512;
111 unsigned char *p2
= buf2
+ page_size
- 512;
113 fprintf (stdout
, "Number of iterations in random test = %zd\n",
115 for (n
= 0; n
< ITERATIONS
; n
++)
118 align1
= random () & 31;
120 align2
= random () & 31;
122 align2
= align1
+ (random () & 24);
123 len1
= random () & 511;
124 if (len1
+ align2
> 512)
125 len2
= random () & 7;
127 len2
= (512 - len1
- align2
) * (random () & (1024 * 1024 - 1))
130 if (align2
+ len2
> j
)
133 len1
= 510 - j
- (random () & 7);
136 if (align1
+ len1
< 512 - 8)
138 j
= 510 - align1
- len1
- (random () & 31);
139 if (j
> 0 && j
< 512)
142 j
= len1
+ align1
+ 64;
145 for (i
= 0; i
< j
; i
++)
147 if (i
== len1
+ align1
)
151 p1
[i
] = random () & 255;
152 if (i
>= align1
&& i
< len1
+ align1
&& !p1
[i
])
153 p1
[i
] = (random () & 127) + 3;
156 for (i
= 0; i
< len2
; i
++)
158 buf1
[i
] = random () & 255;
160 buf1
[i
] = (random () & 127) + 3;
164 FOR_EACH_IMPL (impl
, 1)
166 memset (p2
- 64, '\1', align2
+ 64);
167 memset (p2
+ align2
+ len2
+ 1, '\1', 512 - align2
- len2
- 1);
168 memcpy (p2
+ align2
, buf1
, len2
+ 1);
169 res
= (unsigned char *) CALL (impl
, (char *) (p2
+ align2
),
170 (char *) (p1
+ align1
), N
);
171 if (res
!= p2
+ align2
)
173 error (0, 0, "Iteration %zd - wrong result in function %s "
174 "(%zd, %zd, %zd, %zd, %zd) %p != %p",
175 n
, impl
->name
, align1
, align2
, len1
, len2
, N
,
179 for (j
= 0; j
< align2
+ 64; ++j
)
181 if (p2
[j
- 64] != '\1')
183 error (0, 0, "Iteration %zd - garbage before dst, %s "
184 "%zd, %zd, %zd, %zd, %zd)",
185 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
190 if (memcmp (p2
+ align2
, buf1
, len2
))
192 error (0, 0, "Iteration %zd - garbage in string before, %s "
193 "(%zd, %zd, %zd, %zd, %zd)",
194 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
199 j
= align2
+ N
+ 1 + len2
;
201 j
= align2
+ len1
+ 1 + len2
;
206 error (0, 0, "Iteration %zd - garbage after, %s "
207 "(%zd, %zd, %zd, %zd, %zd)",
208 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
215 if (p2
[align2
+ N
+ len2
] != '\0')
217 error (0, 0, "Iteration %zd - there is no zero at the "
218 "end of output string, %s (%zd, %zd, %zd, %zd, %zd)",
219 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
223 if (memcmp (p1
+ align1
, p2
+ align2
+ len2
,
224 (len1
+ 1) > N
? N
: len1
+ 1))
226 error (0, 0, "Iteration %zd - different strings, %s "
227 "(%zd, %zd, %zd, %zd, %zd)",
228 n
, impl
->name
, align1
, align2
, len1
, len2
, N
);
243 FOR_EACH_IMPL (impl
, 0)
244 printf ("\t%s", impl
->name
);
247 for (n
= 2; n
<= 2048; n
*=4)
249 do_test (0, 2, 2, 2, n
, 127);
250 do_test (0, 0, 4, 4, n
, 127);
251 do_test (4, 0, 4, 4, n
, 255);
252 do_test (0, 0, 8, 8, n
, 127);
253 do_test (0, 8, 8, 8, n
, 127);
255 for (i
= 1; i
< 8; ++i
)
257 do_test (0, 0, 8 << i
, 8 << i
, n
, 127);
258 do_test (8 - i
, 2 * i
, 8 << i
, 8 << i
, n
, 127);
259 do_test (0, 0, 8 << i
, 2 << i
, n
, 127);
260 do_test (8 - i
, 2 * i
, 8 << i
, 2 << i
, n
, 127);
263 for (i
= 1; i
< 8; ++i
)
265 do_test (i
, 2 * i
, 8 << i
, 1, n
, 127);
266 do_test (2 * i
, i
, 8 << i
, 1, n
, 255);
267 do_test (i
, i
, 8 << i
, 10, n
, 127);