]> sourceware.org Git - glibc.git/blob - string/test-strrchr.c
Hurd: Support --prefix=/usr special-casing for all GNU systems.
[glibc.git] / string / test-strrchr.c
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.
7
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.
12
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.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, see
20 <http://www.gnu.org/licenses/>. */
21
22 #define TEST_MAIN
23 #include "test-string.h"
24
25 #ifdef WIDE
26 # include <wchar.h>
27 # define SIMPLE_STRRCHR simple_wcsrchr
28 # define STRRCHR wcsrchr
29 # define CHAR wchar_t
30 # define UCHAR wchar_t
31 # define BIG_CHAR WCHAR_MAX
32 # define SMALL_CHAR 1273
33 #else
34 # define SIMPLE_STRRCHR simple_strrchr
35 # define STRRCHR strrchr
36 # define CHAR char
37 # define UCHAR unsigned char
38 # define BIG_CHAR CHAR_MAX
39 # define SMALL_CHAR 127
40 #endif
41
42 typedef CHAR *(*proto_t) (const CHAR *, int);
43 CHAR *SIMPLE_STRRCHR (const CHAR *, int);
44
45 IMPL (SIMPLE_STRRCHR, 0)
46 IMPL (STRRCHR, 1)
47
48 CHAR *
49 SIMPLE_STRRCHR (const CHAR *s, int c)
50 {
51 const CHAR *ret = NULL;
52
53 for (; *s != '\0'; ++s)
54 if (*s == (CHAR) c)
55 ret = s;
56
57 return (CHAR *) (c == '\0' ? s : ret);
58 }
59
60 static void
61 do_one_test (impl_t *impl, const CHAR *s, int c, CHAR *exp_res)
62 {
63 CHAR *res = CALL (impl, s, c);
64 if (res != exp_res)
65 {
66 error (0, 0, "Wrong result in function %s %p %p", impl->name,
67 res, exp_res);
68 ret = 1;
69 return;
70 }
71
72 if (HP_TIMING_AVAIL)
73 {
74 hp_timing_t start __attribute ((unused));
75 hp_timing_t stop __attribute ((unused));
76 hp_timing_t best_time = ~ (hp_timing_t) 0;
77 size_t i;
78
79 for (i = 0; i < 32; ++i)
80 {
81 HP_TIMING_NOW (start);
82 CALL (impl, s, c);
83 HP_TIMING_NOW (stop);
84 HP_TIMING_BEST (best_time, start, stop);
85 }
86
87 printf ("\t%zd", (size_t) best_time);
88 }
89 }
90
91 static void
92 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
93 /* For wcsrchr: align here means align not in bytes,
94 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
95 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
96 {
97 size_t i;
98 CHAR *result;
99 CHAR *buf = (CHAR *) buf1;
100
101 align &= 7;
102 if ( (align + len) * sizeof(CHAR) >= page_size)
103 return;
104
105 for (i = 0; i < len; ++i)
106 {
107 buf[align + i] = (random () * random ()) & max_char;
108 if (!buf[align + i])
109 buf[align + i] = (random () * random ()) & max_char;
110 if (!buf[align + i])
111 buf[align + i] = 1;
112 if ((i > pos || pos >= len) && buf[align + i] == seek_char)
113 buf[align + i] = seek_char + 10 + (random () & 15);
114 }
115 buf[align + len] = 0;
116
117 if (pos < len)
118 {
119 buf[align + pos] = seek_char;
120 result = (CHAR *) (buf + align + pos);
121 }
122 else if (seek_char == 0)
123 result = (CHAR *) (buf + align + len);
124 else
125 result = NULL;
126
127 if (HP_TIMING_AVAIL)
128 printf ("Length %4zd, alignment in bytes %2zd:", pos, align * sizeof(CHAR));
129
130 FOR_EACH_IMPL (impl, 0)
131 do_one_test (impl, (CHAR *) (buf + align), seek_char, result);
132
133 if (HP_TIMING_AVAIL)
134 putchar ('\n');
135 }
136
137 static void
138 do_random_tests (void)
139 {
140 size_t i, j, n, align, pos, len;
141 int seek_char;
142 CHAR *result;
143 UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
144
145 for (n = 0; n < ITERATIONS; n++)
146 {
147 align = random () & (63 / sizeof(CHAR));
148 /* For wcsrchr: align here means align not in bytes, but in wchar_ts,
149 in bytes it will equal to align * (sizeof (wchar_t)).
150 For strrchr we need to check all alignments from 0 to 63 since
151 some assembly implementations have separate prolog for alignments
152 more 48. */
153 pos = random () & 511;
154 if (pos + align >= 511)
155 pos = 510 - align - (random () & 7);
156 len = random () & 511;
157 /* len for wcschr here isn't in bytes but it's number of wchar_t
158 symbols. */
159 if (pos >= len)
160 len = pos + (random () & 7);
161 if (len + align >= 512)
162 len = 511 - align - (random () & 7);
163 seek_char = random () & 255;
164 if (seek_char && pos == len)
165 {
166 if (pos)
167 --pos;
168 else
169 ++len;
170 }
171 j = len + align + 64;
172 if (j > 512)
173 j = 512;
174
175 for (i = 0; i < j; i++)
176 {
177 if (i == pos + align)
178 p[i] = seek_char;
179 else if (i == len + align)
180 p[i] = 0;
181 else
182 {
183 p[i] = random () & 255;
184 if (((i > pos + align && i < len + align) || pos > len)
185 && p[i] == seek_char)
186 p[i] = seek_char + 13;
187 if (i < len + align && !p[i])
188 {
189 p[i] = seek_char - 13;
190 if (!p[i])
191 p[i] = 140;
192 }
193 }
194 }
195
196 if (pos <= len)
197 result = (CHAR *) (p + pos + align);
198 else if (seek_char == 0)
199 result = (CHAR *) (p + len + align);
200 else
201 result = NULL;
202
203 FOR_EACH_IMPL (impl, 1)
204 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
205 {
206 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
207 n, impl->name, align, seek_char, len, pos,
208 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
209 ret = 1;
210 }
211 }
212 }
213
214 int
215 test_main (void)
216 {
217 size_t i;
218
219 test_init ();
220
221 printf ("%20s", "");
222 FOR_EACH_IMPL (impl, 0)
223 printf ("\t%s", impl->name);
224 putchar ('\n');
225
226 for (i = 1; i < 8; ++i)
227 {
228 do_test (0, 16 << i, 2048, 23, SMALL_CHAR);
229 do_test (i, 16 << i, 2048, 23, SMALL_CHAR);
230 }
231
232 for (i = 1; i < 8; ++i)
233 {
234 do_test (i, 64, 256, 23, SMALL_CHAR);
235 do_test (i, 64, 256, 23, BIG_CHAR);
236 }
237
238 for (i = 0; i < 32; ++i)
239 {
240 do_test (0, i, i + 1, 23, SMALL_CHAR);
241 do_test (0, i, i + 1, 23, BIG_CHAR);
242 }
243
244 for (i = 1; i < 8; ++i)
245 {
246 do_test (0, 16 << i, 2048, 0, SMALL_CHAR);
247 do_test (i, 16 << i, 2048, 0, SMALL_CHAR);
248 }
249
250 for (i = 1; i < 8; ++i)
251 {
252 do_test (i, 64, 256, 0, SMALL_CHAR);
253 do_test (i, 64, 256, 0, BIG_CHAR);
254 }
255
256 for (i = 0; i < 32; ++i)
257 {
258 do_test (0, i, i + 1, 0, SMALL_CHAR);
259 do_test (0, i, i + 1, 0, BIG_CHAR);
260 }
261
262 do_random_tests ();
263 return ret;
264 }
265
266 #include "../test-skeleton.c"
This page took 0.044662 seconds and 5 git commands to generate.