Use x86-64 bits/byteswap.h for both i386 and x86_64
[glibc.git] / string / test-strcpy.c
blob3a0b118afb72b4445451aec1db372895540b04c7
1 /* Test and measure strcpy functions.
2 Copyright (C) 1999, 2002, 2003, 2005, 2011, 2012 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
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.
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.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 #ifdef WIDE
22 # include <wchar.h>
23 # define CHAR wchar_t
24 # define UCHAR wchar_t
25 # define sfmt "ls"
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 sfmt "s"
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
42 #ifndef STRCPY_RESULT
43 # define STRCPY_RESULT(dst, len) dst
44 # define TEST_MAIN
45 # include "test-string.h"
46 # ifndef WIDE
47 # define SIMPLE_STRCPY simple_strcpy
48 # define STRCPY strcpy
49 # else
50 # define SIMPLE_STRCPY simple_wcscpy
51 # define STRCPY wcscpy
52 # endif
54 CHAR *SIMPLE_STRCPY (CHAR *, const CHAR *);
56 IMPL (SIMPLE_STRCPY, 0)
57 IMPL (STRCPY, 1)
59 CHAR *
60 SIMPLE_STRCPY (CHAR *dst, const CHAR *src)
62 CHAR *ret = dst;
63 while ((*dst++ = *src++) != '\0');
64 return ret;
66 #endif
68 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
70 static void
71 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src,
72 size_t len __attribute__((unused)))
74 if (CALL (impl, dst, src) != STRCPY_RESULT (dst, len))
76 error (0, 0, "Wrong result in function %s %p %p", impl->name,
77 CALL (impl, dst, src), STRCPY_RESULT (dst, len));
78 ret = 1;
79 return;
82 if (STRCMP (dst, src) != 0)
84 error (0, 0,
85 "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
86 impl->name, dst, src);
87 ret = 1;
88 return;
91 if (HP_TIMING_AVAIL)
93 hp_timing_t start __attribute ((unused));
94 hp_timing_t stop __attribute ((unused));;
95 hp_timing_t best_time = ~ (hp_timing_t) 0;
96 size_t i;
98 for (i = 0; i < 32; ++i)
100 HP_TIMING_NOW (start);
101 CALL (impl, dst, src);
102 HP_TIMING_NOW (stop);
103 HP_TIMING_BEST (best_time, start, stop);
106 printf ("\t%zd", (size_t) best_time);
110 static void
111 do_test (size_t align1, size_t align2, size_t len, int max_char)
113 size_t i;
114 CHAR *s1, *s2;
115 /* For wcscpy: align1 and align2 here mean alignment not in bytes,
116 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
117 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
118 align1 &= 7;
119 if ((align1 + len) * sizeof(CHAR) >= page_size)
120 return;
122 align2 &= 7;
123 if ((align2 + len) * sizeof(CHAR) >= page_size)
124 return;
126 s1 = (CHAR *) (buf1) + align1;
127 s2 = (CHAR *) (buf2) + align2;
129 for (i = 0; i < len; i++)
130 s1[i] = 32 + 23 * i % (max_char - 32);
131 s1[len] = 0;
133 if (HP_TIMING_AVAIL)
134 printf ("Length %4zd, alignments in bytes %2zd/%2zd:", len, align1 * sizeof(CHAR), align2 * sizeof(CHAR));
136 FOR_EACH_IMPL (impl, 0)
137 do_one_test (impl, s2, s1, len);
139 if (HP_TIMING_AVAIL)
140 putchar ('\n');
143 static void
144 do_random_tests (void)
146 size_t i, j, n, align1, align2, len;
147 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
148 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
149 UCHAR *res;
151 for (n = 0; n < ITERATIONS; n++)
153 /* For wcsrchr: align1 and align2 here mean align not in bytes,
154 but in wchar_ts, in bytes it will equal to align * (sizeof
155 (wchar_t)). For strrchr we need to check all alignments from
156 0 to 63 since some assembly implementations have separate
157 prolog for alignments more 48. */
159 align1 = random () & (63 / sizeof(CHAR));
160 if (random () & 1)
161 align2 = random () & (63 / sizeof(CHAR));
162 else
163 align2 = align1 + (random () & 24);
164 len = random () & 511;
165 j = align1;
166 if (align2 > j)
167 j = align2;
168 if (len + j >= 511)
169 len = 510 - j - (random () & 7);
170 j = len + align1 + 64;
171 if (j > 512)
172 j = 512;
173 for (i = 0; i < j; i++)
175 if (i == len + align1)
176 p1[i] = 0;
177 else
179 p1[i] = random () & BIG_CHAR;
180 if (i >= align1 && i < len + align1 && !p1[i])
181 p1[i] = (random () & SMALL_CHAR) + 3;
185 FOR_EACH_IMPL (impl, 1)
187 MEMSET (p2 - 64, '\1', 512 + 64);
188 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2), (CHAR *) (p1 + align1));
189 if (res != STRCPY_RESULT (p2 + align2, len))
191 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
192 n, impl->name, align1, align2, len, res,
193 STRCPY_RESULT (p2 + align2, len));
194 ret = 1;
196 for (j = 0; j < align2 + 64; ++j)
198 if (p2[j - 64] != '\1')
200 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
201 n, impl->name, align1, align2, len);
202 ret = 1;
203 break;
206 for (j = align2 + len + 1; j < 512; ++j)
208 if (p2[j] != '\1')
210 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
211 n, impl->name, align1, align2, len);
212 ret = 1;
213 break;
216 if (MEMCMP (p1 + align1, p2 + align2, len + 1))
218 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
219 n, impl->name, align1, align2, len);
220 ret = 1;
227 test_main (void)
229 size_t i;
231 test_init ();
233 printf ("%23s", "");
234 FOR_EACH_IMPL (impl, 0)
235 printf ("\t%s", impl->name);
236 putchar ('\n');
238 for (i = 0; i < 16; ++i)
240 do_test (0, 0, i, SMALL_CHAR);
241 do_test (0, 0, i, BIG_CHAR);
242 do_test (0, i, i, SMALL_CHAR);
243 do_test (i, 0, i, BIG_CHAR);
246 for (i = 1; i < 8; ++i)
248 do_test (0, 0, 8 << i, SMALL_CHAR);
249 do_test (8 - i, 2 * i, 8 << i, SMALL_CHAR);
252 for (i = 1; i < 8; ++i)
254 do_test (i, 2 * i, 8 << i, SMALL_CHAR);
255 do_test (2 * i, i, 8 << i, BIG_CHAR);
256 do_test (i, i, 8 << i, SMALL_CHAR);
257 do_test (i, i, 8 << i, BIG_CHAR);
260 do_random_tests ();
261 return ret;
264 #include "../test-skeleton.c"