1 /* Test strcat functions.
2 Copyright (C) 1999-2021 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 <https://www.gnu.org/licenses/>. */
22 # define TEST_NAME "strcat"
24 # define TEST_NAME "wcscat"
26 #include "test-string.h"
29 # define STRCAT strcat
31 # define UCHAR unsigned char
33 # define SIMPLE_STRCAT simple_strcat
34 # define STRLEN strlen
35 # define STRCMP strcmp
36 # define MEMSET memset
37 # define MEMCPY memcpy
38 # define MEMCMP memcmp
39 # define BIG_CHAR CHAR_MAX
40 # define SMALL_CHAR 127
43 # define STRCAT wcscat
45 # define UCHAR wchar_t
47 # define SIMPLE_STRCAT simple_wcscat
48 # define STRLEN wcslen
49 # define STRCMP wcscmp
50 # define MEMSET wmemset
51 # define MEMCPY wmemcpy
52 # define MEMCMP wmemcmp
53 # define BIG_CHAR WCHAR_MAX
54 # define SMALL_CHAR 1273
57 typedef CHAR
*(*proto_t
) (CHAR
*, const CHAR
*);
58 CHAR
*SIMPLE_STRCAT (CHAR
*, const CHAR
*);
60 IMPL (SIMPLE_STRCAT
, 0)
64 SIMPLE_STRCAT (CHAR
*dst
, const CHAR
*src
)
67 while (*dst
++ != '\0');
69 while ((*dst
++ = *src
++) != '\0');
74 do_one_test (impl_t
*impl
, CHAR
*dst
, const CHAR
*src
)
76 size_t k
= STRLEN (dst
);
77 if (CALL (impl
, dst
, src
) != dst
)
79 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
80 CALL (impl
, dst
, src
), dst
);
85 if (STRCMP (dst
+ k
, src
) != 0)
87 error (0, 0, "Wrong result in function %s dst \"%" sfmt
"\" src \"%" sfmt
"\"",
88 impl
->name
, dst
, src
);
95 do_test (size_t align1
, size_t align2
, size_t len1
, size_t len2
, int max_char
)
101 if ((align1
+ len1
) * sizeof (CHAR
) >= page_size
)
105 if ((align2
+ len1
+ len2
) * sizeof (CHAR
) >= page_size
)
108 s1
= (CHAR
*) (buf1
) + align1
;
109 s2
= (CHAR
*) (buf2
) + align2
;
111 for (i
= 0; i
< len1
; ++i
)
112 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
115 for (i
= 0; i
< len2
; i
++)
116 s2
[i
] = 32 + 23 * i
% (max_char
- 32);
118 FOR_EACH_IMPL (impl
, 0)
121 do_one_test (impl
, s2
, s1
);
126 do_random_tests (void)
128 size_t i
, j
, n
, align1
, align2
, len1
, len2
;
129 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
) - 512;
130 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
) - 512;
131 UCHAR
*p3
= (UCHAR
*) buf1
;
134 for (n
= 0; n
< ITERATIONS
; n
++)
136 align1
= random () & 31;
138 align2
= random () & 31;
140 align2
= align1
+ (random () & 24);
141 len1
= random () & 511;
142 if (len1
+ align2
> 512)
143 len2
= random () & 7;
145 len2
= (512 - len1
- align2
) * (random () & (1024 * 1024 - 1))
148 if (align2
+ len2
> j
)
151 len1
= 510 - j
- (random () & 7);
154 if (align1
+ len1
< 512 - 8)
156 j
= 510 - align1
- len1
- (random () & 31);
157 if (j
> 0 && j
< 512)
160 j
= len1
+ align1
+ 64;
163 for (i
= 0; i
< j
; i
++)
165 if (i
== len1
+ align1
)
169 p1
[i
] = random () & BIG_CHAR
;
170 if (i
>= align1
&& i
< len1
+ align1
&& !p1
[i
])
171 p1
[i
] = (random () & SMALL_CHAR
) + 3;
174 for (i
= 0; i
< len2
; i
++)
176 p3
[i
] = random () & BIG_CHAR
;
178 p3
[i
] = (random () & SMALL_CHAR
) + 3;
182 FOR_EACH_IMPL (impl
, 1)
184 MEMSET (p2
- 64, '\1', align2
+ 64);
185 MEMSET (p2
+ align2
+ len2
+ 1, '\1', 512 - align2
- len2
- 1);
186 MEMCPY (p2
+ align2
, p3
, len2
+ 1);
187 res
= (UCHAR
*) CALL (impl
, (CHAR
*) (p2
+ align2
),
188 (CHAR
*) (p1
+ align1
));
189 if (res
!= p2
+ align2
)
191 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd %zd) %p != %p",
192 n
, impl
->name
, align1
, align2
, len1
, len2
, res
,
196 for (j
= 0; j
< align2
+ 64; ++j
)
198 if (p2
[j
- 64] != '\1')
200 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd, %zd)",
201 n
, impl
->name
, align1
, align2
, len1
, len2
);
206 if (MEMCMP (p2
+ align2
, p3
, len2
))
208 error (0, 0, "Iteration %zd - garbage in string before, %s (%zd, %zd, %zd, %zd)",
209 n
, impl
->name
, align1
, align2
, len1
, len2
);
212 for (j
= align2
+ len1
+ len2
+ 1; j
< 512; ++j
)
216 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd, %zd)",
217 n
, impl
->name
, align1
, align2
, len1
, len2
);
222 if (MEMCMP (p1
+ align1
, p2
+ align2
+ len2
, len1
+ 1))
224 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd, %zd)",
225 n
, impl
->name
, align1
, align2
, len1
, len2
);
240 FOR_EACH_IMPL (impl
, 0)
241 printf ("\t%s", impl
->name
);
244 for (i
= 0; i
< 16; ++i
)
246 do_test (0, 0, i
, i
, SMALL_CHAR
);
247 do_test (0, 0, i
, i
, BIG_CHAR
);
248 do_test (0, i
, i
, i
, SMALL_CHAR
);
249 do_test (i
, 0, i
, i
, BIG_CHAR
);
252 for (i
= 1; i
< 8; ++i
)
254 do_test (0, 0, 8 << i
, 8 << i
, SMALL_CHAR
);
255 do_test (8 - i
, 2 * i
, 8 << i
, 8 << i
, SMALL_CHAR
);
256 do_test (0, 0, 8 << i
, 2 << i
, SMALL_CHAR
);
257 do_test (8 - i
, 2 * i
, 8 << i
, 2 << i
, SMALL_CHAR
);
260 for (i
= 1; i
< 8; ++i
)
262 do_test (i
, 2 * i
, 8 << i
, 1, SMALL_CHAR
);
263 do_test (2 * i
, i
, 8 << i
, 1, BIG_CHAR
);
264 do_test (i
, i
, 8 << i
, 10, SMALL_CHAR
);
265 do_test (i
, i
, 8 << i
, 10, BIG_CHAR
);
272 #include <support/test-driver.c>