S390: Add new s390 platform.
[glibc.git] / string / test-strncmp.c
blobfb57a9bdc5b5f9d40e1b60a8c9d9a72139e9e541
1 /* Test strncmp and wcsncmp functions.
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #define TEST_MAIN
21 #ifdef WIDE
22 # define TEST_NAME "wcsncmp"
23 #else
24 # define TEST_NAME "strncmp"
25 #endif
26 #include "test-string.h"
28 #ifdef WIDE
29 # include <wchar.h>
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
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)
49 wchar_t c1, c2;
51 while (n--)
53 c1 = *s1++;
54 c2 = *s2++;
55 if (c1 == L('\0') || c1 != c2)
56 return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0);
58 return 0;
61 int
62 stupid_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n)
64 wchar_t c1, c2;
65 size_t ns1 = wcsnlen (s1, n) + 1, ns2 = wcsnlen (s2, n) + 1;
67 n = ns1 < n ? ns1 : n;
68 n = ns2 < n ? ns2 : n;
70 while (n--)
72 c1 = *s1++;
73 c2 = *s2++;
74 if (c1 != c2)
75 return c1 > c2 ? 1 : -1;
77 return 0;
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
94 /* Strncmp uses unsigned semantics for comparison. */
95 int
96 simple_strncmp (const char *s1, const char *s2, size_t n)
98 int ret = 0;
100 while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
101 && *s1++);
102 return ret;
106 stupid_strncmp (const char *s1, const char *s2, size_t n)
108 size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1;
109 int ret = 0;
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;
117 #endif
119 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
121 IMPL (STUPID_STRNCMP, 0)
122 IMPL (SIMPLE_STRNCMP, 0)
123 IMPL (STRNCMP, 1)
126 static int
127 check_result (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t n,
128 int exp_result)
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))
135 error (0, 0, "Wrong result in function %s %d %d", impl->name,
136 result, exp_result);
137 ret = 1;
138 return -1;
141 return 0;
144 static void
145 do_one_test (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t n,
146 int exp_result)
148 if (check_result (impl, s1, s2, n, exp_result) < 0)
149 return;
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)
156 size_t i, align_n;
157 CHAR *s1, *s2;
159 if (n == 0)
161 s1 = (CHAR *) (buf1 + page_size);
162 s2 = (CHAR *) (buf2 + page_size);
164 FOR_EACH_IMPL (impl, 0)
165 do_one_test (impl, s1, s2, n, 0);
167 return;
170 align1 &= 15;
171 align2 &= 15;
172 align_n = (page_size - n * CHARBYTES) & 15;
174 s1 = (CHAR *) (buf1 + page_size - n * CHARBYTES);
175 s2 = (CHAR *) (buf2 + page_size - n * CHARBYTES);
177 if (align1 < align_n)
178 s1 = (CHAR *) ((char *) s1 - (align_n - align1));
180 if (align2 < align_n)
181 s2 = (CHAR *) ((char *) s2 - (align_n - align2));
183 for (i = 0; i < n; i++)
184 s1[i] = s2[i] = 1 + 23 * i % max_char;
186 if (len < n)
188 s1[len] = 0;
189 s2[len] = 0;
190 if (exp_result < 0)
191 s2[len] = 32;
192 else if (exp_result > 0)
193 s1[len] = 64;
196 FOR_EACH_IMPL (impl, 0)
197 do_one_test (impl, s1, s2, n, exp_result);
200 static void
201 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char,
202 int exp_result)
204 size_t i;
205 CHAR *s1, *s2;
207 if (n == 0)
208 return;
210 align1 &= 63;
211 if (align1 + (n + 1) * CHARBYTES >= page_size)
212 return;
214 align2 &= 63;
215 if (align2 + (n + 1) * CHARBYTES >= page_size)
216 return;
218 s1 = (CHAR *) (buf1 + align1);
219 s2 = (CHAR *) (buf2 + align2);
221 for (i = 0; i < n; i++)
222 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
224 s1[n] = 24 + exp_result;
225 s2[n] = 23;
226 s1[len] = 0;
227 s2[len] = 0;
228 if (exp_result < 0)
229 s2[len] = 32;
230 else if (exp_result > 0)
231 s1[len] = 64;
232 if (len >= n)
233 s2[n - 1] -= exp_result;
235 FOR_EACH_IMPL (impl, 0)
236 do_one_test (impl, s1, s2, n, exp_result);
239 static void
240 do_page_test (size_t offset1, size_t offset2, CHAR *s2)
242 CHAR *s1;
243 int exp_result;
245 if (offset1 * CHARBYTES >= page_size || offset2 * CHARBYTES >= page_size)
246 return;
248 s1 = (CHAR *) buf1;
249 s1 += offset1;
250 s2 += offset2;
252 exp_result= *s1;
254 FOR_EACH_IMPL (impl, 0)
256 check_result (impl, s1, s2, page_size, -exp_result);
257 check_result (impl, s2, s1, page_size, exp_result);
261 static void
262 do_random_tests (void)
264 size_t i, j, n, align1, align2, pos, len1, len2, size;
265 int result;
266 long r;
267 UCHAR *p1 = (UCHAR *) (buf1 + page_size - 512 * CHARBYTES);
268 UCHAR *p2 = (UCHAR *) (buf2 + page_size - 512 * CHARBYTES);
270 for (n = 0; n < ITERATIONS; n++)
272 align1 = random () & 31;
273 if (random () & 1)
274 align2 = random () & 31;
275 else
276 align2 = align1 + (random () & 24);
277 pos = random () & 511;
278 size = random () & 511;
279 j = align1 > align2 ? align1 : align2;
280 if (pos + j >= 511)
281 pos = 510 - j - (random () & 7);
282 len1 = random () & 511;
283 if (pos >= len1 && (random () & 1))
284 len1 = pos + (random () & 7);
285 if (len1 + j >= 512)
286 len1 = 511 - j - (random () & 7);
287 if (pos >= len1)
288 len2 = len1;
289 else
290 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
291 j = (pos > len2 ? pos : len2) + align1 + 64;
292 if (j > 512)
293 j = 512;
294 for (i = 0; i < j; ++i)
296 p1[i] = random () & 255;
297 if (i < len1 + align1 && !p1[i])
299 p1[i] = random () & 255;
300 if (!p1[i])
301 p1[i] = 1 + (random () & 127);
304 for (i = 0; i < j; ++i)
306 p2[i] = random () & 255;
307 if (i < len2 + align2 && !p2[i])
309 p2[i] = random () & 255;
310 if (!p2[i])
311 p2[i] = 1 + (random () & 127);
315 result = 0;
316 MEMCPY (p2 + align2, p1 + align1, pos);
317 if (pos < len1)
319 if (p2[align2 + pos] == p1[align1 + pos])
321 p2[align2 + pos] = random () & 255;
322 if (p2[align2 + pos] == p1[align1 + pos])
323 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
326 if (pos < size)
328 if (p1[align1 + pos] < p2[align2 + pos])
329 result = -1;
330 else
331 result = 1;
334 p1[len1 + align1] = 0;
335 p2[len2 + align2] = 0;
337 FOR_EACH_IMPL (impl, 1)
339 r = CALL (impl, (CHAR *) (p1 + align1), (CHAR *) (p2 + align2), size);
340 /* Test whether on 64-bit architectures where ABI requires
341 callee to promote has the promotion been done. */
342 asm ("" : "=g" (r) : "0" (r));
343 if ((r == 0 && result)
344 || (r < 0 && result >= 0)
345 || (r > 0 && result <= 0))
347 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
348 n, impl->name, align1, align2, len1, len2, pos, size, r, result, p1, p2);
349 ret = 1;
355 static void
356 check1 (void)
358 CHAR *s1 = (CHAR *) (buf1 + 0xb2c);
359 CHAR *s2 = (CHAR *) (buf1 + 0xfd8);
360 size_t i, offset;
361 int exp_result;
363 STRCPY(s1, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
364 STRCPY(s2, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
366 /* Check possible overflow bug for wcsncmp */
367 s1[4] = CHAR__MAX;
368 s2[4] = CHAR__MIN;
370 for (offset = 0; offset < 6; offset++)
372 for (i = 0; i < 80; i++)
374 exp_result = SIMPLE_STRNCMP (s1 + offset, s2 + offset, i);
375 FOR_EACH_IMPL (impl, 0)
376 check_result (impl, s1 + offset, s2 + offset, i, exp_result);
381 static void
382 check2 (void)
384 size_t i;
385 CHAR *s1, *s2;
387 s1 = (CHAR *) buf1;
388 for (i = 0; i < (page_size / CHARBYTES) - 1; i++)
389 s1[i] = 23;
390 s1[i] = 0;
392 s2 = STRDUP (s1);
394 for (i = 0; i < 64; ++i)
395 do_page_test ((3988 / CHARBYTES) + i, (2636 / CHARBYTES), s2);
397 free (s2);
401 test_main (void)
403 size_t i;
405 test_init ();
407 check1 ();
408 check2 ();
410 printf ("%23s", "");
411 FOR_EACH_IMPL (impl, 0)
412 printf ("\t%s", impl->name);
413 putchar ('\n');
415 for (i =0; i < 16; ++i)
417 do_test (0, 0, 8, i, 127, 0);
418 do_test (0, 0, 8, i, 127, -1);
419 do_test (0, 0, 8, i, 127, 1);
420 do_test (i, i, 8, i, 127, 0);
421 do_test (i, i, 8, i, 127, 1);
422 do_test (i, i, 8, i, 127, -1);
423 do_test (i, 2 * i, 8, i, 127, 0);
424 do_test (2 * i, i, 8, i, 127, 1);
425 do_test (i, 3 * i, 8, i, 127, -1);
426 do_test (0, 0, 8, i, 255, 0);
427 do_test (0, 0, 8, i, 255, -1);
428 do_test (0, 0, 8, i, 255, 1);
429 do_test (i, i, 8, i, 255, 0);
430 do_test (i, i, 8, i, 255, 1);
431 do_test (i, i, 8, i, 255, -1);
432 do_test (i, 2 * i, 8, i, 255, 0);
433 do_test (2 * i, i, 8, i, 255, 1);
434 do_test (i, 3 * i, 8, i, 255, -1);
437 for (i = 1; i < 8; ++i)
439 do_test (0, 0, 8 << i, 16 << i, 127, 0);
440 do_test (0, 0, 8 << i, 16 << i, 127, 1);
441 do_test (0, 0, 8 << i, 16 << i, 127, -1);
442 do_test (0, 0, 8 << i, 16 << i, 255, 0);
443 do_test (0, 0, 8 << i, 16 << i, 255, 1);
444 do_test (0, 0, 8 << i, 16 << i, 255, -1);
445 do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
446 do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
447 do_test (2 * i, i, 8 << i, 16 << i, 255, 0);
448 do_test (2 * i, i, 8 << i, 16 << i, 255, 1);
451 do_test_limit (0, 0, 0, 0, 127, 0);
452 do_test_limit (4, 0, 21, 20, 127, 0);
453 do_test_limit (0, 4, 21, 20, 127, 0);
454 do_test_limit (8, 0, 25, 24, 127, 0);
455 do_test_limit (0, 8, 25, 24, 127, 0);
457 for (i = 0; i < 8; ++i)
459 do_test_limit (0, 0, 17 - i, 16 - i, 127, 0);
460 do_test_limit (0, 0, 17 - i, 16 - i, 255, 0);
461 do_test_limit (0, 0, 15 - i, 16 - i, 127, 0);
462 do_test_limit (0, 0, 15 - i, 16 - i, 127, 1);
463 do_test_limit (0, 0, 15 - i, 16 - i, 127, -1);
464 do_test_limit (0, 0, 15 - i, 16 - i, 255, 0);
465 do_test_limit (0, 0, 15 - i, 16 - i, 255, 1);
466 do_test_limit (0, 0, 15 - i, 16 - i, 255, -1);
469 do_random_tests ();
470 return ret;
473 #include "../test-skeleton.c"