Remove support in configure for unsupported architectures
[glibc.git] / string / test-strcpy.c
blob04d9897b6788e130cc8c64472b6b9d799d495895
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, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #ifdef WIDE
23 # include <wchar.h>
24 # define CHAR wchar_t
25 # define UCHAR wchar_t
26 # define sfmt "ls"
27 # define BIG_CHAR WCHAR_MAX
28 # define SMALL_CHAR 1273
29 # define STRCMP wcscmp
30 # define MEMCMP wmemcmp
31 # define MEMSET wmemset
32 #else
33 # define CHAR char
34 # define UCHAR unsigned char
35 # define sfmt "s"
36 # define BIG_CHAR CHAR_MAX
37 # define SMALL_CHAR 127
38 # define STRCMP strcmp
39 # define MEMCMP memcmp
40 # define MEMSET memset
41 #endif
43 #ifndef STRCPY_RESULT
44 # define STRCPY_RESULT(dst, len) dst
45 # define TEST_MAIN
46 # include "test-string.h"
47 # ifndef WIDE
48 # define SIMPLE_STRCPY simple_strcpy
49 # define STRCPY strcpy
50 # else
51 # define SIMPLE_STRCPY simple_wcscpy
52 # define STRCPY wcscpy
53 # endif
55 CHAR *SIMPLE_STRCPY (CHAR *, const CHAR *);
57 IMPL (SIMPLE_STRCPY, 0)
58 IMPL (STRCPY, 1)
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;
92 if (HP_TIMING_AVAIL)
94 hp_timing_t start __attribute ((unused));
95 hp_timing_t stop __attribute ((unused));;
96 hp_timing_t best_time = ~ (hp_timing_t) 0;
97 size_t i;
99 for (i = 0; i < 32; ++i)
101 HP_TIMING_NOW (start);
102 CALL (impl, dst, src);
103 HP_TIMING_NOW (stop);
104 HP_TIMING_BEST (best_time, start, stop);
107 printf ("\t%zd", (size_t) best_time);
111 static void
112 do_test (size_t align1, size_t align2, size_t len, int max_char)
114 size_t i;
115 CHAR *s1, *s2;
116 /* For wcscpy: align1 and align2 here mean alignment not in bytes,
117 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
118 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
119 align1 &= 7;
120 if ((align1 + len) * sizeof(CHAR) >= page_size)
121 return;
123 align2 &= 7;
124 if ((align2 + len) * sizeof(CHAR) >= page_size)
125 return;
127 s1 = (CHAR *) (buf1) + align1;
128 s2 = (CHAR *) (buf2) + align2;
130 for (i = 0; i < len; i++)
131 s1[i] = 32 + 23 * i % (max_char - 32);
132 s1[len] = 0;
134 if (HP_TIMING_AVAIL)
135 printf ("Length %4zd, alignments in bytes %2zd/%2zd:", len, align1 * sizeof(CHAR), align2 * sizeof(CHAR));
137 FOR_EACH_IMPL (impl, 0)
138 do_one_test (impl, s2, s1, len);
140 if (HP_TIMING_AVAIL)
141 putchar ('\n');
144 static void
145 do_random_tests (void)
147 size_t i, j, n, align1, align2, len;
148 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
149 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
150 UCHAR *res;
152 for (n = 0; n < ITERATIONS; n++)
154 /* For wcsrchr: align1 and align2 here mean align not in bytes,
155 but in wchar_ts, in bytes it will equal to align * (sizeof
156 (wchar_t)). For strrchr we need to check all alignments from
157 0 to 63 since some assembly implementations have separate
158 prolog for alignments more 48. */
160 align1 = random () & (63 / sizeof(CHAR));
161 if (random () & 1)
162 align2 = random () & (63 / sizeof(CHAR));
163 else
164 align2 = align1 + (random () & 24);
165 len = random () & 511;
166 j = align1;
167 if (align2 > j)
168 j = align2;
169 if (len + j >= 511)
170 len = 510 - j - (random () & 7);
171 j = len + align1 + 64;
172 if (j > 512)
173 j = 512;
174 for (i = 0; i < j; i++)
176 if (i == len + align1)
177 p1[i] = 0;
178 else
180 p1[i] = random () & BIG_CHAR;
181 if (i >= align1 && i < len + align1 && !p1[i])
182 p1[i] = (random () & SMALL_CHAR) + 3;
186 FOR_EACH_IMPL (impl, 1)
188 MEMSET (p2 - 64, '\1', 512 + 64);
189 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2), (CHAR *) (p1 + align1));
190 if (res != STRCPY_RESULT (p2 + align2, len))
192 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
193 n, impl->name, align1, align2, len, res,
194 STRCPY_RESULT (p2 + align2, len));
195 ret = 1;
197 for (j = 0; j < align2 + 64; ++j)
199 if (p2[j - 64] != '\1')
201 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
202 n, impl->name, align1, align2, len);
203 ret = 1;
204 break;
207 for (j = align2 + len + 1; j < 512; ++j)
209 if (p2[j] != '\1')
211 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
212 n, impl->name, align1, align2, len);
213 ret = 1;
214 break;
217 if (MEMCMP (p1 + align1, p2 + align2, len + 1))
219 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
220 n, impl->name, align1, align2, len);
221 ret = 1;
228 test_main (void)
230 size_t i;
232 test_init ();
234 printf ("%23s", "");
235 FOR_EACH_IMPL (impl, 0)
236 printf ("\t%s", impl->name);
237 putchar ('\n');
239 for (i = 0; i < 16; ++i)
241 do_test (0, 0, i, SMALL_CHAR);
242 do_test (0, 0, i, BIG_CHAR);
243 do_test (0, i, i, SMALL_CHAR);
244 do_test (i, 0, i, BIG_CHAR);
247 for (i = 1; i < 8; ++i)
249 do_test (0, 0, 8 << i, SMALL_CHAR);
250 do_test (8 - i, 2 * i, 8 << i, SMALL_CHAR);
253 for (i = 1; i < 8; ++i)
255 do_test (i, 2 * i, 8 << i, SMALL_CHAR);
256 do_test (2 * i, i, 8 << i, BIG_CHAR);
257 do_test (i, i, 8 << i, SMALL_CHAR);
258 do_test (i, i, 8 << i, BIG_CHAR);
261 do_random_tests ();
262 return ret;
265 #include "../test-skeleton.c"