1 /* Test and measure memcmp functions.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
21 # define TEST_NAME "__memcmpeq"
23 # define TEST_NAME "wmemcmp"
25 # define TEST_NAME "memcmp"
28 #include "test-string.h"
30 # include <inttypes.h>
33 # define MEMCMP wmemcmp
34 # define MEMCPY wmemcpy
35 # define SIMPLE_MEMCMP simple_wmemcmp
37 # define UCHAR wchar_t
39 # define CHAR__MIN WCHAR_MIN
40 # define CHAR__MAX WCHAR_MAX
43 SIMPLE_MEMCMP (const wchar_t *s1
, const wchar_t *s2
, size_t n
)
47 wmemcmp has to use SIGNED comparison for elements.
48 memcmp has to use UNSIGNED comparison for elemnts.
50 while (n
-- && (ret
= *s1
< *s2
? -1 : *s1
== *s2
? 0 : 1) == 0) {s1
++; s2
++;}
56 # define MEMCMP __memcmpeq
57 # define SIMPLE_MEMCMP simple_memcmpeq
59 # define MEMCMP memcmp
60 # define SIMPLE_MEMCMP simple_memcmp
62 # define MEMCPY memcpy
65 # define UCHAR unsigned char
67 # define CHAR__MIN CHAR_MIN
68 # define CHAR__MAX CHAR_MAX
71 SIMPLE_MEMCMP (const char *s1
, const char *s2
, size_t n
)
75 while (n
-- && (ret
= *(unsigned char *) s1
++ - *(unsigned char *) s2
++) == 0);
81 # define BAD_RESULT(result, expec) \
82 (((result) == 0 && (expec)) || ((result) < 0 && (expec) >= 0) || \
83 ((result) > 0 && (expec) <= 0))
86 typedef int (*proto_t
) (const CHAR
*, const CHAR
*, size_t);
91 check_result (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t len
,
94 int result
= CALL (impl
, s1
, s2
, len
);
95 if (BAD_RESULT(result
, exp_result
))
97 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
107 do_one_test (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t len
,
110 if (check_result (impl
, s1
, s2
, len
, exp_result
) < 0)
115 do_test (size_t align1
, size_t align2
, size_t len
, int exp_result
)
123 align1
&= (4096 - CHARBYTES
);
124 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
127 align2
&= (4096 - CHARBYTES
);
128 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
131 s1
= (CHAR
*) (buf1
+ align1
);
132 s2
= (CHAR
*) (buf2
+ align2
);
134 for (i
= 0; i
< len
; i
++)
135 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% CHAR__MAX
;
139 s2
[len
- 1] -= exp_result
;
141 FOR_EACH_IMPL (impl
, 0)
142 do_one_test (impl
, s1
, s2
, len
, exp_result
);
146 do_random_tests (void)
148 size_t i
, j
, n
, align1
, align2
, pos
, len
;
151 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
);
152 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
);
154 for (n
= 0; n
< ITERATIONS
; n
++)
156 align1
= random () & 31;
158 align2
= random () & 31;
160 align2
= align1
+ (random () & 24);
161 pos
= random () & 511;
166 pos
= 511 - j
- (random () & 7);
167 len
= random () & 511;
169 len
= 511 - j
- (random () & 7);
170 j
= len
+ align1
+ 64;
171 if (j
> 512) j
= 512;
172 for (i
= 0; i
< j
; ++i
)
173 p1
[i
] = random () & 255;
174 for (i
= 0; i
< j
; ++i
)
175 p2
[i
] = random () & 255;
179 MEMCPY ((CHAR
*) p2
+ align2
, (const CHAR
*) p1
+ align1
, len
);
182 MEMCPY ((CHAR
*) p2
+ align2
, (const CHAR
*) p1
+ align1
, pos
);
183 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
185 p2
[align2
+ pos
] = random () & 255;
186 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
187 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
190 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
196 FOR_EACH_IMPL (impl
, 1)
198 r
= CALL (impl
, (CHAR
*) p1
+ align1
, (const CHAR
*) p2
+ align2
,
200 if (BAD_RESULT(r
, result
))
202 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
203 n
, impl
->name
, align1
* CHARBYTES
& 63, align2
* CHARBYTES
& 63, len
, pos
, r
, result
, p1
, p2
);
213 CHAR s1
[116], s2
[116];
450 for (size_t i
= 0; i
< n
; i
++)
451 for (size_t len
= 0; len
<= n
- i
; ++len
)
453 exp_result
= SIMPLE_MEMCMP (s1
+ i
, s2
+ i
, len
);
454 FOR_EACH_IMPL (impl
, 0)
455 check_result (impl
, s1
+ i
, s2
+ i
, len
, exp_result
);
459 /* This test checks that memcmp doesn't overrun buffers. */
463 size_t max_length
= page_size
/ sizeof (CHAR
);
465 /* Initialize buf2 to the same values as buf1. The bug requires the
466 last compared byte to be different. */
467 memcpy (buf2
, buf1
, page_size
);
468 ((char *) buf2
)[page_size
- 1] ^= 0x11;
470 for (size_t length
= 1; length
< max_length
; length
++)
472 CHAR
*s1
= (CHAR
*) buf1
+ max_length
- length
;
473 CHAR
*s2
= (CHAR
*) buf2
+ max_length
- length
;
475 const int exp_result
= SIMPLE_MEMCMP (s1
, s2
, length
);
477 FOR_EACH_IMPL (impl
, 0)
478 check_result (impl
, s1
, s2
, length
, exp_result
);
493 FOR_EACH_IMPL (impl
, 0)
494 printf ("\t%s", impl
->name
);
497 for (i
= 1; i
< 32; ++i
)
499 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 0);
500 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 1);
501 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, -1);
504 for (i
= 0; i
< 32; ++i
)
506 do_test (0, 0, i
, 0);
507 do_test (0, 0, i
, 1);
508 do_test (0, 0, i
, -1);
509 do_test (4096 - i
, 0, i
, 0);
510 do_test (4096 - i
, 0, i
, 1);
511 do_test (4096 - i
, 0, i
, -1);
512 do_test (4095, 0, i
, 0);
513 do_test (4095, 0, i
, 1);
514 do_test (4095, 0, i
, -1);
515 do_test (4095, 4095, i
, 0);
516 do_test (4095, 4095, i
, 1);
517 do_test (4095, 4095, i
, -1);
518 do_test (4000, 95, i
, 0);
519 do_test (4000, 95, i
, 1);
520 do_test (4000, 95, i
, -1);
523 for (i
= 33; i
< 385; i
+= 32)
525 do_test (0, 0, i
, 0);
526 do_test (0, 0, i
, 1);
527 do_test (0, 0, i
, -1);
528 do_test (i
, 0, i
, 0);
529 do_test (0, i
, i
, 1);
530 do_test (i
, i
, i
, -1);
533 for (i
= 1; i
< 10; ++i
)
535 do_test (0, 0, 2 << i
, 0);
536 do_test (0, 0, 2 << i
, 1);
537 do_test (0, 0, 2 << i
, -1);
538 do_test ((8 - i
) * CHARBYTES
, (2 * i
) * CHARBYTES
, 16 << i
, 0);
539 do_test (0, 0, 16 << i
, 0);
540 do_test (0, 0, 16 << i
, 1);
541 do_test (0, 0, 16 << i
, -1);
542 do_test (i
, 0, 2 << i
, 0);
543 do_test (0, i
, 2 << i
, 1);
544 do_test (i
, i
, 2 << i
, -1);
545 do_test (i
, 0, 16 << i
, 0);
546 do_test (0, i
, 16 << i
, 1);
547 do_test (i
, i
, 16 << i
, -1);
550 for (i
= 1; i
< 10; ++i
)
552 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 0);
553 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 1);
554 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, -1);
561 #include <support/test-driver.c>