]>
Commit | Line | Data |
---|---|---|
e9f82e0d UD |
1 | /* Test and measure strncasecmp functions. |
2 | Copyright (C) 1999, 2002, 2003, 2005, 2010 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | Written by Jakub Jelinek <jakub@redhat.com>, 1999. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
17 | License along with the GNU C Library; if not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
19 | 02111-1307 USA. */ | |
20 | ||
21 | #include <ctype.h> | |
22 | #define TEST_MAIN | |
23 | #include "test-string.h" | |
24 | ||
25 | typedef int (*proto_t) (const char *, const char *, size_t); | |
26 | static int simple_strncasecmp (const char *, const char *, size_t); | |
27 | static int stupid_strncasecmp (const char *, const char *, size_t); | |
28 | ||
29 | IMPL (stupid_strncasecmp, 0) | |
30 | IMPL (simple_strncasecmp, 0) | |
31 | IMPL (strncasecmp, 1) | |
32 | ||
33 | static int | |
34 | simple_strncasecmp (const char *s1, const char *s2, size_t n) | |
35 | { | |
36 | int ret; | |
37 | ||
38 | if (n == 0) | |
39 | return 0; | |
40 | ||
41 | while ((ret = ((unsigned char) tolower (*s1) | |
42 | - (unsigned char) tolower (*s2))) == 0 | |
43 | && *s1++) | |
44 | { | |
45 | if (--n == 0) | |
46 | return 0; | |
47 | ++s2; | |
48 | } | |
49 | return ret; | |
50 | } | |
51 | ||
52 | static int | |
53 | stupid_strncasecmp (const char *s1, const char *s2, size_t max) | |
54 | { | |
55 | size_t ns1 = strlen (s1) + 1; | |
56 | size_t ns2 = strlen (s2) + 1; | |
57 | size_t n = ns1 < ns2 ? ns1 : ns2; | |
58 | if (n > max) | |
59 | n = max; | |
60 | int ret = 0; | |
61 | ||
62 | while (n--) | |
63 | { | |
64 | if ((ret = ((unsigned char) tolower (*s1) | |
65 | - (unsigned char) tolower (*s2))) != 0) | |
66 | break; | |
67 | ++s1; | |
68 | ++s2; | |
69 | } | |
70 | return ret; | |
71 | } | |
72 | ||
8ca52c6e L |
73 | static int |
74 | check_result (impl_t *impl, const char *s1, const char *s2, size_t n, | |
75 | int exp_result) | |
e9f82e0d UD |
76 | { |
77 | int result = CALL (impl, s1, s2, n); | |
78 | if ((exp_result == 0 && result != 0) | |
79 | || (exp_result < 0 && result >= 0) | |
80 | || (exp_result > 0 && result <= 0)) | |
81 | { | |
82 | error (0, 0, "Wrong result in function %s %d %d", impl->name, | |
83 | result, exp_result); | |
84 | ret = 1; | |
8ca52c6e | 85 | return -1; |
e9f82e0d UD |
86 | } |
87 | ||
8ca52c6e L |
88 | return 0; |
89 | } | |
90 | ||
91 | static void | |
92 | do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n, | |
93 | int exp_result) | |
94 | { | |
95 | if (check_result (impl, s1, s2, n, exp_result) < 0) | |
96 | return; | |
97 | ||
e9f82e0d UD |
98 | if (HP_TIMING_AVAIL) |
99 | { | |
100 | hp_timing_t start __attribute ((unused)); | |
101 | hp_timing_t stop __attribute ((unused)); | |
102 | hp_timing_t best_time = ~ (hp_timing_t) 0; | |
103 | size_t i; | |
104 | ||
105 | for (i = 0; i < 32; ++i) | |
106 | { | |
107 | HP_TIMING_NOW (start); | |
108 | CALL (impl, s1, s2, n); | |
109 | HP_TIMING_NOW (stop); | |
110 | HP_TIMING_BEST (best_time, start, stop); | |
111 | } | |
112 | ||
113 | printf ("\t%zd", (size_t) best_time); | |
114 | } | |
115 | } | |
116 | ||
117 | static void | |
118 | do_test (size_t align1, size_t align2, size_t n, size_t len, int max_char, | |
119 | int exp_result) | |
120 | { | |
121 | size_t i; | |
122 | char *s1, *s2; | |
123 | ||
124 | if (len == 0) | |
125 | return; | |
126 | ||
127 | align1 &= 7; | |
128 | if (align1 + len + 1 >= page_size) | |
129 | return; | |
130 | ||
131 | align2 &= 7; | |
132 | if (align2 + len + 1 >= page_size) | |
133 | return; | |
134 | ||
135 | s1 = (char *) (buf1 + align1); | |
136 | s2 = (char *) (buf2 + align2); | |
137 | ||
138 | for (i = 0; i < len; i++) | |
139 | { | |
140 | s1[i] = toupper (1 + 23 * i % max_char); | |
141 | s2[i] = tolower (s1[i]); | |
142 | } | |
143 | ||
144 | s1[len] = s2[len] = 0; | |
145 | s1[len + 1] = 23; | |
146 | s2[len + 1] = 24 + exp_result; | |
147 | if ((s2[len - 1] == 'z' && exp_result == -1) | |
148 | || (s2[len - 1] == 'a' && exp_result == 1)) | |
149 | s1[len - 1] += exp_result; | |
150 | else | |
151 | s2[len - 1] -= exp_result; | |
152 | ||
153 | if (HP_TIMING_AVAIL) | |
154 | printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2); | |
155 | ||
156 | FOR_EACH_IMPL (impl, 0) | |
157 | do_one_test (impl, s1, s2, n, exp_result); | |
158 | ||
159 | if (HP_TIMING_AVAIL) | |
160 | putchar ('\n'); | |
161 | } | |
162 | ||
163 | static void | |
164 | do_random_tests (void) | |
165 | { | |
166 | size_t i, j, n, align1, align2, pos, len1, len2; | |
167 | int result; | |
168 | long r; | |
169 | unsigned char *p1 = buf1 + page_size - 512; | |
170 | unsigned char *p2 = buf2 + page_size - 512; | |
171 | ||
172 | for (n = 0; n < ITERATIONS; n++) | |
173 | { | |
174 | align1 = random () & 31; | |
175 | if (random () & 1) | |
176 | align2 = random () & 31; | |
177 | else | |
178 | align2 = align1 + (random () & 24); | |
179 | pos = random () & 511; | |
180 | j = align1 > align2 ? align1 : align2; | |
181 | if (pos + j >= 511) | |
182 | pos = 510 - j - (random () & 7); | |
183 | len1 = random () & 511; | |
184 | if (pos >= len1 && (random () & 1)) | |
185 | len1 = pos + (random () & 7); | |
186 | if (len1 + j >= 512) | |
187 | len1 = 511 - j - (random () & 7); | |
188 | if (pos >= len1) | |
189 | len2 = len1; | |
190 | else | |
191 | len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0); | |
192 | j = (pos > len2 ? pos : len2) + align1 + 64; | |
193 | if (j > 512) | |
194 | j = 512; | |
195 | for (i = 0; i < j; ++i) | |
196 | { | |
197 | p1[i] = tolower (random () & 255); | |
198 | if (i < len1 + align1 && !p1[i]) | |
199 | { | |
200 | p1[i] = tolower (random () & 255); | |
201 | if (!p1[i]) | |
202 | p1[i] = tolower (1 + (random () & 127)); | |
203 | } | |
204 | } | |
205 | for (i = 0; i < j; ++i) | |
206 | { | |
207 | p2[i] = toupper (random () & 255); | |
208 | if (i < len2 + align2 && !p2[i]) | |
209 | { | |
210 | p2[i] = toupper (random () & 255); | |
211 | if (!p2[i]) | |
212 | toupper (p2[i] = 1 + (random () & 127)); | |
213 | } | |
214 | } | |
215 | ||
216 | result = 0; | |
217 | memcpy (p2 + align2, p1 + align1, pos); | |
218 | if (pos < len1) | |
219 | { | |
220 | if (tolower (p2[align2 + pos]) == p1[align1 + pos]) | |
221 | { | |
222 | p2[align2 + pos] = toupper (random () & 255); | |
223 | if (tolower (p2[align2 + pos]) == p1[align1 + pos]) | |
224 | p2[align2 + pos] = toupper (p1[align1 + pos] | |
225 | + 3 + (random () & 127)); | |
226 | } | |
227 | ||
228 | if (p1[align1 + pos] < tolower (p2[align2 + pos])) | |
229 | result = -1; | |
230 | else | |
231 | result = 1; | |
232 | } | |
233 | p1[len1 + align1] = 0; | |
234 | p2[len2 + align2] = 0; | |
235 | ||
236 | FOR_EACH_IMPL (impl, 1) | |
237 | { | |
238 | r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2), | |
239 | pos + 1 + (random () & 255)); | |
240 | /* Test whether on 64-bit architectures where ABI requires | |
241 | callee to promote has the promotion been done. */ | |
242 | asm ("" : "=g" (r) : "0" (r)); | |
243 | if ((r == 0 && result) | |
244 | || (r < 0 && result >= 0) | |
245 | || (r > 0 && result <= 0)) | |
246 | { | |
247 | error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p", | |
248 | n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2); | |
249 | ret = 1; | |
250 | } | |
251 | } | |
252 | } | |
253 | } | |
254 | ||
8ca52c6e L |
255 | |
256 | static void | |
257 | check1 (void) | |
258 | { | |
259 | static char cp [4096+16] __attribute__ ((aligned(4096))); | |
260 | static char gotrel[4096] __attribute__ ((aligned(4096))); | |
261 | char *s1 = cp + 0xffa; | |
262 | char *s2 = gotrel + 0xcbe; | |
263 | int exp_result; | |
264 | size_t n = 6; | |
265 | ||
266 | strcpy (s1, "gottpoff"); | |
267 | strcpy (s2, "GOTPLT"); | |
268 | ||
269 | exp_result = simple_strncasecmp (s1, s2, n); | |
270 | FOR_EACH_IMPL (impl, 0) | |
271 | check_result (impl, s1, s2, n, exp_result); | |
272 | } | |
273 | ||
e9f82e0d UD |
274 | int |
275 | test_main (void) | |
276 | { | |
277 | size_t i; | |
278 | ||
279 | test_init (); | |
280 | ||
8ca52c6e L |
281 | check1 (); |
282 | ||
e9f82e0d UD |
283 | printf ("%23s", ""); |
284 | FOR_EACH_IMPL (impl, 0) | |
285 | printf ("\t%s", impl->name); | |
286 | putchar ('\n'); | |
287 | ||
288 | for (i = 1; i < 16; ++i) | |
289 | { | |
290 | do_test (i, i, i - 1, i, 127, 0); | |
291 | ||
292 | do_test (i, i, i, i, 127, 0); | |
293 | do_test (i, i, i, i, 127, 1); | |
294 | do_test (i, i, i, i, 127, -1); | |
295 | ||
296 | do_test (i, i, i + 1, i, 127, 0); | |
297 | do_test (i, i, i + 1, i, 127, 1); | |
298 | do_test (i, i, i + 1, i, 127, -1); | |
299 | } | |
300 | ||
301 | for (i = 1; i < 10; ++i) | |
302 | { | |
303 | do_test (0, 0, (2 << i) - 1, 2 << i, 127, 0); | |
304 | do_test (0, 0, 2 << i, 2 << i, 254, 0); | |
305 | do_test (0, 0, (2 << i) + 1, 2 << i, 127, 0); | |
306 | ||
307 | do_test (0, 0, (2 << i) + 1, 2 << i, 254, 0); | |
308 | ||
309 | do_test (0, 0, 2 << i, 2 << i, 127, 1); | |
310 | do_test (0, 0, (2 << i) + 10, 2 << i, 127, 1); | |
311 | ||
312 | do_test (0, 0, 2 << i, 2 << i, 254, 1); | |
313 | do_test (0, 0, (2 << i) + 10, 2 << i, 254, 1); | |
314 | ||
315 | do_test (0, 0, 2 << i, 2 << i, 127, -1); | |
316 | do_test (0, 0, (2 << i) + 10, 2 << i, 127, -1); | |
317 | ||
318 | do_test (0, 0, 2 << i, 2 << i, 254, -1); | |
319 | do_test (0, 0, (2 << i) + 10, 2 << i, 254, -1); | |
320 | } | |
321 | ||
322 | for (i = 1; i < 8; ++i) | |
323 | { | |
324 | do_test (i, 2 * i, (8 << i) - 1, 8 << i, 127, 0); | |
325 | do_test (i, 2 * i, 8 << i, 8 << i, 127, 0); | |
326 | do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 0); | |
327 | ||
328 | do_test (2 * i, i, (8 << i) - 1, 8 << i, 254, 0); | |
329 | do_test (2 * i, i, 8 << i, 8 << i, 254, 0); | |
330 | do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 0); | |
331 | ||
332 | do_test (i, 2 * i, 8 << i, 8 << i, 127, 1); | |
333 | do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 1); | |
334 | ||
335 | do_test (2 * i, i, 8 << i, 8 << i, 254, 1); | |
336 | do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 1); | |
337 | ||
338 | do_test (i, 2 * i, 8 << i, 8 << i, 127, -1); | |
339 | do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, -1); | |
340 | ||
341 | do_test (2 * i, i, 8 << i, 8 << i, 254, -1); | |
342 | do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, -1); | |
343 | } | |
344 | ||
345 | do_random_tests (); | |
346 | return ret; | |
347 | } | |
348 | ||
349 | #include "../test-skeleton.c" |