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);
88 IMPL (SIMPLE_MEMCMP
, 0)
92 check_result (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t len
,
95 int result
= CALL (impl
, s1
, s2
, len
);
96 if (BAD_RESULT(result
, exp_result
))
98 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
108 do_one_test (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t len
,
111 if (check_result (impl
, s1
, s2
, len
, exp_result
) < 0)
116 do_test (size_t align1
, size_t align2
, size_t len
, int exp_result
)
124 align1
&= (4096 - CHARBYTES
);
125 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
128 align2
&= (4096 - CHARBYTES
);
129 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
132 s1
= (CHAR
*) (buf1
+ align1
);
133 s2
= (CHAR
*) (buf2
+ align2
);
135 for (i
= 0; i
< len
; i
++)
136 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% CHAR__MAX
;
140 s2
[len
- 1] -= exp_result
;
142 FOR_EACH_IMPL (impl
, 0)
143 do_one_test (impl
, s1
, s2
, len
, exp_result
);
147 do_random_tests (void)
149 size_t i
, j
, n
, align1
, align2
, pos
, len
;
152 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
);
153 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
);
155 for (n
= 0; n
< ITERATIONS
; n
++)
157 align1
= random () & 31;
159 align2
= random () & 31;
161 align2
= align1
+ (random () & 24);
162 pos
= random () & 511;
167 pos
= 511 - j
- (random () & 7);
168 len
= random () & 511;
170 len
= 511 - j
- (random () & 7);
171 j
= len
+ align1
+ 64;
172 if (j
> 512) j
= 512;
173 for (i
= 0; i
< j
; ++i
)
174 p1
[i
] = random () & 255;
175 for (i
= 0; i
< j
; ++i
)
176 p2
[i
] = random () & 255;
180 MEMCPY ((CHAR
*) p2
+ align2
, (const CHAR
*) p1
+ align1
, len
);
183 MEMCPY ((CHAR
*) p2
+ align2
, (const CHAR
*) p1
+ align1
, pos
);
184 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
186 p2
[align2
+ pos
] = random () & 255;
187 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
188 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
191 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
197 FOR_EACH_IMPL (impl
, 1)
199 r
= CALL (impl
, (CHAR
*) p1
+ align1
, (const CHAR
*) p2
+ align2
,
201 if (BAD_RESULT(r
, result
))
203 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
204 n
, impl
->name
, align1
* CHARBYTES
& 63, align2
* CHARBYTES
& 63, len
, pos
, r
, result
, p1
, p2
);
214 CHAR s1
[116], s2
[116];
451 for (size_t i
= 0; i
< n
; i
++)
452 for (size_t len
= 0; len
<= n
- i
; ++len
)
454 exp_result
= SIMPLE_MEMCMP (s1
+ i
, s2
+ i
, len
);
455 FOR_EACH_IMPL (impl
, 0)
456 check_result (impl
, s1
+ i
, s2
+ i
, len
, exp_result
);
460 /* This test checks that memcmp doesn't overrun buffers. */
464 size_t max_length
= page_size
/ sizeof (CHAR
);
466 /* Initialize buf2 to the same values as buf1. The bug requires the
467 last compared byte to be different. */
468 memcpy (buf2
, buf1
, page_size
);
469 ((char *) buf2
)[page_size
- 1] ^= 0x11;
471 for (size_t length
= 1; length
< max_length
; length
++)
473 CHAR
*s1
= (CHAR
*) buf1
+ max_length
- length
;
474 CHAR
*s2
= (CHAR
*) buf2
+ max_length
- length
;
476 const int exp_result
= SIMPLE_MEMCMP (s1
, s2
, length
);
478 FOR_EACH_IMPL (impl
, 0)
479 check_result (impl
, s1
, s2
, length
, exp_result
);
494 FOR_EACH_IMPL (impl
, 0)
495 printf ("\t%s", impl
->name
);
498 for (i
= 1; i
< 32; ++i
)
500 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 0);
501 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 1);
502 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, -1);
505 for (i
= 0; i
< 32; ++i
)
507 do_test (0, 0, i
, 0);
508 do_test (0, 0, i
, 1);
509 do_test (0, 0, i
, -1);
510 do_test (4096 - i
, 0, i
, 0);
511 do_test (4096 - i
, 0, i
, 1);
512 do_test (4096 - i
, 0, i
, -1);
513 do_test (4095, 0, i
, 0);
514 do_test (4095, 0, i
, 1);
515 do_test (4095, 0, i
, -1);
516 do_test (4095, 4095, i
, 0);
517 do_test (4095, 4095, i
, 1);
518 do_test (4095, 4095, i
, -1);
519 do_test (4000, 95, i
, 0);
520 do_test (4000, 95, i
, 1);
521 do_test (4000, 95, i
, -1);
524 for (i
= 33; i
< 385; i
+= 32)
526 do_test (0, 0, i
, 0);
527 do_test (0, 0, i
, 1);
528 do_test (0, 0, i
, -1);
529 do_test (i
, 0, i
, 0);
530 do_test (0, i
, i
, 1);
531 do_test (i
, i
, i
, -1);
534 for (i
= 1; i
< 10; ++i
)
536 do_test (0, 0, 2 << i
, 0);
537 do_test (0, 0, 2 << i
, 1);
538 do_test (0, 0, 2 << i
, -1);
539 do_test ((8 - i
) * CHARBYTES
, (2 * i
) * CHARBYTES
, 16 << i
, 0);
540 do_test (0, 0, 16 << i
, 0);
541 do_test (0, 0, 16 << i
, 1);
542 do_test (0, 0, 16 << i
, -1);
543 do_test (i
, 0, 2 << i
, 0);
544 do_test (0, i
, 2 << i
, 1);
545 do_test (i
, i
, 2 << i
, -1);
546 do_test (i
, 0, 16 << i
, 0);
547 do_test (0, i
, 16 << i
, 1);
548 do_test (i
, i
, 16 << i
, -1);
551 for (i
= 1; i
< 10; ++i
)
553 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 0);
554 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 1);
555 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, -1);
562 #include <support/test-driver.c>