1 /* Test and measure memcmp functions.
2 Copyright (C) 1999-2024 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 elements.
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
)
120 align1
&= (4096 - CHARBYTES
);
121 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
124 align2
&= (4096 - CHARBYTES
);
125 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
128 s1
= (CHAR
*) (buf1
+ align1
);
129 s2
= (CHAR
*) (buf2
+ align2
);
131 for (i
= 0; i
< len
; i
++)
132 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% CHAR__MAX
;
138 s2
[len
- 1] -= exp_result
;
145 FOR_EACH_IMPL (impl
, 0)
146 do_one_test (impl
, s1
, s2
, len
, exp_result
);
150 do_random_tests (void)
152 size_t i
, j
, n
, align1
, align2
, pos
, len
;
155 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
);
156 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
);
158 for (n
= 0; n
< ITERATIONS
; n
++)
160 align1
= random () & 31;
162 align2
= random () & 31;
164 align2
= align1
+ (random () & 24);
165 pos
= random () & 511;
170 pos
= 511 - j
- (random () & 7);
171 len
= random () & 511;
173 len
= 511 - j
- (random () & 7);
174 j
= len
+ align1
+ 64;
175 if (j
> 512) j
= 512;
176 for (i
= 0; i
< j
; ++i
)
177 p1
[i
] = random () & 255;
178 for (i
= 0; i
< j
; ++i
)
179 p2
[i
] = random () & 255;
183 MEMCPY ((CHAR
*) p2
+ align2
, (const CHAR
*) p1
+ align1
, len
);
186 MEMCPY ((CHAR
*) p2
+ align2
, (const CHAR
*) p1
+ align1
, pos
);
187 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
189 p2
[align2
+ pos
] = random () & 255;
190 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
191 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
194 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
200 FOR_EACH_IMPL (impl
, 1)
202 r
= CALL (impl
, (CHAR
*) p1
+ align1
, (const CHAR
*) p2
+ align2
,
204 if (BAD_RESULT(r
, result
))
206 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
207 n
, impl
->name
, align1
* CHARBYTES
& 63, align2
* CHARBYTES
& 63, len
, pos
, r
, result
, p1
, p2
);
217 CHAR s1
[116], s2
[116];
454 for (size_t i
= 0; i
< n
; i
++)
455 for (size_t len
= 0; len
<= n
- i
; ++len
)
457 exp_result
= SIMPLE_MEMCMP (s1
+ i
, s2
+ i
, len
);
458 FOR_EACH_IMPL (impl
, 0)
459 check_result (impl
, s1
+ i
, s2
+ i
, len
, exp_result
);
463 /* This test checks that memcmp doesn't overrun buffers. */
467 size_t max_length
= page_size
/ sizeof (CHAR
);
469 /* Initialize buf2 to the same values as buf1. The bug requires the
470 last compared byte to be different. */
471 memcpy (buf2
, buf1
, page_size
);
472 ((char *) buf2
)[page_size
- 1] ^= 0x11;
474 for (size_t length
= 1; length
< max_length
; length
++)
476 CHAR
*s1
= (CHAR
*) buf1
+ max_length
- length
;
477 CHAR
*s2
= (CHAR
*) buf2
+ max_length
- length
;
479 const int exp_result
= SIMPLE_MEMCMP (s1
, s2
, length
);
481 FOR_EACH_IMPL (impl
, 0)
482 check_result (impl
, s1
, s2
, length
, exp_result
);
497 FOR_EACH_IMPL (impl
, 0)
498 printf ("\t%s", impl
->name
);
501 for (i
= 1; i
< 32; ++i
)
503 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 0);
504 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, 1);
505 do_test (i
* CHARBYTES
, i
* CHARBYTES
, i
, -1);
508 for (i
= 0; i
< 32; ++i
)
510 do_test (0, 0, i
, 0);
511 do_test (0, 0, i
, 1);
512 do_test (0, 0, i
, -1);
513 do_test (4096 - i
, 0, i
, 0);
514 do_test (4096 - i
, 0, i
, 1);
515 do_test (4096 - i
, 0, i
, -1);
516 do_test (4095, 0, i
, 0);
517 do_test (4095, 0, i
, 1);
518 do_test (4095, 0, i
, -1);
519 do_test (4095, 4095, i
, 0);
520 do_test (4095, 4095, i
, 1);
521 do_test (4095, 4095, i
, -1);
522 do_test (4000, 95, i
, 0);
523 do_test (4000, 95, i
, 1);
524 do_test (4000, 95, i
, -1);
527 for (i
= 33; i
< 385; i
+= 32)
529 do_test (0, 0, i
, 0);
530 do_test (0, 0, i
, 1);
531 do_test (0, 0, i
, -1);
532 do_test (i
, 0, i
, 0);
533 do_test (0, i
, i
, 1);
534 do_test (i
, i
, i
, -1);
537 for (i
= 1; i
< 10; ++i
)
539 do_test (0, 0, 2 << i
, 0);
540 do_test (0, 0, 2 << i
, 1);
541 do_test (0, 0, 2 << i
, -1);
542 do_test ((8 - i
) * CHARBYTES
, (2 * i
) * CHARBYTES
, 16 << i
, 0);
543 do_test (0, 0, 16 << i
, 0);
544 do_test (0, 0, 16 << i
, 1);
545 do_test (0, 0, 16 << i
, -1);
546 do_test (i
, 0, 2 << i
, 0);
547 do_test (0, i
, 2 << i
, 1);
548 do_test (i
, i
, 2 << i
, -1);
549 do_test (i
, 0, 16 << i
, 0);
550 do_test (0, i
, 16 << i
, 1);
551 do_test (i
, i
, 16 << i
, -1);
554 for (i
= 1; i
< 10; ++i
)
556 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 0);
557 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, 1);
558 do_test (i
* CHARBYTES
, 2 * (i
* CHARBYTES
), 8 << i
, -1);
565 #include <support/test-driver.c>