1 /* Test and measure strcpy 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/>. */
22 # define UCHAR wchar_t
24 # define BIG_CHAR WCHAR_MAX
25 # define SMALL_CHAR 1273
26 # define STRCMP wcscmp
27 # define MEMCMP wmemcmp
28 # define MEMSET wmemset
31 # define UCHAR unsigned char
33 # define BIG_CHAR CHAR_MAX
34 # define SMALL_CHAR 127
35 # define STRCMP strcmp
36 # define MEMCMP memcmp
37 # define MEMSET memset
41 # define STRCPY_RESULT(dst, len) dst
44 # define TEST_NAME "strcpy"
46 # define TEST_NAME "wcscpy"
48 # include "test-string.h"
50 # define SIMPLE_STRCPY simple_strcpy
51 # define STRCPY strcpy
53 # define SIMPLE_STRCPY simple_wcscpy
54 # define STRCPY wcscpy
57 CHAR
*SIMPLE_STRCPY (CHAR
*, const CHAR
*);
59 IMPL (SIMPLE_STRCPY
, 0)
63 SIMPLE_STRCPY (CHAR
*dst
, const CHAR
*src
)
66 while ((*dst
++ = *src
++) != '\0');
71 typedef CHAR
*(*proto_t
) (CHAR
*, const CHAR
*);
74 do_one_test (impl_t
*impl
, CHAR
*dst
, const CHAR
*src
,
75 size_t len
__attribute__((unused
)))
77 if (CALL (impl
, dst
, src
) != STRCPY_RESULT (dst
, len
))
79 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
80 CALL (impl
, dst
, src
), STRCPY_RESULT (dst
, len
));
85 if (STRCMP (dst
, src
) != 0)
88 "Wrong result in function %s dst \"%" sfmt
"\" src \"%" sfmt
"\"",
89 impl
->name
, dst
, src
);
96 do_test (size_t align1
, size_t align2
, size_t len
, int max_char
)
100 /* For wcscpy: align1 and align2 here mean alignment not in bytes,
101 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
102 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
104 if ((align1
+ len
) * sizeof (CHAR
) >= page_size
)
108 if ((align2
+ len
) * sizeof (CHAR
) >= page_size
)
111 s1
= (CHAR
*) (buf1
) + align1
;
112 s2
= (CHAR
*) (buf2
) + align2
;
114 for (i
= 0; i
< len
; i
++)
115 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
118 FOR_EACH_IMPL (impl
, 0)
119 do_one_test (impl
, s2
, s1
, len
);
123 do_random_tests (void)
125 size_t i
, j
, n
, align1
, align2
, len
;
126 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
) - 512;
127 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
) - 512;
130 for (n
= 0; n
< ITERATIONS
; n
++)
132 /* For wcsrchr: align1 and align2 here mean align not in bytes,
133 but in wchar_ts, in bytes it will equal to align * (sizeof
134 (wchar_t)). For strrchr we need to check all alignments from
135 0 to 63 since some assembly implementations have separate
136 prolog for alignments more 48. */
138 align1
= random () & (63 / sizeof (CHAR
));
140 align2
= random () & (63 / sizeof (CHAR
));
142 align2
= align1
+ (random () & 24);
143 len
= random () & 511;
148 len
= 510 - j
- (random () & 7);
149 j
= len
+ align1
+ 64;
152 for (i
= 0; i
< j
; i
++)
154 if (i
== len
+ align1
)
158 p1
[i
] = random () & BIG_CHAR
;
159 if (i
>= align1
&& i
< len
+ align1
&& !p1
[i
])
160 p1
[i
] = (random () & SMALL_CHAR
) + 3;
164 FOR_EACH_IMPL (impl
, 1)
166 MEMSET (p2
- 64, '\1', 512 + 64);
167 res
= (UCHAR
*) CALL (impl
, (CHAR
*) (p2
+ align2
), (CHAR
*) (p1
+ align1
));
168 if (res
!= STRCPY_RESULT (p2
+ align2
, len
))
170 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
171 n
, impl
->name
, align1
, align2
, len
, res
,
172 STRCPY_RESULT (p2
+ align2
, len
));
175 for (j
= 0; j
< align2
+ 64; ++j
)
177 if (p2
[j
- 64] != '\1')
179 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
180 n
, impl
->name
, align1
, align2
, len
);
185 for (j
= align2
+ len
+ 1; j
< 512; ++j
)
189 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
190 n
, impl
->name
, align1
, align2
, len
);
195 if (MEMCMP (p1
+ align1
, p2
+ align2
, len
+ 1))
197 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
198 n
, impl
->name
, align1
, align2
, len
);
213 FOR_EACH_IMPL (impl
, 0)
214 printf ("\t%s", impl
->name
);
217 for (i
= 0; i
< 16; ++i
)
219 do_test (0, 0, i
, SMALL_CHAR
);
220 do_test (0, 0, i
, BIG_CHAR
);
221 do_test (0, i
, i
, SMALL_CHAR
);
222 do_test (i
, 0, i
, BIG_CHAR
);
225 for (i
= 1; i
< 8; ++i
)
227 do_test (0, 0, 8 << i
, SMALL_CHAR
);
228 do_test (8 - i
, 2 * i
, 8 << i
, SMALL_CHAR
);
231 for (i
= 1; i
< 8; ++i
)
233 do_test (i
, 2 * i
, 8 << i
, SMALL_CHAR
);
234 do_test (2 * i
, i
, 8 << i
, BIG_CHAR
);
235 do_test (i
, i
, 8 << i
, SMALL_CHAR
);
236 do_test (i
, i
, 8 << i
, BIG_CHAR
);
243 #include <support/test-driver.c>