1 /* Test and measure STRCHR functions.
2 Copyright (C) 1999, 2002, 2003, 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 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, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include "test-string.h"
26 # define STRCHR strchr
27 # define STRLEN strlen
29 # define BIG_CHAR CHAR_MAX
30 # define MIDDLE_CHAR 127
31 # define SMALL_CHAR 23
32 # define UCHAR unsigned char
35 # define STRCHR wcschr
36 # define STRLEN wcslen
38 # define BIG_CHAR WCHAR_MAX
39 # define MIDDLE_CHAR 1121
40 # define SMALL_CHAR 851
41 # define UCHAR wchar_t
44 typedef CHAR *(*proto_t) (const CHAR *, int);
47 simple_STRCHR (const CHAR *s, int c)
49 for (; *s != (CHAR) c; ++s)
56 stupid_STRCHR (const CHAR *s, int c)
58 size_t n = STRLEN (s) + 1;
62 return (CHAR *) s - 1;
66 IMPL (stupid_STRCHR, 0)
67 IMPL (simple_STRCHR, 0)
71 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
73 CHAR *res = CALL (impl, s, c);
76 error (0, 0, "Wrong result in function %s %p %p", impl->name,
84 hp_timing_t start __attribute ((unused));
85 hp_timing_t stop __attribute ((unused));
86 hp_timing_t best_time = ~ (hp_timing_t) 0;
89 for (i = 0; i < 32; ++i)
91 HP_TIMING_NOW (start);
94 HP_TIMING_BEST (best_time, start, stop);
97 printf ("\t%zd", (size_t) best_time);
102 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
103 /* For wcschr: align here means align not in bytes,
104 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
105 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
109 CHAR *buf = (CHAR *) buf1;
111 if ((align + len) * sizeof (CHAR) >= page_size)
114 for (i = 0; i < len; ++i)
116 buf[align + i] = 32 + 23 * i % max_char;
117 if (buf[align + i] == seek_char)
118 buf[align + i] = seek_char + 1;
119 else if (buf[align + i] == 0)
122 buf[align + len] = 0;
126 buf[align + pos] = seek_char;
127 result = buf + align + pos;
129 else if (seek_char == 0)
130 result = buf + align + len;
135 printf ("Length %4zd, alignment in bytes %2zd:",
136 pos, align * sizeof (CHAR));
138 FOR_EACH_IMPL (impl, 0)
139 do_one_test (impl, buf + align, seek_char, result);
146 do_random_tests (void)
148 size_t i, j, n, align, pos, len;
151 UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
153 for (n = 0; n < ITERATIONS; n++)
155 /* For wcschr: align here means align not in bytes, but in wchar_ts,
156 in bytes it will equal to align * (sizeof (wchar_t)). */
157 align = random () & 15;
158 pos = random () & 511;
159 seek_char = random () & 255;
160 if (pos + align >= 511)
161 pos = 510 - align - (random () & 7);
162 /* len for wcschr here isn't in bytes but it's number of wchar_t
164 len = random () & 511;
165 if ((pos == len && seek_char)
166 || (pos > len && (random () & 1)))
167 len = pos + 1 + (random () & 7);
168 if (len + align >= 512)
169 len = 511 - align - (random () & 7);
170 if (pos == len && seek_char)
172 j = (pos > len ? pos : len) + align + 64;
176 for (i = 0; i < j; i++)
178 if (i == pos + align)
180 else if (i == len + align)
184 p[i] = random () & 255;
185 if (i < pos + align && p[i] == seek_char)
186 p[i] = seek_char + 13;
187 if (i < len + align && !p[i])
189 p[i] = seek_char - 13;
197 result = (CHAR *) (p + pos + align);
198 else if (seek_char == 0)
199 result = (CHAR *) (p + len + align);
203 FOR_EACH_IMPL (impl, 1)
204 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
206 error (0, 0, "Iteration %zd - wrong result in function \
207 %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
208 n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
209 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
223 FOR_EACH_IMPL (impl, 0)
224 printf ("\t%s", impl->name);
227 for (i = 1; i < 8; ++i)
229 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
230 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
233 for (i = 1; i < 8; ++i)
235 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
236 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
239 for (i = 0; i < 32; ++i)
241 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
242 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
245 for (i = 1; i < 8; ++i)
247 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
248 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
251 for (i = 1; i < 8; ++i)
253 do_test (i, 64, 256, 0, MIDDLE_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, MIDDLE_CHAR);
260 do_test (0, i, i + 1, 0, BIG_CHAR);
267 #include "../test-skeleton.c"