]> git.ipfire.org Git - thirdparty/glibc.git/blame - string/test-strcpy.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / string / test-strcpy.c
CommitLineData
58ef9ef7 1/* Test and measure strcpy functions.
bfff8b1b 2 Copyright (C) 1999-2017 Free Software Foundation, Inc.
58ef9ef7
RM
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5b330a2d 5 Added wcscpy support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, 2011
58ef9ef7
RM
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
58ef9ef7 20
5b330a2d
UD
21#ifdef WIDE
22# include <wchar.h>
23# define CHAR wchar_t
24# define UCHAR wchar_t
df78418a 25# define sfmt "ls"
5b330a2d
UD
26# define BIG_CHAR WCHAR_MAX
27# define SMALL_CHAR 1273
28# define STRCMP wcscmp
29# define MEMCMP wmemcmp
30# define MEMSET wmemset
31#else
32# define CHAR char
33# define UCHAR unsigned char
df78418a 34# define sfmt "s"
5b330a2d
UD
35# define BIG_CHAR CHAR_MAX
36# define SMALL_CHAR 127
37# define STRCMP strcmp
38# define MEMCMP memcmp
39# define MEMSET memset
40#endif
41
58ef9ef7
RM
42#ifndef STRCPY_RESULT
43# define STRCPY_RESULT(dst, len) dst
44# define TEST_MAIN
69f07e5f
L
45# ifndef WIDE
46# define TEST_NAME "strcpy"
47# else
48# define TEST_NAME "wcscpy"
49# endif
58ef9ef7 50# include "test-string.h"
5b330a2d
UD
51# ifndef WIDE
52# define SIMPLE_STRCPY simple_strcpy
53# define STRCPY strcpy
54# else
55# define SIMPLE_STRCPY simple_wcscpy
56# define STRCPY wcscpy
57# endif
58ef9ef7 58
5b330a2d 59CHAR *SIMPLE_STRCPY (CHAR *, const CHAR *);
58ef9ef7 60
5b330a2d
UD
61IMPL (SIMPLE_STRCPY, 0)
62IMPL (STRCPY, 1)
58ef9ef7 63
5b330a2d
UD
64CHAR *
65SIMPLE_STRCPY (CHAR *dst, const CHAR *src)
58ef9ef7 66{
5b330a2d 67 CHAR *ret = dst;
58ef9ef7
RM
68 while ((*dst++ = *src++) != '\0');
69 return ret;
70}
71#endif
72
5b330a2d 73typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
58ef9ef7
RM
74
75static void
5b330a2d 76do_one_test (impl_t *impl, CHAR *dst, const CHAR *src,
58ef9ef7
RM
77 size_t len __attribute__((unused)))
78{
79 if (CALL (impl, dst, src) != STRCPY_RESULT (dst, len))
80 {
81 error (0, 0, "Wrong result in function %s %p %p", impl->name,
82 CALL (impl, dst, src), STRCPY_RESULT (dst, len));
83 ret = 1;
84 return;
85 }
86
5b330a2d 87 if (STRCMP (dst, src) != 0)
58ef9ef7 88 {
df78418a
UD
89 error (0, 0,
90 "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
58ef9ef7
RM
91 impl->name, dst, src);
92 ret = 1;
93 return;
94 }
58ef9ef7
RM
95}
96
97static void
98do_test (size_t align1, size_t align2, size_t len, int max_char)
99{
100 size_t i;
5b330a2d
UD
101 CHAR *s1, *s2;
102/* For wcscpy: align1 and align2 here mean alignment not in bytes,
103 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
104 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
58ef9ef7 105 align1 &= 7;
5b330a2d 106 if ((align1 + len) * sizeof(CHAR) >= page_size)
58ef9ef7
RM
107 return;
108
109 align2 &= 7;
5b330a2d 110 if ((align2 + len) * sizeof(CHAR) >= page_size)
58ef9ef7
RM
111 return;
112
5b330a2d
UD
113 s1 = (CHAR *) (buf1) + align1;
114 s2 = (CHAR *) (buf2) + align2;
58ef9ef7
RM
115
116 for (i = 0; i < len; i++)
117 s1[i] = 32 + 23 * i % (max_char - 32);
118 s1[len] = 0;
119
58ef9ef7
RM
120 FOR_EACH_IMPL (impl, 0)
121 do_one_test (impl, s2, s1, len);
58ef9ef7
RM
122}
123
124static void
125do_random_tests (void)
126{
127 size_t i, j, n, align1, align2, len;
5b330a2d
UD
128 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
129 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
130 UCHAR *res;
58ef9ef7
RM
131
132 for (n = 0; n < ITERATIONS; n++)
133 {
a2d18b64
UD
134 /* For wcsrchr: align1 and align2 here mean align not in bytes,
135 but in wchar_ts, in bytes it will equal to align * (sizeof
136 (wchar_t)). For strrchr we need to check all alignments from
137 0 to 63 since some assembly implementations have separate
138 prolog for alignments more 48. */
5b330a2d
UD
139
140 align1 = random () & (63 / sizeof(CHAR));
58ef9ef7 141 if (random () & 1)
5b330a2d 142 align2 = random () & (63 / sizeof(CHAR));
58ef9ef7
RM
143 else
144 align2 = align1 + (random () & 24);
145 len = random () & 511;
146 j = align1;
147 if (align2 > j)
148 j = align2;
149 if (len + j >= 511)
150 len = 510 - j - (random () & 7);
151 j = len + align1 + 64;
152 if (j > 512)
153 j = 512;
154 for (i = 0; i < j; i++)
155 {
156 if (i == len + align1)
157 p1[i] = 0;
158 else
159 {
5b330a2d 160 p1[i] = random () & BIG_CHAR;
58ef9ef7 161 if (i >= align1 && i < len + align1 && !p1[i])
5b330a2d 162 p1[i] = (random () & SMALL_CHAR) + 3;
58ef9ef7
RM
163 }
164 }
165
166 FOR_EACH_IMPL (impl, 1)
167 {
5b330a2d
UD
168 MEMSET (p2 - 64, '\1', 512 + 64);
169 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2), (CHAR *) (p1 + align1));
58ef9ef7
RM
170 if (res != STRCPY_RESULT (p2 + align2, len))
171 {
172 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
173 n, impl->name, align1, align2, len, res,
174 STRCPY_RESULT (p2 + align2, len));
175 ret = 1;
176 }
177 for (j = 0; j < align2 + 64; ++j)
178 {
179 if (p2[j - 64] != '\1')
180 {
181 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
182 n, impl->name, align1, align2, len);
183 ret = 1;
184 break;
185 }
186 }
187 for (j = align2 + len + 1; j < 512; ++j)
188 {
189 if (p2[j] != '\1')
190 {
191 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
192 n, impl->name, align1, align2, len);
193 ret = 1;
194 break;
195 }
196 }
5b330a2d 197 if (MEMCMP (p1 + align1, p2 + align2, len + 1))
58ef9ef7
RM
198 {
199 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
200 n, impl->name, align1, align2, len);
201 ret = 1;
202 }
203 }
204 }
205}
206
207int
208test_main (void)
209{
210 size_t i;
211
212 test_init ();
213
214 printf ("%23s", "");
215 FOR_EACH_IMPL (impl, 0)
216 printf ("\t%s", impl->name);
217 putchar ('\n');
218
219 for (i = 0; i < 16; ++i)
220 {
5b330a2d
UD
221 do_test (0, 0, i, SMALL_CHAR);
222 do_test (0, 0, i, BIG_CHAR);
223 do_test (0, i, i, SMALL_CHAR);
224 do_test (i, 0, i, BIG_CHAR);
58ef9ef7
RM
225 }
226
227 for (i = 1; i < 8; ++i)
228 {
5b330a2d
UD
229 do_test (0, 0, 8 << i, SMALL_CHAR);
230 do_test (8 - i, 2 * i, 8 << i, SMALL_CHAR);
58ef9ef7
RM
231 }
232
233 for (i = 1; i < 8; ++i)
234 {
5b330a2d
UD
235 do_test (i, 2 * i, 8 << i, SMALL_CHAR);
236 do_test (2 * i, i, 8 << i, BIG_CHAR);
237 do_test (i, i, 8 << i, SMALL_CHAR);
238 do_test (i, i, 8 << i, BIG_CHAR);
58ef9ef7
RM
239 }
240
241 do_random_tests ();
242 return ret;
243}
244
245#include "../test-skeleton.c"