]>
Commit | Line | Data |
---|---|---|
920a0395 | 1 | /* Test strncmp and wcsncmp functions. |
d614a753 | 2 | Copyright (C) 1999-2020 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. | |
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 | |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
58ef9ef7 RM |
19 | |
20 | #define TEST_MAIN | |
920a0395 SL |
21 | #ifdef WIDE |
22 | # define TEST_NAME "wcsncmp" | |
23 | #else | |
24 | # define TEST_NAME "strncmp" | |
25 | #endif | |
58ef9ef7 RM |
26 | #include "test-string.h" |
27 | ||
920a0395 SL |
28 | #ifdef WIDE |
29 | # include <wchar.h> | |
30 | ||
31 | # define L(str) L##str | |
32 | # define STRNCMP wcsncmp | |
33 | # define STRCPY wcscpy | |
34 | # define STRDUP wcsdup | |
35 | # define MEMCPY wmemcpy | |
36 | # define SIMPLE_STRNCMP simple_wcsncmp | |
37 | # define STUPID_STRNCMP stupid_wcsncmp | |
38 | # define CHAR wchar_t | |
39 | # define UCHAR wchar_t | |
40 | # define CHARBYTES 4 | |
41 | # define CHAR__MAX WCHAR_MAX | |
42 | # define CHAR__MIN WCHAR_MIN | |
43 | ||
44 | /* Wcsncmp uses signed semantics for comparison, not unsigned. | |
45 | Avoid using substraction since possible overflow */ | |
46 | int | |
47 | simple_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n) | |
48 | { | |
49 | wchar_t c1, c2; | |
50 | ||
51 | while (n--) | |
52 | { | |
53 | c1 = *s1++; | |
54 | c2 = *s2++; | |
55 | if (c1 == L('\0') || c1 != c2) | |
56 | return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0); | |
57 | } | |
58 | return 0; | |
59 | } | |
58ef9ef7 | 60 | |
920a0395 SL |
61 | int |
62 | stupid_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n) | |
63 | { | |
64 | wchar_t c1, c2; | |
65 | size_t ns1 = wcsnlen (s1, n) + 1, ns2 = wcsnlen (s2, n) + 1; | |
66 | ||
67 | n = ns1 < n ? ns1 : n; | |
68 | n = ns2 < n ? ns2 : n; | |
69 | ||
70 | while (n--) | |
71 | { | |
72 | c1 = *s1++; | |
73 | c2 = *s2++; | |
74 | if (c1 != c2) | |
75 | return c1 > c2 ? 1 : -1; | |
76 | } | |
77 | return 0; | |
78 | } | |
58ef9ef7 | 79 | |
920a0395 SL |
80 | #else |
81 | # define L(str) str | |
82 | # define STRNCMP strncmp | |
83 | # define STRCPY strcpy | |
84 | # define STRDUP strdup | |
85 | # define MEMCPY memcpy | |
86 | # define SIMPLE_STRNCMP simple_strncmp | |
87 | # define STUPID_STRNCMP stupid_strncmp | |
88 | # define CHAR char | |
89 | # define UCHAR unsigned char | |
90 | # define CHARBYTES 1 | |
91 | # define CHAR__MAX CHAR_MAX | |
92 | # define CHAR__MIN CHAR_MIN | |
93 | ||
94 | /* Strncmp uses unsigned semantics for comparison. */ | |
58ef9ef7 RM |
95 | int |
96 | simple_strncmp (const char *s1, const char *s2, size_t n) | |
97 | { | |
98 | int ret = 0; | |
99 | ||
100 | while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0 | |
101 | && *s1++); | |
102 | return ret; | |
103 | } | |
104 | ||
e8c1660f RM |
105 | int |
106 | stupid_strncmp (const char *s1, const char *s2, size_t n) | |
107 | { | |
9372c958 | 108 | size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1; |
e8c1660f RM |
109 | int ret = 0; |
110 | ||
111 | n = ns1 < n ? ns1 : n; | |
112 | n = ns2 < n ? ns2 : n; | |
113 | while (n-- && (ret = *(unsigned char *) s1++ - * (unsigned char *) s2++) == 0); | |
114 | return ret; | |
115 | } | |
116 | ||
920a0395 SL |
117 | #endif |
118 | ||
119 | typedef int (*proto_t) (const CHAR *, const CHAR *, size_t); | |
120 | ||
121 | IMPL (STUPID_STRNCMP, 0) | |
122 | IMPL (SIMPLE_STRNCMP, 0) | |
123 | IMPL (STRNCMP, 1) | |
124 | ||
125 | ||
6cc2b8a6 | 126 | static int |
920a0395 | 127 | check_result (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t n, |
58ef9ef7 RM |
128 | int exp_result) |
129 | { | |
130 | int result = CALL (impl, s1, s2, n); | |
131 | if ((exp_result == 0 && result != 0) | |
132 | || (exp_result < 0 && result >= 0) | |
133 | || (exp_result > 0 && result <= 0)) | |
134 | { | |
135 | error (0, 0, "Wrong result in function %s %d %d", impl->name, | |
136 | result, exp_result); | |
137 | ret = 1; | |
6cc2b8a6 | 138 | return -1; |
58ef9ef7 RM |
139 | } |
140 | ||
6cc2b8a6 L |
141 | return 0; |
142 | } | |
143 | ||
144 | static void | |
920a0395 | 145 | do_one_test (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t n, |
6cc2b8a6 L |
146 | int exp_result) |
147 | { | |
148 | if (check_result (impl, s1, s2, n, exp_result) < 0) | |
149 | return; | |
58ef9ef7 RM |
150 | } |
151 | ||
8ce9ea74 UD |
152 | static void |
153 | do_test_limit (size_t align1, size_t align2, size_t len, size_t n, int max_char, | |
154 | int exp_result) | |
155 | { | |
8f84d931 | 156 | size_t i, align_n; |
920a0395 | 157 | CHAR *s1, *s2; |
8ce9ea74 | 158 | |
5a0b6138 DM |
159 | align1 &= ~(CHARBYTES - 1); |
160 | align2 &= ~(CHARBYTES - 1); | |
161 | ||
8ce9ea74 | 162 | if (n == 0) |
8f84d931 | 163 | { |
920a0395 SL |
164 | s1 = (CHAR *) (buf1 + page_size); |
165 | s2 = (CHAR *) (buf2 + page_size); | |
786e84c5 | 166 | |
8f84d931 UD |
167 | FOR_EACH_IMPL (impl, 0) |
168 | do_one_test (impl, s1, s2, n, 0); | |
169 | ||
8f84d931 UD |
170 | return; |
171 | } | |
8ce9ea74 | 172 | |
8f84d931 UD |
173 | align1 &= 15; |
174 | align2 &= 15; | |
920a0395 | 175 | align_n = (page_size - n * CHARBYTES) & 15; |
8ce9ea74 | 176 | |
920a0395 SL |
177 | s1 = (CHAR *) (buf1 + page_size - n * CHARBYTES); |
178 | s2 = (CHAR *) (buf2 + page_size - n * CHARBYTES); | |
786e84c5 | 179 | |
8f84d931 | 180 | if (align1 < align_n) |
920a0395 | 181 | s1 = (CHAR *) ((char *) s1 - (align_n - align1)); |
786e84c5 | 182 | |
8f84d931 | 183 | if (align2 < align_n) |
920a0395 | 184 | s2 = (CHAR *) ((char *) s2 - (align_n - align2)); |
786e84c5 | 185 | |
8ce9ea74 UD |
186 | for (i = 0; i < n; i++) |
187 | s1[i] = s2[i] = 1 + 23 * i % max_char; | |
188 | ||
189 | if (len < n) | |
190 | { | |
191 | s1[len] = 0; | |
192 | s2[len] = 0; | |
193 | if (exp_result < 0) | |
194 | s2[len] = 32; | |
195 | else if (exp_result > 0) | |
196 | s1[len] = 64; | |
197 | } | |
198 | ||
8ce9ea74 UD |
199 | FOR_EACH_IMPL (impl, 0) |
200 | do_one_test (impl, s1, s2, n, exp_result); | |
8ce9ea74 UD |
201 | } |
202 | ||
58ef9ef7 RM |
203 | static void |
204 | do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char, | |
205 | int exp_result) | |
206 | { | |
207 | size_t i; | |
920a0395 | 208 | CHAR *s1, *s2; |
58ef9ef7 | 209 | |
5a0b6138 DM |
210 | align1 &= ~(CHARBYTES - 1); |
211 | align2 &= ~(CHARBYTES - 1); | |
212 | ||
58ef9ef7 RM |
213 | if (n == 0) |
214 | return; | |
215 | ||
920a0395 SL |
216 | align1 &= 63; |
217 | if (align1 + (n + 1) * CHARBYTES >= page_size) | |
58ef9ef7 RM |
218 | return; |
219 | ||
920a0395 SL |
220 | align2 &= 63; |
221 | if (align2 + (n + 1) * CHARBYTES >= page_size) | |
58ef9ef7 RM |
222 | return; |
223 | ||
920a0395 SL |
224 | s1 = (CHAR *) (buf1 + align1); |
225 | s2 = (CHAR *) (buf2 + align2); | |
58ef9ef7 RM |
226 | |
227 | for (i = 0; i < n; i++) | |
920a0395 | 228 | s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char; |
58ef9ef7 RM |
229 | |
230 | s1[n] = 24 + exp_result; | |
231 | s2[n] = 23; | |
232 | s1[len] = 0; | |
233 | s2[len] = 0; | |
234 | if (exp_result < 0) | |
235 | s2[len] = 32; | |
236 | else if (exp_result > 0) | |
237 | s1[len] = 64; | |
238 | if (len >= n) | |
239 | s2[n - 1] -= exp_result; | |
240 | ||
58ef9ef7 | 241 | FOR_EACH_IMPL (impl, 0) |
920a0395 | 242 | do_one_test (impl, s1, s2, n, exp_result); |
58ef9ef7 RM |
243 | } |
244 | ||
c97a1282 | 245 | static void |
920a0395 | 246 | do_page_test (size_t offset1, size_t offset2, CHAR *s2) |
c97a1282 | 247 | { |
920a0395 | 248 | CHAR *s1; |
c97a1282 L |
249 | int exp_result; |
250 | ||
920a0395 | 251 | if (offset1 * CHARBYTES >= page_size || offset2 * CHARBYTES >= page_size) |
c97a1282 L |
252 | return; |
253 | ||
920a0395 SL |
254 | s1 = (CHAR *) buf1; |
255 | s1 += offset1; | |
c97a1282 L |
256 | s2 += offset2; |
257 | ||
821ae713 | 258 | exp_result= *s1; |
786e84c5 | 259 | |
c97a1282 L |
260 | FOR_EACH_IMPL (impl, 0) |
261 | { | |
821ae713 AS |
262 | check_result (impl, s1, s2, page_size, -exp_result); |
263 | check_result (impl, s2, s1, page_size, exp_result); | |
c97a1282 L |
264 | } |
265 | } | |
266 | ||
58ef9ef7 RM |
267 | static void |
268 | do_random_tests (void) | |
269 | { | |
e8c1660f | 270 | size_t i, j, n, align1, align2, pos, len1, len2, size; |
9fb0cae8 RM |
271 | int result; |
272 | long r; | |
920a0395 SL |
273 | UCHAR *p1 = (UCHAR *) (buf1 + page_size - 512 * CHARBYTES); |
274 | UCHAR *p2 = (UCHAR *) (buf2 + page_size - 512 * CHARBYTES); | |
58ef9ef7 RM |
275 | |
276 | for (n = 0; n < ITERATIONS; n++) | |
277 | { | |
278 | align1 = random () & 31; | |
279 | if (random () & 1) | |
280 | align2 = random () & 31; | |
281 | else | |
282 | align2 = align1 + (random () & 24); | |
283 | pos = random () & 511; | |
284 | size = random () & 511; | |
e8c1660f | 285 | j = align1 > align2 ? align1 : align2; |
58ef9ef7 RM |
286 | if (pos + j >= 511) |
287 | pos = 510 - j - (random () & 7); | |
e8c1660f RM |
288 | len1 = random () & 511; |
289 | if (pos >= len1 && (random () & 1)) | |
290 | len1 = pos + (random () & 7); | |
291 | if (len1 + j >= 512) | |
292 | len1 = 511 - j - (random () & 7); | |
293 | if (pos >= len1) | |
294 | len2 = len1; | |
295 | else | |
296 | len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0); | |
297 | j = (pos > len2 ? pos : len2) + align1 + 64; | |
298 | if (j > 512) | |
299 | j = 512; | |
58ef9ef7 RM |
300 | for (i = 0; i < j; ++i) |
301 | { | |
302 | p1[i] = random () & 255; | |
e8c1660f | 303 | if (i < len1 + align1 && !p1[i]) |
58ef9ef7 RM |
304 | { |
305 | p1[i] = random () & 255; | |
306 | if (!p1[i]) | |
307 | p1[i] = 1 + (random () & 127); | |
308 | } | |
309 | } | |
310 | for (i = 0; i < j; ++i) | |
311 | { | |
312 | p2[i] = random () & 255; | |
e8c1660f | 313 | if (i < len2 + align2 && !p2[i]) |
58ef9ef7 RM |
314 | { |
315 | p2[i] = random () & 255; | |
316 | if (!p2[i]) | |
317 | p2[i] = 1 + (random () & 127); | |
318 | } | |
319 | } | |
320 | ||
321 | result = 0; | |
920a0395 | 322 | MEMCPY (p2 + align2, p1 + align1, pos); |
e8c1660f | 323 | if (pos < len1) |
58ef9ef7 RM |
324 | { |
325 | if (p2[align2 + pos] == p1[align1 + pos]) | |
326 | { | |
327 | p2[align2 + pos] = random () & 255; | |
328 | if (p2[align2 + pos] == p1[align1 + pos]) | |
329 | p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127); | |
330 | } | |
331 | ||
332 | if (pos < size) | |
333 | { | |
334 | if (p1[align1 + pos] < p2[align2 + pos]) | |
335 | result = -1; | |
336 | else | |
337 | result = 1; | |
338 | } | |
339 | } | |
e8c1660f RM |
340 | p1[len1 + align1] = 0; |
341 | p2[len2 + align2] = 0; | |
58ef9ef7 RM |
342 | |
343 | FOR_EACH_IMPL (impl, 1) | |
344 | { | |
920a0395 | 345 | r = CALL (impl, (CHAR *) (p1 + align1), (CHAR *) (p2 + align2), size); |
9fb0cae8 RM |
346 | /* Test whether on 64-bit architectures where ABI requires |
347 | callee to promote has the promotion been done. */ | |
348 | asm ("" : "=g" (r) : "0" (r)); | |
58ef9ef7 RM |
349 | if ((r == 0 && result) |
350 | || (r < 0 && result >= 0) | |
351 | || (r > 0 && result <= 0)) | |
352 | { | |
9fb0cae8 | 353 | error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p", |
e8c1660f | 354 | n, impl->name, align1, align2, len1, len2, pos, size, r, result, p1, p2); |
58ef9ef7 RM |
355 | ret = 1; |
356 | } | |
357 | } | |
358 | } | |
359 | } | |
360 | ||
6cc2b8a6 L |
361 | static void |
362 | check1 (void) | |
363 | { | |
920a0395 SL |
364 | CHAR *s1 = (CHAR *) (buf1 + 0xb2c); |
365 | CHAR *s2 = (CHAR *) (buf1 + 0xfd8); | |
366 | size_t i, offset; | |
6cc2b8a6 L |
367 | int exp_result; |
368 | ||
920a0395 SL |
369 | STRCPY(s1, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs")); |
370 | STRCPY(s2, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV")); | |
371 | ||
372 | /* Check possible overflow bug for wcsncmp */ | |
373 | s1[4] = CHAR__MAX; | |
374 | s2[4] = CHAR__MIN; | |
6cc2b8a6 | 375 | |
920a0395 | 376 | for (offset = 0; offset < 6; offset++) |
6cc2b8a6 | 377 | { |
920a0395 SL |
378 | for (i = 0; i < 80; i++) |
379 | { | |
380 | exp_result = SIMPLE_STRNCMP (s1 + offset, s2 + offset, i); | |
381 | FOR_EACH_IMPL (impl, 0) | |
382 | check_result (impl, s1 + offset, s2 + offset, i, exp_result); | |
383 | } | |
6cc2b8a6 L |
384 | } |
385 | } | |
386 | ||
c97a1282 L |
387 | static void |
388 | check2 (void) | |
389 | { | |
390 | size_t i; | |
920a0395 | 391 | CHAR *s1, *s2; |
c97a1282 | 392 | |
920a0395 SL |
393 | s1 = (CHAR *) buf1; |
394 | for (i = 0; i < (page_size / CHARBYTES) - 1; i++) | |
c97a1282 L |
395 | s1[i] = 23; |
396 | s1[i] = 0; | |
397 | ||
920a0395 | 398 | s2 = STRDUP (s1); |
c97a1282 L |
399 | |
400 | for (i = 0; i < 64; ++i) | |
920a0395 | 401 | do_page_test ((3988 / CHARBYTES) + i, (2636 / CHARBYTES), s2); |
c97a1282 L |
402 | |
403 | free (s2); | |
404 | } | |
405 | ||
58ef9ef7 RM |
406 | int |
407 | test_main (void) | |
408 | { | |
409 | size_t i; | |
410 | ||
411 | test_init (); | |
412 | ||
6cc2b8a6 | 413 | check1 (); |
c97a1282 | 414 | check2 (); |
6cc2b8a6 | 415 | |
58ef9ef7 RM |
416 | printf ("%23s", ""); |
417 | FOR_EACH_IMPL (impl, 0) | |
418 | printf ("\t%s", impl->name); | |
419 | putchar ('\n'); | |
420 | ||
421 | for (i =0; i < 16; ++i) | |
422 | { | |
423 | do_test (0, 0, 8, i, 127, 0); | |
424 | do_test (0, 0, 8, i, 127, -1); | |
425 | do_test (0, 0, 8, i, 127, 1); | |
426 | do_test (i, i, 8, i, 127, 0); | |
427 | do_test (i, i, 8, i, 127, 1); | |
428 | do_test (i, i, 8, i, 127, -1); | |
429 | do_test (i, 2 * i, 8, i, 127, 0); | |
430 | do_test (2 * i, i, 8, i, 127, 1); | |
431 | do_test (i, 3 * i, 8, i, 127, -1); | |
432 | do_test (0, 0, 8, i, 255, 0); | |
433 | do_test (0, 0, 8, i, 255, -1); | |
434 | do_test (0, 0, 8, i, 255, 1); | |
435 | do_test (i, i, 8, i, 255, 0); | |
436 | do_test (i, i, 8, i, 255, 1); | |
437 | do_test (i, i, 8, i, 255, -1); | |
438 | do_test (i, 2 * i, 8, i, 255, 0); | |
439 | do_test (2 * i, i, 8, i, 255, 1); | |
440 | do_test (i, 3 * i, 8, i, 255, -1); | |
441 | } | |
442 | ||
443 | for (i = 1; i < 8; ++i) | |
444 | { | |
445 | do_test (0, 0, 8 << i, 16 << i, 127, 0); | |
446 | do_test (0, 0, 8 << i, 16 << i, 127, 1); | |
447 | do_test (0, 0, 8 << i, 16 << i, 127, -1); | |
448 | do_test (0, 0, 8 << i, 16 << i, 255, 0); | |
449 | do_test (0, 0, 8 << i, 16 << i, 255, 1); | |
450 | do_test (0, 0, 8 << i, 16 << i, 255, -1); | |
451 | do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 0); | |
452 | do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 1); | |
453 | do_test (2 * i, i, 8 << i, 16 << i, 255, 0); | |
454 | do_test (2 * i, i, 8 << i, 16 << i, 255, 1); | |
455 | } | |
786e84c5 | 456 | |
8f84d931 UD |
457 | do_test_limit (0, 0, 0, 0, 127, 0); |
458 | do_test_limit (4, 0, 21, 20, 127, 0); | |
459 | do_test_limit (0, 4, 21, 20, 127, 0); | |
460 | do_test_limit (8, 0, 25, 24, 127, 0); | |
461 | do_test_limit (0, 8, 25, 24, 127, 0); | |
462 | ||
463 | for (i = 0; i < 8; ++i) | |
8ce9ea74 UD |
464 | { |
465 | do_test_limit (0, 0, 17 - i, 16 - i, 127, 0); | |
466 | do_test_limit (0, 0, 17 - i, 16 - i, 255, 0); | |
467 | do_test_limit (0, 0, 15 - i, 16 - i, 127, 0); | |
468 | do_test_limit (0, 0, 15 - i, 16 - i, 127, 1); | |
469 | do_test_limit (0, 0, 15 - i, 16 - i, 127, -1); | |
470 | do_test_limit (0, 0, 15 - i, 16 - i, 255, 0); | |
471 | do_test_limit (0, 0, 15 - i, 16 - i, 255, 1); | |
472 | do_test_limit (0, 0, 15 - i, 16 - i, 255, -1); | |
473 | } | |
474 | ||
58ef9ef7 RM |
475 | do_random_tests (); |
476 | return ret; | |
477 | } | |
478 | ||
fb82116f | 479 | #include <support/test-driver.c> |