]> git.ipfire.org Git - thirdparty/glibc.git/blob - string/test-strcpy.c
WP cleanup
[thirdparty/glibc.git] / string / test-strcpy.c
1 /* Test and measure strcpy functions.
2 Copyright (C) 1999, 2002, 2003, 2005, 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5 Added wcscpy support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, 2011
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
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
21
22 #ifdef WIDE
23 # include <wchar.h>
24 # define CHAR wchar_t
25 # define UCHAR wchar_t
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
34 # define BIG_CHAR CHAR_MAX
35 # define SMALL_CHAR 127
36 # define STRCMP strcmp
37 # define MEMCMP memcmp
38 # define MEMSET memset
39 #endif
40
41 #ifndef STRCPY_RESULT
42 # define STRCPY_RESULT(dst, len) dst
43 # define TEST_MAIN
44 # include "test-string.h"
45 # ifndef WIDE
46 # define SIMPLE_STRCPY simple_strcpy
47 # define STRCPY strcpy
48 # else
49 # define SIMPLE_STRCPY simple_wcscpy
50 # define STRCPY wcscpy
51 # endif
52
53 CHAR *SIMPLE_STRCPY (CHAR *, const CHAR *);
54
55 IMPL (SIMPLE_STRCPY, 0)
56 IMPL (STRCPY, 1)
57
58 CHAR *
59 SIMPLE_STRCPY (CHAR *dst, const CHAR *src)
60 {
61 CHAR *ret = dst;
62 while ((*dst++ = *src++) != '\0');
63 return ret;
64 }
65 #endif
66
67 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
68
69 static void
70 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src,
71 size_t len __attribute__((unused)))
72 {
73 if (CALL (impl, dst, src) != STRCPY_RESULT (dst, len))
74 {
75 error (0, 0, "Wrong result in function %s %p %p", impl->name,
76 CALL (impl, dst, src), STRCPY_RESULT (dst, len));
77 ret = 1;
78 return;
79 }
80
81 if (STRCMP (dst, src) != 0)
82 {
83 error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
84 impl->name, dst, src);
85 ret = 1;
86 return;
87 }
88
89 if (HP_TIMING_AVAIL)
90 {
91 hp_timing_t start __attribute ((unused));
92 hp_timing_t stop __attribute ((unused));;
93 hp_timing_t best_time = ~ (hp_timing_t) 0;
94 size_t i;
95
96 for (i = 0; i < 32; ++i)
97 {
98 HP_TIMING_NOW (start);
99 CALL (impl, dst, src);
100 HP_TIMING_NOW (stop);
101 HP_TIMING_BEST (best_time, start, stop);
102 }
103
104 printf ("\t%zd", (size_t) best_time);
105 }
106 }
107
108 static void
109 do_test (size_t align1, size_t align2, size_t len, int max_char)
110 {
111 size_t i;
112 CHAR *s1, *s2;
113 /* For wcscpy: align1 and align2 here mean alignment not in bytes,
114 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
115 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
116 align1 &= 7;
117 if ((align1 + len) * sizeof(CHAR) >= page_size)
118 return;
119
120 align2 &= 7;
121 if ((align2 + len) * sizeof(CHAR) >= page_size)
122 return;
123
124 s1 = (CHAR *) (buf1) + align1;
125 s2 = (CHAR *) (buf2) + align2;
126
127 for (i = 0; i < len; i++)
128 s1[i] = 32 + 23 * i % (max_char - 32);
129 s1[len] = 0;
130
131 if (HP_TIMING_AVAIL)
132 printf ("Length %4zd, alignments in bytes %2zd/%2zd:", len, align1 * sizeof(CHAR), align2 * sizeof(CHAR));
133
134 FOR_EACH_IMPL (impl, 0)
135 do_one_test (impl, s2, s1, len);
136
137 if (HP_TIMING_AVAIL)
138 putchar ('\n');
139 }
140
141 static void
142 do_random_tests (void)
143 {
144 size_t i, j, n, align1, align2, len;
145 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
146 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
147 UCHAR *res;
148
149 for (n = 0; n < ITERATIONS; n++)
150 {
151 /* For wcsrchr: align1 and align2 here mean align not in bytes,
152 but in wchar_ts, in bytes it will equal to align * (sizeof
153 (wchar_t)). For strrchr we need to check all alignments from
154 0 to 63 since some assembly implementations have separate
155 prolog for alignments more 48. */
156
157 align1 = random () & (63 / sizeof(CHAR));
158 if (random () & 1)
159 align2 = random () & (63 / sizeof(CHAR));
160 else
161 align2 = align1 + (random () & 24);
162 len = random () & 511;
163 j = align1;
164 if (align2 > j)
165 j = align2;
166 if (len + j >= 511)
167 len = 510 - j - (random () & 7);
168 j = len + align1 + 64;
169 if (j > 512)
170 j = 512;
171 for (i = 0; i < j; i++)
172 {
173 if (i == len + align1)
174 p1[i] = 0;
175 else
176 {
177 p1[i] = random () & BIG_CHAR;
178 if (i >= align1 && i < len + align1 && !p1[i])
179 p1[i] = (random () & SMALL_CHAR) + 3;
180 }
181 }
182
183 FOR_EACH_IMPL (impl, 1)
184 {
185 MEMSET (p2 - 64, '\1', 512 + 64);
186 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2), (CHAR *) (p1 + align1));
187 if (res != STRCPY_RESULT (p2 + align2, len))
188 {
189 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
190 n, impl->name, align1, align2, len, res,
191 STRCPY_RESULT (p2 + align2, len));
192 ret = 1;
193 }
194 for (j = 0; j < align2 + 64; ++j)
195 {
196 if (p2[j - 64] != '\1')
197 {
198 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
199 n, impl->name, align1, align2, len);
200 ret = 1;
201 break;
202 }
203 }
204 for (j = align2 + len + 1; j < 512; ++j)
205 {
206 if (p2[j] != '\1')
207 {
208 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
209 n, impl->name, align1, align2, len);
210 ret = 1;
211 break;
212 }
213 }
214 if (MEMCMP (p1 + align1, p2 + align2, len + 1))
215 {
216 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
217 n, impl->name, align1, align2, len);
218 ret = 1;
219 }
220 }
221 }
222 }
223
224 int
225 test_main (void)
226 {
227 size_t i;
228
229 test_init ();
230
231 printf ("%23s", "");
232 FOR_EACH_IMPL (impl, 0)
233 printf ("\t%s", impl->name);
234 putchar ('\n');
235
236 for (i = 0; i < 16; ++i)
237 {
238 do_test (0, 0, i, SMALL_CHAR);
239 do_test (0, 0, i, BIG_CHAR);
240 do_test (0, i, i, SMALL_CHAR);
241 do_test (i, 0, i, BIG_CHAR);
242 }
243
244 for (i = 1; i < 8; ++i)
245 {
246 do_test (0, 0, 8 << i, SMALL_CHAR);
247 do_test (8 - i, 2 * i, 8 << i, SMALL_CHAR);
248 }
249
250 for (i = 1; i < 8; ++i)
251 {
252 do_test (i, 2 * i, 8 << i, SMALL_CHAR);
253 do_test (2 * i, i, 8 << i, BIG_CHAR);
254 do_test (i, i, 8 << i, SMALL_CHAR);
255 do_test (i, i, 8 << i, BIG_CHAR);
256 }
257
258 do_random_tests ();
259 return ret;
260 }
261
262 #include "../test-skeleton.c"