powerpc64: Add optimized chacha20
[glibc.git] / string / test-strcpy.c
blob7e7d10af634a8dfb0e546850f70d25a382fe4530
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/>. */
19 #ifdef WIDE
20 # include <wchar.h>
21 # define CHAR wchar_t
22 # define UCHAR wchar_t
23 # define sfmt "ls"
24 # define BIG_CHAR WCHAR_MAX
25 # define SMALL_CHAR 1273
26 # define STRCMP wcscmp
27 # define MEMCMP wmemcmp
28 # define MEMSET wmemset
29 #else
30 # define CHAR char
31 # define UCHAR unsigned char
32 # define sfmt "s"
33 # define BIG_CHAR CHAR_MAX
34 # define SMALL_CHAR 127
35 # define STRCMP strcmp
36 # define MEMCMP memcmp
37 # define MEMSET memset
38 #endif
40 #ifndef STRCPY_RESULT
41 # define STRCPY_RESULT(dst, len) dst
42 # define TEST_MAIN
43 # ifndef WIDE
44 # define TEST_NAME "strcpy"
45 # else
46 # define TEST_NAME "wcscpy"
47 # endif
48 # include "test-string.h"
49 # ifndef WIDE
50 # define SIMPLE_STRCPY simple_strcpy
51 # define STRCPY strcpy
52 # else
53 # define SIMPLE_STRCPY simple_wcscpy
54 # define STRCPY wcscpy
55 # endif
57 IMPL (STRCPY, 1)
59 /* Naive implementation to verify results. */
60 CHAR *
61 SIMPLE_STRCPY (CHAR *dst, const CHAR *src)
63 CHAR *ret = dst;
64 while ((*dst++ = *src++) != '\0');
65 return ret;
67 #endif
69 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
71 static void
72 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src,
73 size_t len __attribute__((unused)))
75 if (CALL (impl, dst, src) != STRCPY_RESULT (dst, len))
77 error (0, 0, "Wrong result in function %s %p %p", impl->name,
78 CALL (impl, dst, src), STRCPY_RESULT (dst, len));
79 ret = 1;
80 return;
83 if (STRCMP (dst, src) != 0)
85 error (0, 0,
86 "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
87 impl->name, dst, src);
88 ret = 1;
89 return;
93 static void
94 do_test (size_t align1, size_t align2, size_t len, int max_char)
96 size_t i;
97 CHAR *s1, *s2;
98 /* For wcscpy: align1 and align2 here mean alignment not in bytes,
99 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
100 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
101 align1 &= 7;
102 if ((align1 + len) * sizeof (CHAR) >= page_size)
103 return;
105 align2 &= 7;
106 if ((align2 + len) * sizeof (CHAR) >= page_size)
107 return;
109 s1 = (CHAR *) (buf1) + align1;
110 s2 = (CHAR *) (buf2) + align2;
112 for (i = 0; i < len; i++)
113 s1[i] = 32 + 23 * i % (max_char - 32);
114 s1[len] = 0;
116 FOR_EACH_IMPL (impl, 0)
117 do_one_test (impl, s2, s1, len);
120 static void
121 do_random_tests (void)
123 size_t i, j, n, align1, align2, len;
124 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
125 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
126 UCHAR *res;
128 for (n = 0; n < ITERATIONS; n++)
130 /* For wcsrchr: align1 and align2 here mean align not in bytes,
131 but in wchar_ts, in bytes it will equal to align * (sizeof
132 (wchar_t)). For strrchr we need to check all alignments from
133 0 to 63 since some assembly implementations have separate
134 prolog for alignments more 48. */
136 align1 = random () & (63 / sizeof (CHAR));
137 if (random () & 1)
138 align2 = random () & (63 / sizeof (CHAR));
139 else
140 align2 = align1 + (random () & 24);
141 len = random () & 511;
142 j = align1;
143 if (align2 > j)
144 j = align2;
145 if (len + j >= 511)
146 len = 510 - j - (random () & 7);
147 j = len + align1 + 64;
148 if (j > 512)
149 j = 512;
150 for (i = 0; i < j; i++)
152 if (i == len + align1)
153 p1[i] = 0;
154 else
156 p1[i] = random () & BIG_CHAR;
157 if (i >= align1 && i < len + align1 && !p1[i])
158 p1[i] = (random () & SMALL_CHAR) + 3;
162 FOR_EACH_IMPL (impl, 1)
164 MEMSET (p2 - 64, '\1', 512 + 64);
165 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2), (CHAR *) (p1 + align1));
166 if (res != STRCPY_RESULT (p2 + align2, len))
168 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
169 n, impl->name, align1, align2, len, res,
170 STRCPY_RESULT (p2 + align2, len));
171 ret = 1;
173 for (j = 0; j < align2 + 64; ++j)
175 if (p2[j - 64] != '\1')
177 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
178 n, impl->name, align1, align2, len);
179 ret = 1;
180 break;
183 for (j = align2 + len + 1; j < 512; ++j)
185 if (p2[j] != '\1')
187 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
188 n, impl->name, align1, align2, len);
189 ret = 1;
190 break;
193 if (MEMCMP (p1 + align1, p2 + align2, len + 1))
195 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
196 n, impl->name, align1, align2, len);
197 ret = 1;
204 test_main (void)
206 size_t i;
208 test_init ();
210 printf ("%23s", "");
211 FOR_EACH_IMPL (impl, 0)
212 printf ("\t%s", impl->name);
213 putchar ('\n');
215 for (i = 0; i < 16; ++i)
217 do_test (0, 0, i, SMALL_CHAR);
218 do_test (0, 0, i, BIG_CHAR);
219 do_test (0, i, i, SMALL_CHAR);
220 do_test (i, 0, i, BIG_CHAR);
223 for (i = 1; i < 8; ++i)
225 do_test (0, 0, 8 << i, SMALL_CHAR);
226 do_test (8 - i, 2 * i, 8 << i, SMALL_CHAR);
229 for (i = 1; i < 8; ++i)
231 do_test (i, 2 * i, 8 << i, SMALL_CHAR);
232 do_test (2 * i, i, 8 << i, BIG_CHAR);
233 do_test (i, i, 8 << i, SMALL_CHAR);
234 do_test (i, i, 8 << i, BIG_CHAR);
237 do_random_tests ();
238 return ret;
241 #include <support/test-driver.c>