Set LC_ALL=C before sed.
[glibc.git] / string / test-strrchr.c
blob984561d8fae3709702a4b7646ef019b0f6ab1685
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 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, see
20 <http://www.gnu.org/licenses/>. */
22 #define TEST_MAIN
23 #ifdef WIDE
24 # define TEST_NAME "wcsrchr"
25 #else
26 # define TEST_NAME "strrchr"
27 #endif
28 #include "test-string.h"
30 #ifdef WIDE
31 # include <wchar.h>
32 # define SIMPLE_STRRCHR simple_wcsrchr
33 # define STRRCHR wcsrchr
34 # define CHAR wchar_t
35 # define UCHAR wchar_t
36 # define BIG_CHAR WCHAR_MAX
37 # define SMALL_CHAR 1273
38 #else
39 # define SIMPLE_STRRCHR simple_strrchr
40 # define STRRCHR strrchr
41 # define CHAR char
42 # define UCHAR unsigned char
43 # define BIG_CHAR CHAR_MAX
44 # define SMALL_CHAR 127
45 #endif
47 typedef CHAR *(*proto_t) (const CHAR *, int);
48 CHAR *SIMPLE_STRRCHR (const CHAR *, int);
50 IMPL (SIMPLE_STRRCHR, 0)
51 IMPL (STRRCHR, 1)
53 CHAR *
54 SIMPLE_STRRCHR (const CHAR *s, int c)
56 const CHAR *ret = NULL;
58 for (; *s != '\0'; ++s)
59 if (*s == (CHAR) c)
60 ret = s;
62 return (CHAR *) (c == '\0' ? s : ret);
65 static void
66 do_one_test (impl_t *impl, const CHAR *s, int c, CHAR *exp_res)
68 CHAR *res = CALL (impl, s, c);
69 if (res != exp_res)
71 error (0, 0, "Wrong result in function %s %p %p", impl->name,
72 res, exp_res);
73 ret = 1;
74 return;
77 if (HP_TIMING_AVAIL)
79 hp_timing_t start __attribute ((unused));
80 hp_timing_t stop __attribute ((unused));
81 hp_timing_t best_time = ~ (hp_timing_t) 0;
82 size_t i;
84 for (i = 0; i < 32; ++i)
86 HP_TIMING_NOW (start);
87 CALL (impl, s, c);
88 HP_TIMING_NOW (stop);
89 HP_TIMING_BEST (best_time, start, stop);
92 printf ("\t%zd", (size_t) best_time);
96 static void
97 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
98 /* For wcsrchr: align here means align not in bytes,
99 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
100 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
102 size_t i;
103 CHAR *result;
104 CHAR *buf = (CHAR *) buf1;
106 align &= 7;
107 if ( (align + len) * sizeof(CHAR) >= page_size)
108 return;
110 for (i = 0; i < len; ++i)
112 buf[align + i] = (random () * random ()) & max_char;
113 if (!buf[align + i])
114 buf[align + i] = (random () * random ()) & max_char;
115 if (!buf[align + i])
116 buf[align + i] = 1;
117 if ((i > pos || pos >= len) && buf[align + i] == seek_char)
118 buf[align + i] = seek_char + 10 + (random () & 15);
120 buf[align + len] = 0;
122 if (pos < len)
124 buf[align + pos] = seek_char;
125 result = (CHAR *) (buf + align + pos);
127 else if (seek_char == 0)
128 result = (CHAR *) (buf + align + len);
129 else
130 result = NULL;
132 if (HP_TIMING_AVAIL)
133 printf ("Length %4zd, alignment in bytes %2zd:", pos, align * sizeof(CHAR));
135 FOR_EACH_IMPL (impl, 0)
136 do_one_test (impl, (CHAR *) (buf + align), seek_char, result);
138 if (HP_TIMING_AVAIL)
139 putchar ('\n');
142 static void
143 do_random_tests (void)
145 size_t i, j, n, align, pos, len;
146 int seek_char;
147 CHAR *result;
148 UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
150 for (n = 0; n < ITERATIONS; n++)
152 align = random () & (63 / sizeof(CHAR));
153 /* For wcsrchr: align here means align not in bytes, but in wchar_ts,
154 in bytes it will equal to align * (sizeof (wchar_t)).
155 For strrchr we need to check all alignments from 0 to 63 since
156 some assembly implementations have separate prolog for alignments
157 more 48. */
158 pos = random () & 511;
159 if (pos + align >= 511)
160 pos = 510 - align - (random () & 7);
161 len = random () & 511;
162 /* len for wcschr here isn't in bytes but it's number of wchar_t
163 symbols. */
164 if (pos >= len)
165 len = pos + (random () & 7);
166 if (len + align >= 512)
167 len = 511 - align - (random () & 7);
168 seek_char = random () & 255;
169 if (seek_char && pos == len)
171 if (pos)
172 --pos;
173 else
174 ++len;
176 j = len + align + 64;
177 if (j > 512)
178 j = 512;
180 for (i = 0; i < j; i++)
182 if (i == pos + align)
183 p[i] = seek_char;
184 else if (i == len + align)
185 p[i] = 0;
186 else
188 p[i] = random () & 255;
189 if (((i > pos + align && i < len + align) || pos > len)
190 && p[i] == seek_char)
191 p[i] = seek_char + 13;
192 if (i < len + align && !p[i])
194 p[i] = seek_char - 13;
195 if (!p[i])
196 p[i] = 140;
201 if (pos <= len)
202 result = (CHAR *) (p + pos + align);
203 else if (seek_char == 0)
204 result = (CHAR *) (p + len + align);
205 else
206 result = NULL;
208 FOR_EACH_IMPL (impl, 1)
209 if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
211 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
212 n, impl->name, align, seek_char, len, pos,
213 CALL (impl, (CHAR *) (p + align), seek_char), result, p);
214 ret = 1;
220 test_main (void)
222 size_t i;
224 test_init ();
226 printf ("%20s", "");
227 FOR_EACH_IMPL (impl, 0)
228 printf ("\t%s", impl->name);
229 putchar ('\n');
231 for (i = 1; i < 8; ++i)
233 do_test (0, 16 << i, 2048, 23, SMALL_CHAR);
234 do_test (i, 16 << i, 2048, 23, SMALL_CHAR);
237 for (i = 1; i < 8; ++i)
239 do_test (i, 64, 256, 23, SMALL_CHAR);
240 do_test (i, 64, 256, 23, BIG_CHAR);
243 for (i = 0; i < 32; ++i)
245 do_test (0, i, i + 1, 23, SMALL_CHAR);
246 do_test (0, i, i + 1, 23, BIG_CHAR);
249 for (i = 1; i < 8; ++i)
251 do_test (0, 16 << i, 2048, 0, SMALL_CHAR);
252 do_test (i, 16 << i, 2048, 0, SMALL_CHAR);
255 for (i = 1; i < 8; ++i)
257 do_test (i, 64, 256, 0, SMALL_CHAR);
258 do_test (i, 64, 256, 0, BIG_CHAR);
261 for (i = 0; i < 32; ++i)
263 do_test (0, i, i + 1, 0, SMALL_CHAR);
264 do_test (0, i, i + 1, 0, BIG_CHAR);
267 do_random_tests ();
268 return ret;
271 #include "../test-skeleton.c"