Don't fail in makedb if SELinux is disabled
[glibc.git] / string / test-strchr.c
bloba46ee82c1d5be5439955671e5374a0812579d925
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
20 02111-1307 USA. */
22 #define TEST_MAIN
23 #include "test-string.h"
25 #ifndef WIDE
26 # ifdef USE_FOR_STRCHRNUL
27 # define STRCHR strchrnul
28 # define stupid_STRCHR stupid_STRCHRNUL
29 # define simple_STRCHR simple_STRCHRNUL
30 # else
31 # define STRCHR strchr
32 # endif
33 # define STRLEN strlen
34 # define CHAR char
35 # define BIG_CHAR CHAR_MAX
36 # define MIDDLE_CHAR 127
37 # define SMALL_CHAR 23
38 # define UCHAR unsigned char
39 #else
40 # include <wchar.h>
41 # define STRCHR wcschr
42 # define STRLEN wcslen
43 # define CHAR wchar_t
44 # define BIG_CHAR WCHAR_MAX
45 # define MIDDLE_CHAR 1121
46 # define SMALL_CHAR 851
47 # define UCHAR wchar_t
48 #endif
50 #ifdef USE_FOR_STRCHRNUL
51 # define NULLRET(endptr) endptr
52 #else
53 # define NULLRET(endptr) NULL
54 #endif
57 typedef CHAR *(*proto_t) (const CHAR *, int);
59 CHAR *
60 simple_STRCHR (const CHAR *s, int c)
62 for (; *s != (CHAR) c; ++s)
63 if (*s == '\0')
64 return NULLRET ((CHAR *) s);
65 return (CHAR *) s;
68 CHAR *
69 stupid_STRCHR (const CHAR *s, int c)
71 size_t n = STRLEN (s) + 1;
73 while (n--)
74 if (*s++ == (CHAR) c)
75 return (CHAR *) s - 1;
76 return NULLRET ((CHAR *) s - 1);
79 IMPL (stupid_STRCHR, 0)
80 IMPL (simple_STRCHR, 0)
81 IMPL (STRCHR, 1)
83 static void
84 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
86 CHAR *res = CALL (impl, s, c);
87 if (res != exp_res)
89 error (0, 0, "Wrong result in function %s %#x %p %p", impl->name,
90 c, res, exp_res);
91 ret = 1;
92 return;
95 if (HP_TIMING_AVAIL)
97 hp_timing_t start __attribute ((unused));
98 hp_timing_t stop __attribute ((unused));
99 hp_timing_t best_time = ~ (hp_timing_t) 0;
100 size_t i;
102 for (i = 0; i < 32; ++i)
104 HP_TIMING_NOW (start);
105 CALL (impl, s, c);
106 HP_TIMING_NOW (stop);
107 HP_TIMING_BEST (best_time, start, stop);
110 printf ("\t%zd", (size_t) best_time);
114 static void
115 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
116 /* For wcschr: align here means align 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. */
120 size_t i;
121 CHAR *result;
122 CHAR *buf = (CHAR *) buf1;
123 align &= 15;
124 if ((align + len) * sizeof (CHAR) >= page_size)
125 return;
127 for (i = 0; i < len; ++i)
129 buf[align + i] = 32 + 23 * i % max_char;
130 if (buf[align + i] == seek_char)
131 buf[align + i] = seek_char + 1;
132 else if (buf[align + i] == 0)
133 buf[align + i] = 1;
135 buf[align + len] = 0;
137 if (pos < len)
139 buf[align + pos] = seek_char;
140 result = buf + align + pos;
142 else if (seek_char == 0)
143 result = buf + align + len;
144 else
145 result = NULLRET (buf + align + len);
147 if (HP_TIMING_AVAIL)
148 printf ("Length %4zd, alignment in bytes %2zd:",
149 pos, align * sizeof (CHAR));
151 FOR_EACH_IMPL (impl, 0)
152 do_one_test (impl, buf + align, seek_char, result);
154 if (HP_TIMING_AVAIL)
155 putchar ('\n');
158 static void
159 do_random_tests (void)
161 size_t i, j, n, align, pos, len;
162 int seek_char;
163 CHAR *result;
164 UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
166 for (n = 0; n < ITERATIONS; n++)
168 /* For wcschr: align here means align not in bytes, but in wchar_ts,
169 in bytes it will equal to align * (sizeof (wchar_t)). */
170 align = random () & 15;
171 pos = random () & 511;
172 seek_char = random () & 255;
173 if (pos + align >= 511)
174 pos = 510 - align - (random () & 7);
175 /* len for wcschr here isn't in bytes but it's number of wchar_t
176 symbols. */
177 len = random () & 511;
178 if ((pos == len && seek_char)
179 || (pos > len && (random () & 1)))
180 len = pos + 1 + (random () & 7);
181 if (len + align >= 512)
182 len = 511 - align - (random () & 7);
183 if (pos == len && seek_char)
184 len = pos + 1;
185 j = (pos > len ? pos : len) + align + 64;
186 if (j > 512)
187 j = 512;
189 for (i = 0; i < j; i++)
191 if (i == pos + align)
192 p[i] = seek_char;
193 else if (i == len + align)
194 p[i] = 0;
195 else
197 p[i] = random () & 255;
198 if (i < pos + align && p[i] == seek_char)
199 p[i] = seek_char + 13;
200 if (i < len + align && !p[i])
202 p[i] = seek_char - 13;
203 if (!p[i])
204 p[i] = 140;
209 if (pos <= len)
210 result = (CHAR *) (p + pos + align);
211 else if (seek_char == 0)
212 result = (CHAR *) (p + len + align);
213 else
214 result = NULLRET ((CHAR *) (p + len + align));
216 FOR_EACH_IMPL (impl, 1)
217 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
219 error (0, 0, "Iteration %zd - wrong result in function \
220 %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
221 n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
222 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
223 ret = 1;
229 test_main (void)
231 size_t i;
233 test_init ();
235 printf ("%20s", "");
236 FOR_EACH_IMPL (impl, 0)
237 printf ("\t%s", impl->name);
238 putchar ('\n');
240 for (i = 1; i < 8; ++i)
242 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
243 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
246 for (i = 1; i < 8; ++i)
248 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
249 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
252 for (i = 0; i < 32; ++i)
254 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
255 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
258 for (i = 1; i < 8; ++i)
260 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
261 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
264 for (i = 1; i < 8; ++i)
266 do_test (i, 64, 256, 0, MIDDLE_CHAR);
267 do_test (i, 64, 256, 0, BIG_CHAR);
270 for (i = 0; i < 32; ++i)
272 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
273 do_test (0, i, i + 1, 0, BIG_CHAR);
276 do_random_tests ();
277 return ret;
280 #include "../test-skeleton.c"