1 /* Test strcat functions.
2 Copyright (C) 1999-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <https://www.gnu.org/licenses/>. */
21 # define TEST_NAME "strcat"
23 # define TEST_NAME "wcscat"
25 #include "test-string.h"
28 # define STRCAT strcat
30 # define UCHAR unsigned char
32 # define SIMPLE_STRCAT simple_strcat
33 # define STRLEN strlen
34 # define STRCMP strcmp
35 # define MEMSET memset
36 # define MEMCPY memcpy
37 # define MEMCMP memcmp
38 # define BIG_CHAR CHAR_MAX
39 # define SMALL_CHAR 127
42 # define STRCAT wcscat
44 # define UCHAR wchar_t
46 # define SIMPLE_STRCAT simple_wcscat
47 # define STRLEN wcslen
48 # define STRCMP wcscmp
49 # define MEMSET wmemset
50 # define MEMCPY wmemcpy
51 # define MEMCMP wmemcmp
52 # define BIG_CHAR WCHAR_MAX
53 # define SMALL_CHAR 1273
56 typedef CHAR
*(*proto_t
) (CHAR
*, const CHAR
*);
57 CHAR
*SIMPLE_STRCAT (CHAR
*, const CHAR
*);
59 IMPL (SIMPLE_STRCAT
, 0)
63 SIMPLE_STRCAT (CHAR
*dst
, const CHAR
*src
)
66 while (*dst
++ != '\0');
68 while ((*dst
++ = *src
++) != '\0');
73 do_one_test (impl_t
*impl
, CHAR
*dst
, const CHAR
*src
)
75 size_t k
= STRLEN (dst
);
76 if (CALL (impl
, dst
, src
) != dst
)
78 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
79 CALL (impl
, dst
, src
), dst
);
84 if (STRCMP (dst
+ k
, src
) != 0)
86 error (0, 0, "Wrong result in function %s dst \"%" sfmt
"\" src \"%" sfmt
"\"",
87 impl
->name
, dst
, src
);
94 do_test (size_t align1
, size_t align2
, size_t len1
, size_t len2
, int max_char
)
100 if ((align1
+ len1
) * sizeof (CHAR
) >= page_size
)
104 if ((align2
+ len1
+ len2
) * sizeof (CHAR
) >= 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);
117 FOR_EACH_IMPL (impl
, 0)
120 do_one_test (impl
, s2
, s1
);
125 do_random_tests (void)
127 size_t i
, j
, n
, align1
, align2
, len1
, len2
;
128 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
) - 512;
129 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
) - 512;
130 UCHAR
*p3
= (UCHAR
*) buf1
;
133 for (n
= 0; n
< ITERATIONS
; n
++)
135 align1
= random () & 31;
137 align2
= random () & 31;
139 align2
= align1
+ (random () & 24);
140 len1
= random () & 511;
141 if (len1
+ align2
> 512)
142 len2
= random () & 7;
144 len2
= (512 - len1
- align2
) * (random () & (1024 * 1024 - 1))
147 if (align2
+ len2
> j
)
150 len1
= 510 - j
- (random () & 7);
153 if (align1
+ len1
< 512 - 8)
155 j
= 510 - align1
- len1
- (random () & 31);
156 if (j
> 0 && j
< 512)
159 j
= len1
+ align1
+ 64;
162 for (i
= 0; i
< j
; i
++)
164 if (i
== len1
+ align1
)
168 p1
[i
] = random () & BIG_CHAR
;
169 if (i
>= align1
&& i
< len1
+ align1
&& !p1
[i
])
170 p1
[i
] = (random () & SMALL_CHAR
) + 3;
173 for (i
= 0; i
< len2
; i
++)
175 p3
[i
] = random () & BIG_CHAR
;
177 p3
[i
] = (random () & SMALL_CHAR
) + 3;
181 FOR_EACH_IMPL (impl
, 1)
183 MEMSET (p2
- 64, '\1', align2
+ 64);
184 MEMSET (p2
+ align2
+ len2
+ 1, '\1', 512 - align2
- len2
- 1);
185 MEMCPY (p2
+ align2
, p3
, len2
+ 1);
186 res
= (UCHAR
*) CALL (impl
, (CHAR
*) (p2
+ align2
),
187 (CHAR
*) (p1
+ align1
));
188 if (res
!= p2
+ align2
)
190 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd %zd) %p != %p",
191 n
, impl
->name
, align1
, align2
, len1
, len2
, res
,
195 for (j
= 0; j
< align2
+ 64; ++j
)
197 if (p2
[j
- 64] != '\1')
199 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd, %zd)",
200 n
, impl
->name
, align1
, align2
, len1
, len2
);
205 if (MEMCMP (p2
+ align2
, p3
, len2
))
207 error (0, 0, "Iteration %zd - garbage in string before, %s (%zd, %zd, %zd, %zd)",
208 n
, impl
->name
, align1
, align2
, len1
, len2
);
211 for (j
= align2
+ len1
+ len2
+ 1; j
< 512; ++j
)
215 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd, %zd)",
216 n
, impl
->name
, align1
, align2
, len1
, len2
);
221 if (MEMCMP (p1
+ align1
, p2
+ align2
+ len2
, len1
+ 1))
223 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd, %zd)",
224 n
, impl
->name
, align1
, align2
, len1
, len2
);
239 FOR_EACH_IMPL (impl
, 0)
240 printf ("\t%s", impl
->name
);
243 for (i
= 0; i
< 16; ++i
)
245 do_test (0, 0, i
, i
, SMALL_CHAR
);
246 do_test (0, 0, i
, i
, BIG_CHAR
);
247 do_test (0, i
, i
, i
, SMALL_CHAR
);
248 do_test (i
, 0, i
, i
, BIG_CHAR
);
251 for (i
= 1; i
< 8; ++i
)
253 do_test (0, 0, 8 << i
, 8 << i
, SMALL_CHAR
);
254 do_test (8 - i
, 2 * i
, 8 << i
, 8 << i
, SMALL_CHAR
);
255 do_test (0, 0, 8 << i
, 2 << i
, SMALL_CHAR
);
256 do_test (8 - i
, 2 * i
, 8 << i
, 2 << i
, SMALL_CHAR
);
259 for (i
= 1; i
< 8; ++i
)
261 do_test (i
, 2 * i
, 8 << i
, 1, SMALL_CHAR
);
262 do_test (2 * i
, i
, 8 << i
, 1, BIG_CHAR
);
263 do_test (i
, i
, 8 << i
, 10, SMALL_CHAR
);
264 do_test (i
, i
, 8 << i
, 10, BIG_CHAR
);
271 #include <support/test-driver.c>