1 /* Test strcat functions.
2 Copyright (C) 1999-2022 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
*);
60 /* Naive implementation to verify results. */
62 SIMPLE_STRCAT (CHAR
*dst
, const CHAR
*src
)
65 while (*dst
++ != '\0');
67 while ((*dst
++ = *src
++) != '\0');
72 do_one_test (impl_t
*impl
, CHAR
*dst
, const CHAR
*src
)
74 size_t k
= STRLEN (dst
);
75 if (CALL (impl
, dst
, src
) != dst
)
77 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
78 CALL (impl
, dst
, src
), dst
);
83 if (STRCMP (dst
+ k
, src
) != 0)
85 error (0, 0, "Wrong result in function %s dst \"%" sfmt
"\" src \"%" sfmt
"\"",
86 impl
->name
, dst
, src
);
93 do_test (size_t align1
, size_t align2
, size_t len1
, size_t len2
, int max_char
)
99 if ((align1
+ len1
) * sizeof (CHAR
) >= page_size
)
103 if ((align2
+ len1
+ len2
) * sizeof (CHAR
) >= 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);
116 FOR_EACH_IMPL (impl
, 0)
119 do_one_test (impl
, s2
, s1
);
124 do_random_tests (void)
126 size_t i
, j
, n
, align1
, align2
, len1
, len2
;
127 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
) - 512;
128 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
) - 512;
129 UCHAR
*p3
= (UCHAR
*) buf1
;
132 for (n
= 0; n
< ITERATIONS
; n
++)
134 align1
= random () & 31;
136 align2
= random () & 31;
138 align2
= align1
+ (random () & 24);
139 len1
= random () & 511;
140 if (len1
+ align2
> 512)
141 len2
= random () & 7;
143 len2
= (512 - len1
- align2
) * (random () & (1024 * 1024 - 1))
146 if (align2
+ len2
> j
)
149 len1
= 510 - j
- (random () & 7);
152 if (align1
+ len1
< 512 - 8)
154 j
= 510 - align1
- len1
- (random () & 31);
155 if (j
> 0 && j
< 512)
158 j
= len1
+ align1
+ 64;
161 for (i
= 0; i
< j
; i
++)
163 if (i
== len1
+ align1
)
167 p1
[i
] = random () & BIG_CHAR
;
168 if (i
>= align1
&& i
< len1
+ align1
&& !p1
[i
])
169 p1
[i
] = (random () & SMALL_CHAR
) + 3;
172 for (i
= 0; i
< len2
; i
++)
174 p3
[i
] = random () & BIG_CHAR
;
176 p3
[i
] = (random () & SMALL_CHAR
) + 3;
180 FOR_EACH_IMPL (impl
, 1)
182 MEMSET (p2
- 64, '\1', align2
+ 64);
183 MEMSET (p2
+ align2
+ len2
+ 1, '\1', 512 - align2
- len2
- 1);
184 MEMCPY (p2
+ align2
, p3
, len2
+ 1);
185 res
= (UCHAR
*) CALL (impl
, (CHAR
*) (p2
+ align2
),
186 (CHAR
*) (p1
+ align1
));
187 if (res
!= p2
+ align2
)
189 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd %zd) %p != %p",
190 n
, impl
->name
, align1
, align2
, len1
, len2
, res
,
194 for (j
= 0; j
< align2
+ 64; ++j
)
196 if (p2
[j
- 64] != '\1')
198 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd, %zd)",
199 n
, impl
->name
, align1
, align2
, len1
, len2
);
204 if (MEMCMP (p2
+ align2
, p3
, len2
))
206 error (0, 0, "Iteration %zd - garbage in string before, %s (%zd, %zd, %zd, %zd)",
207 n
, impl
->name
, align1
, align2
, len1
, len2
);
210 for (j
= align2
+ len1
+ len2
+ 1; j
< 512; ++j
)
214 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd, %zd)",
215 n
, impl
->name
, align1
, align2
, len1
, len2
);
220 if (MEMCMP (p1
+ align1
, p2
+ align2
+ len2
, len1
+ 1))
222 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd, %zd)",
223 n
, impl
->name
, align1
, align2
, len1
, len2
);
238 FOR_EACH_IMPL (impl
, 0)
239 printf ("\t%s", impl
->name
);
242 for (i
= 0; i
< 16; ++i
)
244 do_test (0, 0, i
, i
, SMALL_CHAR
);
245 do_test (0, 0, i
, i
, BIG_CHAR
);
246 do_test (0, i
, i
, i
, SMALL_CHAR
);
247 do_test (i
, 0, i
, i
, BIG_CHAR
);
250 for (i
= 1; i
< 8; ++i
)
252 do_test (0, 0, 8 << i
, 8 << i
, SMALL_CHAR
);
253 do_test (8 - i
, 2 * i
, 8 << i
, 8 << i
, SMALL_CHAR
);
254 do_test (0, 0, 8 << i
, 2 << i
, SMALL_CHAR
);
255 do_test (8 - i
, 2 * i
, 8 << i
, 2 << i
, SMALL_CHAR
);
258 for (i
= 1; i
< 8; ++i
)
260 do_test (i
, 2 * i
, 8 << i
, 1, SMALL_CHAR
);
261 do_test (2 * i
, i
, 8 << i
, 1, BIG_CHAR
);
262 do_test (i
, i
, 8 << i
, 10, SMALL_CHAR
);
263 do_test (i
, i
, 8 << i
, 10, BIG_CHAR
);
270 #include <support/test-driver.c>