Include atomic.h in generic lowlevellock.c.
[glibc.git] / string / test-strchr.c
blobdeca51692c22968150d8fdcbf951e29077147759
1 /* Test and measure STRCHR functions.
2 Copyright (C) 1999-2013 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 wcschr 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 #define TEST_MAIN
22 #ifndef WIDE
23 # ifdef USE_FOR_STRCHRNUL
24 # define TEST_NAME "strchrnul"
25 # else
26 # define TEST_NAME "strchr"
27 # endif
28 #else
29 # define TEST_NAME "wcschr"
30 #endif
31 #include "test-string.h"
33 #ifndef WIDE
34 # ifdef USE_FOR_STRCHRNUL
35 # define STRCHR strchrnul
36 # define stupid_STRCHR stupid_STRCHRNUL
37 # define simple_STRCHR simple_STRCHRNUL
38 # else
39 # define STRCHR strchr
40 # endif
41 # define STRLEN strlen
42 # define CHAR char
43 # define BIG_CHAR CHAR_MAX
44 # define MIDDLE_CHAR 127
45 # define SMALL_CHAR 23
46 # define UCHAR unsigned char
47 #else
48 # include <wchar.h>
49 # define STRCHR wcschr
50 # define STRLEN wcslen
51 # define CHAR wchar_t
52 # define BIG_CHAR WCHAR_MAX
53 # define MIDDLE_CHAR 1121
54 # define SMALL_CHAR 851
55 # define UCHAR wchar_t
56 #endif
58 #ifdef USE_FOR_STRCHRNUL
59 # define NULLRET(endptr) endptr
60 #else
61 # define NULLRET(endptr) NULL
62 #endif
65 typedef CHAR *(*proto_t) (const CHAR *, int);
67 CHAR *
68 simple_STRCHR (const CHAR *s, int c)
70 for (; *s != (CHAR) c; ++s)
71 if (*s == '\0')
72 return NULLRET ((CHAR *) s);
73 return (CHAR *) s;
76 CHAR *
77 stupid_STRCHR (const CHAR *s, int c)
79 size_t n = STRLEN (s) + 1;
81 while (n--)
82 if (*s++ == (CHAR) c)
83 return (CHAR *) s - 1;
84 return NULLRET ((CHAR *) s - 1);
87 IMPL (stupid_STRCHR, 0)
88 IMPL (simple_STRCHR, 0)
89 IMPL (STRCHR, 1)
91 static int
92 check_result (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
94 CHAR *res = CALL (impl, s, c);
95 if (res != exp_res)
97 error (0, 0, "Wrong result in function %s %#x %p %p", impl->name,
98 c, res, exp_res);
99 ret = 1;
100 return -1;
102 return 0;
105 static void
106 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
108 if (check_result (impl, s, c, exp_res) < 0)
109 return;
111 if (HP_TIMING_AVAIL)
113 hp_timing_t start __attribute ((unused));
114 hp_timing_t stop __attribute ((unused));
115 hp_timing_t best_time = ~ (hp_timing_t) 0;
116 size_t i;
118 for (i = 0; i < 32; ++i)
120 HP_TIMING_NOW (start);
121 CALL (impl, s, c);
122 HP_TIMING_NOW (stop);
123 HP_TIMING_BEST (best_time, start, stop);
126 printf ("\t%zd", (size_t) best_time);
130 static void
131 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
132 /* For wcschr: align here means align not in bytes,
133 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
134 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
136 size_t i;
137 CHAR *result;
138 CHAR *buf = (CHAR *) buf1;
139 align &= 15;
140 if ((align + len) * sizeof (CHAR) >= page_size)
141 return;
143 for (i = 0; i < len; ++i)
145 buf[align + i] = 32 + 23 * i % max_char;
146 if (buf[align + i] == seek_char)
147 buf[align + i] = seek_char + 1;
148 else if (buf[align + i] == 0)
149 buf[align + i] = 1;
151 buf[align + len] = 0;
153 if (pos < len)
155 buf[align + pos] = seek_char;
156 result = buf + align + pos;
158 else if (seek_char == 0)
159 result = buf + align + len;
160 else
161 result = NULLRET (buf + align + len);
163 if (HP_TIMING_AVAIL)
164 printf ("Length %4zd, alignment in bytes %2zd:",
165 pos, align * sizeof (CHAR));
167 FOR_EACH_IMPL (impl, 0)
168 do_one_test (impl, buf + align, seek_char, result);
170 if (HP_TIMING_AVAIL)
171 putchar ('\n');
174 static void
175 do_random_tests (void)
177 size_t i, j, n, align, pos, len;
178 int seek_char;
179 CHAR *result;
180 UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
182 for (n = 0; n < ITERATIONS; n++)
184 /* For wcschr: align here means align not in bytes, but in wchar_ts,
185 in bytes it will equal to align * (sizeof (wchar_t)). */
186 align = random () & 15;
187 pos = random () & 511;
188 seek_char = random () & 255;
189 if (pos + align >= 511)
190 pos = 510 - align - (random () & 7);
191 /* len for wcschr here isn't in bytes but it's number of wchar_t
192 symbols. */
193 len = random () & 511;
194 if ((pos == len && seek_char)
195 || (pos > len && (random () & 1)))
196 len = pos + 1 + (random () & 7);
197 if (len + align >= 512)
198 len = 511 - align - (random () & 7);
199 if (pos == len && seek_char)
200 len = pos + 1;
201 j = (pos > len ? pos : len) + align + 64;
202 if (j > 512)
203 j = 512;
205 for (i = 0; i < j; i++)
207 if (i == pos + align)
208 p[i] = seek_char;
209 else if (i == len + align)
210 p[i] = 0;
211 else
213 p[i] = random () & 255;
214 if (i < pos + align && p[i] == seek_char)
215 p[i] = seek_char + 13;
216 if (i < len + align && !p[i])
218 p[i] = seek_char - 13;
219 if (!p[i])
220 p[i] = 140;
225 if (pos <= len)
226 result = (CHAR *) (p + pos + align);
227 else if (seek_char == 0)
228 result = (CHAR *) (p + len + align);
229 else
230 result = NULLRET ((CHAR *) (p + len + align));
232 FOR_EACH_IMPL (impl, 1)
233 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
235 error (0, 0, "Iteration %zd - wrong result in function \
236 %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
237 n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
238 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
239 ret = 1;
244 static void
245 check1 (void)
247 char s[] __attribute__((aligned(16))) = "\xff";
248 char c = '\xfe';
249 char *exp_result = stupid_STRCHR (s, c);
251 FOR_EACH_IMPL (impl, 0)
252 check_result (impl, s, c, exp_result);
256 test_main (void)
258 size_t i;
260 test_init ();
262 check1 ();
264 printf ("%20s", "");
265 FOR_EACH_IMPL (impl, 0)
266 printf ("\t%s", impl->name);
267 putchar ('\n');
269 for (i = 1; i < 8; ++i)
271 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
272 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
275 for (i = 1; i < 8; ++i)
277 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
278 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
281 for (i = 0; i < 32; ++i)
283 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
284 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
287 for (i = 1; i < 8; ++i)
289 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
290 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
293 for (i = 1; i < 8; ++i)
295 do_test (i, 64, 256, 0, MIDDLE_CHAR);
296 do_test (i, 64, 256, 0, BIG_CHAR);
299 for (i = 0; i < 32; ++i)
301 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
302 do_test (0, i, i + 1, 0, BIG_CHAR);
305 do_random_tests ();
306 return ret;
309 #include "../test-skeleton.c"