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, see
19 <http://www.gnu.org/licenses/>. */
22 #include "test-string.h"
25 # ifdef USE_FOR_STRCHRNUL
26 # define STRCHR strchrnul
27 # define stupid_STRCHR stupid_STRCHRNUL
28 # define simple_STRCHR simple_STRCHRNUL
30 # define STRCHR strchr
32 # define STRLEN strlen
34 # define BIG_CHAR CHAR_MAX
35 # define MIDDLE_CHAR 127
36 # define SMALL_CHAR 23
37 # define UCHAR unsigned char
40 # define STRCHR wcschr
41 # define STRLEN wcslen
43 # define BIG_CHAR WCHAR_MAX
44 # define MIDDLE_CHAR 1121
45 # define SMALL_CHAR 851
46 # define UCHAR wchar_t
49 #ifdef USE_FOR_STRCHRNUL
50 # define NULLRET(endptr) endptr
52 # define NULLRET(endptr) NULL
56 typedef CHAR
*(*proto_t
) (const CHAR
*, int);
59 simple_STRCHR (const CHAR
*s
, int c
)
61 for (; *s
!= (CHAR
) c
; ++s
)
63 return NULLRET ((CHAR
*) s
);
68 stupid_STRCHR (const CHAR
*s
, int c
)
70 size_t n
= STRLEN (s
) + 1;
74 return (CHAR
*) s
- 1;
75 return NULLRET ((CHAR
*) s
- 1);
78 IMPL (stupid_STRCHR
, 0)
79 IMPL (simple_STRCHR
, 0)
83 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, const CHAR
*exp_res
)
85 CHAR
*res
= CALL (impl
, s
, c
);
88 error (0, 0, "Wrong result in function %s %#x %p %p", impl
->name
,
96 hp_timing_t start
__attribute ((unused
));
97 hp_timing_t stop
__attribute ((unused
));
98 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
101 for (i
= 0; i
< 32; ++i
)
103 HP_TIMING_NOW (start
);
105 HP_TIMING_NOW (stop
);
106 HP_TIMING_BEST (best_time
, start
, stop
);
109 printf ("\t%zd", (size_t) best_time
);
114 do_test (size_t align
, size_t pos
, size_t len
, int seek_char
, int max_char
)
115 /* For wcschr: align here means align not in bytes,
116 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
117 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
121 CHAR
*buf
= (CHAR
*) buf1
;
123 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
126 for (i
= 0; i
< len
; ++i
)
128 buf
[align
+ i
] = 32 + 23 * i
% max_char
;
129 if (buf
[align
+ i
] == seek_char
)
130 buf
[align
+ i
] = seek_char
+ 1;
131 else if (buf
[align
+ i
] == 0)
134 buf
[align
+ len
] = 0;
138 buf
[align
+ pos
] = seek_char
;
139 result
= buf
+ align
+ pos
;
141 else if (seek_char
== 0)
142 result
= buf
+ align
+ len
;
144 result
= NULLRET (buf
+ align
+ len
);
147 printf ("Length %4zd, alignment in bytes %2zd:",
148 pos
, align
* sizeof (CHAR
));
150 FOR_EACH_IMPL (impl
, 0)
151 do_one_test (impl
, buf
+ align
, seek_char
, result
);
158 do_random_tests (void)
160 size_t i
, j
, n
, align
, pos
, len
;
163 UCHAR
*p
= (UCHAR
*) (buf1
+ page_size
- 512 * sizeof (CHAR
));
165 for (n
= 0; n
< ITERATIONS
; n
++)
167 /* For wcschr: align here means align not in bytes, but in wchar_ts,
168 in bytes it will equal to align * (sizeof (wchar_t)). */
169 align
= random () & 15;
170 pos
= random () & 511;
171 seek_char
= random () & 255;
172 if (pos
+ align
>= 511)
173 pos
= 510 - align
- (random () & 7);
174 /* len for wcschr here isn't in bytes but it's number of wchar_t
176 len
= random () & 511;
177 if ((pos
== len
&& seek_char
)
178 || (pos
> len
&& (random () & 1)))
179 len
= pos
+ 1 + (random () & 7);
180 if (len
+ align
>= 512)
181 len
= 511 - align
- (random () & 7);
182 if (pos
== len
&& seek_char
)
184 j
= (pos
> len
? pos
: len
) + align
+ 64;
188 for (i
= 0; i
< j
; i
++)
190 if (i
== pos
+ align
)
192 else if (i
== len
+ align
)
196 p
[i
] = random () & 255;
197 if (i
< pos
+ align
&& p
[i
] == seek_char
)
198 p
[i
] = seek_char
+ 13;
199 if (i
< len
+ align
&& !p
[i
])
201 p
[i
] = seek_char
- 13;
209 result
= (CHAR
*) (p
+ pos
+ align
);
210 else if (seek_char
== 0)
211 result
= (CHAR
*) (p
+ len
+ align
);
213 result
= NULLRET ((CHAR
*) (p
+ len
+ align
));
215 FOR_EACH_IMPL (impl
, 1)
216 if (CALL (impl
, (CHAR
*) (p
+ align
), seek_char
) != result
)
218 error (0, 0, "Iteration %zd - wrong result in function \
219 %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
220 n
, impl
->name
, align
* sizeof (CHAR
), seek_char
, len
, pos
,
221 CALL (impl
, (CHAR
*) (p
+ align
), seek_char
), result
, p
);
235 FOR_EACH_IMPL (impl
, 0)
236 printf ("\t%s", impl
->name
);
239 for (i
= 1; i
< 8; ++i
)
241 do_test (0, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
242 do_test (i
, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
245 for (i
= 1; i
< 8; ++i
)
247 do_test (i
, 64, 256, SMALL_CHAR
, MIDDLE_CHAR
);
248 do_test (i
, 64, 256, SMALL_CHAR
, BIG_CHAR
);
251 for (i
= 0; i
< 32; ++i
)
253 do_test (0, i
, i
+ 1, SMALL_CHAR
, MIDDLE_CHAR
);
254 do_test (0, i
, i
+ 1, SMALL_CHAR
, BIG_CHAR
);
257 for (i
= 1; i
< 8; ++i
)
259 do_test (0, 16 << i
, 2048, 0, MIDDLE_CHAR
);
260 do_test (i
, 16 << i
, 2048, 0, MIDDLE_CHAR
);
263 for (i
= 1; i
< 8; ++i
)
265 do_test (i
, 64, 256, 0, MIDDLE_CHAR
);
266 do_test (i
, 64, 256, 0, BIG_CHAR
);
269 for (i
= 0; i
< 32; ++i
)
271 do_test (0, i
, i
+ 1, 0, MIDDLE_CHAR
);
272 do_test (0, i
, i
+ 1, 0, BIG_CHAR
);
279 #include "../test-skeleton.c"