1 /* Test and measure STRCHR functions.
2 Copyright (C) 1999-2021 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>,
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 <https://www.gnu.org/licenses/>. */
24 # define TEST_NAME "wcsrchr"
26 # define TEST_NAME "strrchr"
28 #include "test-string.h"
32 # define SIMPLE_STRRCHR simple_wcsrchr
33 # define STRRCHR wcsrchr
35 # define UCHAR wchar_t
36 # define BIG_CHAR WCHAR_MAX
37 # define SMALL_CHAR 1273
39 # define SIMPLE_STRRCHR simple_strrchr
40 # define STRRCHR strrchr
42 # define UCHAR unsigned char
43 # define BIG_CHAR CHAR_MAX
44 # define SMALL_CHAR 127
47 typedef CHAR
*(*proto_t
) (const CHAR
*, int);
48 CHAR
*SIMPLE_STRRCHR (const CHAR
*, int);
50 IMPL (SIMPLE_STRRCHR
, 0)
54 SIMPLE_STRRCHR (const CHAR
*s
, int c
)
56 const CHAR
*ret
= NULL
;
58 for (; *s
!= '\0'; ++s
)
62 return (CHAR
*) (c
== '\0' ? s
: ret
);
66 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, CHAR
*exp_res
)
68 CHAR
*res
= CALL (impl
, s
, c
);
71 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
79 do_test (size_t align
, size_t pos
, size_t len
, int seek_char
, int max_char
)
80 /* For wcsrchr: align here means align not in bytes,
81 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
82 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
86 CHAR
*buf
= (CHAR
*) buf1
;
89 if ( (align
+ len
) * sizeof (CHAR
) >= page_size
)
92 for (i
= 0; i
< len
; ++i
)
94 buf
[align
+ i
] = (random () * random ()) & max_char
;
96 buf
[align
+ i
] = (random () * random ()) & max_char
;
99 if ((i
> pos
|| pos
>= len
) && buf
[align
+ i
] == seek_char
)
100 buf
[align
+ i
] = seek_char
+ 10 + (random () & 15);
102 buf
[align
+ len
] = 0;
106 buf
[align
+ pos
] = seek_char
;
107 result
= (CHAR
*) (buf
+ align
+ pos
);
109 else if (seek_char
== 0)
110 result
= (CHAR
*) (buf
+ align
+ len
);
114 FOR_EACH_IMPL (impl
, 0)
115 do_one_test (impl
, (CHAR
*) (buf
+ align
), seek_char
, result
);
119 do_random_tests (void)
121 size_t i
, j
, n
, align
, pos
, len
;
124 UCHAR
*p
= (UCHAR
*) (buf1
+ page_size
) - 512;
126 for (n
= 0; n
< ITERATIONS
; n
++)
128 align
= random () & (63 / sizeof (CHAR
));
129 /* For wcsrchr: align here means align not in bytes, but in wchar_ts,
130 in bytes it will equal to align * (sizeof (wchar_t)).
131 For strrchr we need to check all alignments from 0 to 63 since
132 some assembly implementations have separate prolog for alignments
134 pos
= random () & 511;
135 if (pos
+ align
>= 511)
136 pos
= 510 - align
- (random () & 7);
137 len
= random () & 511;
138 /* len for wcschr here isn't in bytes but it's number of wchar_t
141 len
= pos
+ (random () & 7);
142 if (len
+ align
>= 512)
143 len
= 511 - align
- (random () & 7);
144 seek_char
= random () & 255;
145 if (seek_char
&& pos
== len
)
152 j
= len
+ align
+ 64;
156 for (i
= 0; i
< j
; i
++)
158 if (i
== pos
+ align
)
160 else if (i
== len
+ align
)
164 p
[i
] = random () & 255;
165 if (((i
> pos
+ align
&& i
< len
+ align
) || pos
> len
)
166 && p
[i
] == seek_char
)
167 p
[i
] = seek_char
+ 13;
168 if (i
< len
+ align
&& !p
[i
])
170 p
[i
] = seek_char
- 13;
178 result
= (CHAR
*) (p
+ pos
+ align
);
179 else if (seek_char
== 0)
180 result
= (CHAR
*) (p
+ len
+ align
);
184 FOR_EACH_IMPL (impl
, 1)
185 if (CALL (impl
, (CHAR
*) (p
+ align
), seek_char
) != result
)
187 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
188 n
, impl
->name
, align
, seek_char
, len
, pos
,
189 CALL (impl
, (CHAR
*) (p
+ align
), seek_char
), result
, p
);
203 FOR_EACH_IMPL (impl
, 0)
204 printf ("\t%s", impl
->name
);
207 for (i
= 1; i
< 8; ++i
)
209 do_test (0, 16 << i
, 2048, 23, SMALL_CHAR
);
210 do_test (i
, 16 << i
, 2048, 23, SMALL_CHAR
);
213 for (i
= 1; i
< 8; ++i
)
215 do_test (i
, 64, 256, 23, SMALL_CHAR
);
216 do_test (i
, 64, 256, 23, BIG_CHAR
);
219 for (i
= 0; i
< 32; ++i
)
221 do_test (0, i
, i
+ 1, 23, SMALL_CHAR
);
222 do_test (0, i
, i
+ 1, 23, BIG_CHAR
);
225 for (i
= 1; i
< 8; ++i
)
227 do_test (0, 16 << i
, 2048, 0, SMALL_CHAR
);
228 do_test (i
, 16 << i
, 2048, 0, SMALL_CHAR
);
231 for (i
= 1; i
< 8; ++i
)
233 do_test (i
, 64, 256, 0, SMALL_CHAR
);
234 do_test (i
, 64, 256, 0, BIG_CHAR
);
237 for (i
= 0; i
< 32; ++i
)
239 do_test (0, i
, i
+ 1, 0, SMALL_CHAR
);
240 do_test (0, i
, i
+ 1, 0, BIG_CHAR
);
247 #include <support/test-driver.c>