Remove support in configure for unsupported architectures
[glibc.git] / string / test-strrchr.c
blob484c2f302af2f52d8f5c9b0e7e64a14b9060e01d
1 /* Test and measure STRCHR 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 wcsrrchr support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>,
6 2011.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 02111-1307 USA. */
23 #define TEST_MAIN
24 #include "test-string.h"
26 #ifdef WIDE
27 # include <wchar.h>
28 # define SIMPLE_STRRCHR simple_wcsrchr
29 # define STRRCHR wcsrchr
30 # define CHAR wchar_t
31 # define UCHAR wchar_t
32 # define BIG_CHAR WCHAR_MAX
33 # define SMALL_CHAR 1273
34 #else
35 # define SIMPLE_STRRCHR simple_strrchr
36 # define STRRCHR strrchr
37 # define CHAR char
38 # define UCHAR unsigned char
39 # define BIG_CHAR CHAR_MAX
40 # define SMALL_CHAR 127
41 #endif
43 typedef CHAR *(*proto_t) (const CHAR *, int);
44 CHAR *SIMPLE_STRRCHR (const CHAR *, int);
46 IMPL (SIMPLE_STRRCHR, 0)
47 IMPL (STRRCHR, 1)
49 CHAR *
50 SIMPLE_STRRCHR (const CHAR *s, int c)
52 const CHAR *ret = NULL;
54 for (; *s != '\0'; ++s)
55 if (*s == (CHAR) c)
56 ret = s;
58 return (CHAR *) (c == '\0' ? s : ret);
61 static void
62 do_one_test (impl_t *impl, const CHAR *s, int c, CHAR *exp_res)
64 CHAR *res = CALL (impl, s, c);
65 if (res != exp_res)
67 error (0, 0, "Wrong result in function %s %p %p", impl->name,
68 res, exp_res);
69 ret = 1;
70 return;
73 if (HP_TIMING_AVAIL)
75 hp_timing_t start __attribute ((unused));
76 hp_timing_t stop __attribute ((unused));
77 hp_timing_t best_time = ~ (hp_timing_t) 0;
78 size_t i;
80 for (i = 0; i < 32; ++i)
82 HP_TIMING_NOW (start);
83 CALL (impl, s, c);
84 HP_TIMING_NOW (stop);
85 HP_TIMING_BEST (best_time, start, stop);
88 printf ("\t%zd", (size_t) best_time);
92 static void
93 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
94 /* For wcsrchr: align here means align not in bytes,
95 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
96 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
98 size_t i;
99 CHAR *result;
100 CHAR *buf = (CHAR *) buf1;
102 align &= 7;
103 if ( (align + len) * sizeof(CHAR) >= page_size)
104 return;
106 for (i = 0; i < len; ++i)
108 buf[align + i] = (random () * random ()) & max_char;
109 if (!buf[align + i])
110 buf[align + i] = (random () * random ()) & max_char;
111 if (!buf[align + i])
112 buf[align + i] = 1;
113 if ((i > pos || pos >= len) && buf[align + i] == seek_char)
114 buf[align + i] = seek_char + 10 + (random () & 15);
116 buf[align + len] = 0;
118 if (pos < len)
120 buf[align + pos] = seek_char;
121 result = (CHAR *) (buf + align + pos);
123 else if (seek_char == 0)
124 result = (CHAR *) (buf + align + len);
125 else
126 result = NULL;
128 if (HP_TIMING_AVAIL)
129 printf ("Length %4zd, alignment in bytes %2zd:", pos, align * sizeof(CHAR));
131 FOR_EACH_IMPL (impl, 0)
132 do_one_test (impl, (CHAR *) (buf + align), seek_char, result);
134 if (HP_TIMING_AVAIL)
135 putchar ('\n');
138 static void
139 do_random_tests (void)
141 size_t i, j, n, align, pos, len;
142 int seek_char;
143 CHAR *result;
144 UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
146 for (n = 0; n < ITERATIONS; n++)
148 align = random () & (63 / sizeof(CHAR));
149 /* For wcsrchr: align here means align not in bytes, but in wchar_ts,
150 in bytes it will equal to align * (sizeof (wchar_t)).
151 For strrchr we need to check all alignments from 0 to 63 since
152 some assembly implementations have separate prolog for alignments
153 more 48. */
154 pos = random () & 511;
155 if (pos + align >= 511)
156 pos = 510 - align - (random () & 7);
157 len = random () & 511;
158 /* len for wcschr here isn't in bytes but it's number of wchar_t
159 symbols. */
160 if (pos >= len)
161 len = pos + (random () & 7);
162 if (len + align >= 512)
163 len = 511 - align - (random () & 7);
164 seek_char = random () & 255;
165 if (seek_char && pos == len)
167 if (pos)
168 --pos;
169 else
170 ++len;
172 j = len + align + 64;
173 if (j > 512)
174 j = 512;
176 for (i = 0; i < j; i++)
178 if (i == pos + align)
179 p[i] = seek_char;
180 else if (i == len + align)
181 p[i] = 0;
182 else
184 p[i] = random () & 255;
185 if (((i > pos + align && i < len + align) || pos > len)
186 && p[i] == seek_char)
187 p[i] = seek_char + 13;
188 if (i < len + align && !p[i])
190 p[i] = seek_char - 13;
191 if (!p[i])
192 p[i] = 140;
197 if (pos <= len)
198 result = (CHAR *) (p + pos + align);
199 else if (seek_char == 0)
200 result = (CHAR *) (p + len + align);
201 else
202 result = NULL;
204 FOR_EACH_IMPL (impl, 1)
205 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
207 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
208 n, impl->name, align, seek_char, len, pos,
209 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
210 ret = 1;
216 test_main (void)
218 size_t i;
220 test_init ();
222 printf ("%20s", "");
223 FOR_EACH_IMPL (impl, 0)
224 printf ("\t%s", impl->name);
225 putchar ('\n');
227 for (i = 1; i < 8; ++i)
229 do_test (0, 16 << i, 2048, 23, SMALL_CHAR);
230 do_test (i, 16 << i, 2048, 23, SMALL_CHAR);
233 for (i = 1; i < 8; ++i)
235 do_test (i, 64, 256, 23, SMALL_CHAR);
236 do_test (i, 64, 256, 23, BIG_CHAR);
239 for (i = 0; i < 32; ++i)
241 do_test (0, i, i + 1, 23, SMALL_CHAR);
242 do_test (0, i, i + 1, 23, BIG_CHAR);
245 for (i = 1; i < 8; ++i)
247 do_test (0, 16 << i, 2048, 0, SMALL_CHAR);
248 do_test (i, 16 << i, 2048, 0, SMALL_CHAR);
251 for (i = 1; i < 8; ++i)
253 do_test (i, 64, 256, 0, SMALL_CHAR);
254 do_test (i, 64, 256, 0, BIG_CHAR);
257 for (i = 0; i < 32; ++i)
259 do_test (0, i, i + 1, 0, SMALL_CHAR);
260 do_test (0, i, i + 1, 0, BIG_CHAR);
263 do_random_tests ();
264 return ret;
267 #include "../test-skeleton.c"