]>
Commit | Line | Data |
---|---|---|
5b330a2d | 1 | /* Test and measure STRCHR functions. |
688903eb | 2 | Copyright (C) 1999-2018 Free Software Foundation, Inc. |
58ef9ef7 RM |
3 | This file is part of the GNU C Library. |
4 | Written by Jakub Jelinek <jakub@redhat.com>, 1999. | |
df33a8fb UD |
5 | Added wcsrrchr support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, |
6 | 2011. | |
58ef9ef7 RM |
7 | |
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. | |
12 | ||
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. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
19 | License along with the GNU C Library; if not, see |
20 | <http://www.gnu.org/licenses/>. */ | |
58ef9ef7 RM |
21 | |
22 | #define TEST_MAIN | |
69f07e5f L |
23 | #ifdef WIDE |
24 | # define TEST_NAME "wcsrchr" | |
25 | #else | |
26 | # define TEST_NAME "strrchr" | |
27 | #endif | |
58ef9ef7 RM |
28 | #include "test-string.h" |
29 | ||
5b330a2d UD |
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 | |
46 | ||
47 | typedef CHAR *(*proto_t) (const CHAR *, int); | |
48 | CHAR *SIMPLE_STRRCHR (const CHAR *, int); | |
49 | ||
50 | IMPL (SIMPLE_STRRCHR, 0) | |
51 | IMPL (STRRCHR, 1) | |
52 | ||
53 | CHAR * | |
54 | SIMPLE_STRRCHR (const CHAR *s, int c) | |
58ef9ef7 | 55 | { |
5b330a2d | 56 | const CHAR *ret = NULL; |
58ef9ef7 RM |
57 | |
58 | for (; *s != '\0'; ++s) | |
5b330a2d | 59 | if (*s == (CHAR) c) |
58ef9ef7 RM |
60 | ret = s; |
61 | ||
5b330a2d | 62 | return (CHAR *) (c == '\0' ? s : ret); |
58ef9ef7 RM |
63 | } |
64 | ||
65 | static void | |
5b330a2d | 66 | do_one_test (impl_t *impl, const CHAR *s, int c, CHAR *exp_res) |
58ef9ef7 | 67 | { |
5b330a2d | 68 | CHAR *res = CALL (impl, s, c); |
58ef9ef7 RM |
69 | if (res != exp_res) |
70 | { | |
71 | error (0, 0, "Wrong result in function %s %p %p", impl->name, | |
72 | res, exp_res); | |
73 | ret = 1; | |
74 | return; | |
75 | } | |
58ef9ef7 RM |
76 | } |
77 | ||
78 | static void | |
79 | do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char) | |
5b330a2d UD |
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)) | |
a2d18b64 | 82 | len for wcschr here isn't in bytes but it's number of wchar_t symbols. */ |
58ef9ef7 RM |
83 | { |
84 | size_t i; | |
5b330a2d UD |
85 | CHAR *result; |
86 | CHAR *buf = (CHAR *) buf1; | |
a2d18b64 | 87 | |
58ef9ef7 | 88 | align &= 7; |
5b330a2d | 89 | if ( (align + len) * sizeof(CHAR) >= page_size) |
58ef9ef7 | 90 | return; |
c9df3df9 | 91 | |
58ef9ef7 RM |
92 | for (i = 0; i < len; ++i) |
93 | { | |
5b330a2d UD |
94 | buf[align + i] = (random () * random ()) & max_char; |
95 | if (!buf[align + i]) | |
96 | buf[align + i] = (random () * random ()) & max_char; | |
97 | if (!buf[align + i]) | |
a2d18b64 | 98 | buf[align + i] = 1; |
5b330a2d UD |
99 | if ((i > pos || pos >= len) && buf[align + i] == seek_char) |
100 | buf[align + i] = seek_char + 10 + (random () & 15); | |
58ef9ef7 | 101 | } |
5b330a2d | 102 | buf[align + len] = 0; |
58ef9ef7 RM |
103 | |
104 | if (pos < len) | |
105 | { | |
5b330a2d UD |
106 | buf[align + pos] = seek_char; |
107 | result = (CHAR *) (buf + align + pos); | |
58ef9ef7 RM |
108 | } |
109 | else if (seek_char == 0) | |
5b330a2d | 110 | result = (CHAR *) (buf + align + len); |
58ef9ef7 RM |
111 | else |
112 | result = NULL; | |
113 | ||
58ef9ef7 | 114 | FOR_EACH_IMPL (impl, 0) |
5b330a2d | 115 | do_one_test (impl, (CHAR *) (buf + align), seek_char, result); |
58ef9ef7 RM |
116 | } |
117 | ||
118 | static void | |
119 | do_random_tests (void) | |
120 | { | |
121 | size_t i, j, n, align, pos, len; | |
122 | int seek_char; | |
5b330a2d UD |
123 | CHAR *result; |
124 | UCHAR *p = (UCHAR *) (buf1 + page_size) - 512; | |
58ef9ef7 RM |
125 | |
126 | for (n = 0; n < ITERATIONS; n++) | |
127 | { | |
5b330a2d | 128 | align = random () & (63 / sizeof(CHAR)); |
df33a8fb | 129 | /* For wcsrchr: align here means align not in bytes, but in wchar_ts, |
5b330a2d | 130 | in bytes it will equal to align * (sizeof (wchar_t)). |
df33a8fb UD |
131 | For strrchr we need to check all alignments from 0 to 63 since |
132 | some assembly implementations have separate prolog for alignments | |
133 | more 48. */ | |
58ef9ef7 RM |
134 | pos = random () & 511; |
135 | if (pos + align >= 511) | |
136 | pos = 510 - align - (random () & 7); | |
137 | len = random () & 511; | |
df33a8fb | 138 | /* len for wcschr here isn't in bytes but it's number of wchar_t |
5b330a2d | 139 | symbols. */ |
58ef9ef7 RM |
140 | if (pos >= len) |
141 | len = pos + (random () & 7); | |
142 | if (len + align >= 512) | |
a2d18b64 | 143 | len = 511 - align - (random () & 7); |
58ef9ef7 RM |
144 | seek_char = random () & 255; |
145 | if (seek_char && pos == len) | |
146 | { | |
147 | if (pos) | |
148 | --pos; | |
149 | else | |
150 | ++len; | |
151 | } | |
152 | j = len + align + 64; | |
153 | if (j > 512) | |
a2d18b64 | 154 | j = 512; |
58ef9ef7 RM |
155 | |
156 | for (i = 0; i < j; i++) | |
157 | { | |
158 | if (i == pos + align) | |
159 | p[i] = seek_char; | |
160 | else if (i == len + align) | |
161 | p[i] = 0; | |
162 | else | |
163 | { | |
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]) | |
169 | { | |
170 | p[i] = seek_char - 13; | |
171 | if (!p[i]) | |
172 | p[i] = 140; | |
173 | } | |
174 | } | |
175 | } | |
176 | ||
177 | if (pos <= len) | |
5b330a2d | 178 | result = (CHAR *) (p + pos + align); |
58ef9ef7 | 179 | else if (seek_char == 0) |
a2d18b64 | 180 | result = (CHAR *) (p + len + align); |
58ef9ef7 RM |
181 | else |
182 | result = NULL; | |
183 | ||
184 | FOR_EACH_IMPL (impl, 1) | |
5b330a2d | 185 | if (CALL (impl, (CHAR *) (p + align), seek_char) != result) |
58ef9ef7 RM |
186 | { |
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, | |
5b330a2d | 189 | CALL (impl, (CHAR *) (p + align), seek_char), result, p); |
58ef9ef7 RM |
190 | ret = 1; |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | int | |
196 | test_main (void) | |
197 | { | |
198 | size_t i; | |
199 | ||
200 | test_init (); | |
201 | ||
202 | printf ("%20s", ""); | |
203 | FOR_EACH_IMPL (impl, 0) | |
204 | printf ("\t%s", impl->name); | |
205 | putchar ('\n'); | |
206 | ||
207 | for (i = 1; i < 8; ++i) | |
208 | { | |
5b330a2d UD |
209 | do_test (0, 16 << i, 2048, 23, SMALL_CHAR); |
210 | do_test (i, 16 << i, 2048, 23, SMALL_CHAR); | |
58ef9ef7 RM |
211 | } |
212 | ||
213 | for (i = 1; i < 8; ++i) | |
214 | { | |
5b330a2d UD |
215 | do_test (i, 64, 256, 23, SMALL_CHAR); |
216 | do_test (i, 64, 256, 23, BIG_CHAR); | |
58ef9ef7 RM |
217 | } |
218 | ||
219 | for (i = 0; i < 32; ++i) | |
220 | { | |
5b330a2d UD |
221 | do_test (0, i, i + 1, 23, SMALL_CHAR); |
222 | do_test (0, i, i + 1, 23, BIG_CHAR); | |
58ef9ef7 RM |
223 | } |
224 | ||
225 | for (i = 1; i < 8; ++i) | |
226 | { | |
5b330a2d UD |
227 | do_test (0, 16 << i, 2048, 0, SMALL_CHAR); |
228 | do_test (i, 16 << i, 2048, 0, SMALL_CHAR); | |
58ef9ef7 RM |
229 | } |
230 | ||
231 | for (i = 1; i < 8; ++i) | |
232 | { | |
5b330a2d UD |
233 | do_test (i, 64, 256, 0, SMALL_CHAR); |
234 | do_test (i, 64, 256, 0, BIG_CHAR); | |
58ef9ef7 RM |
235 | } |
236 | ||
237 | for (i = 0; i < 32; ++i) | |
238 | { | |
5b330a2d UD |
239 | do_test (0, i, i + 1, 0, SMALL_CHAR); |
240 | do_test (0, i, i + 1, 0, BIG_CHAR); | |
58ef9ef7 RM |
241 | } |
242 | ||
243 | do_random_tests (); | |
244 | return ret; | |
245 | } | |
246 | ||
fb82116f | 247 | #include <support/test-driver.c> |