2 * Copyright (C) 2008-2020 Free Software Foundation, Inc.
3 * Written by Simon Josefsson
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>. */
22 #include "signature.h"
23 SIGNATURE_CHECK (memcmp
, int, (void const *, void const *, size_t));
25 #include "zerosize-ptr.h"
31 int (* volatile memcmp_ptr
) (const void *, const void *, size_t) = memcmp
;
33 /* Test equal / not equal distinction. */
34 ASSERT (memcmp (zerosize_ptr (), zerosize_ptr (), 0) == 0);
35 ASSERT (memcmp ("foo", "foobar", 2) == 0);
36 ASSERT (memcmp ("foo", "foobar", 3) == 0);
37 ASSERT (memcmp ("foo", "foobar", 4) != 0);
38 ASSERT (memcmp ("foo", "bar", 1) != 0);
39 ASSERT (memcmp ("foo", "bar", 3) != 0);
41 /* Test less / equal / greater distinction. */
42 ASSERT (memcmp ("foo", "moo", 4) < 0);
43 ASSERT (memcmp ("moo", "foo", 4) > 0);
44 ASSERT (memcmp ("oomph", "oops", 3) < 0);
45 ASSERT (memcmp ("oops", "oomph", 3) > 0);
46 ASSERT (memcmp ("foo", "foobar", 4) < 0);
47 ASSERT (memcmp ("foobar", "foo", 4) > 0);
49 /* Some old versions of memcmp were not 8-bit clean. */
50 /* Use the function pointer here, because otherwise this test is sometimes
51 miscompiled by 'clang'.
52 See <https://bugs.llvm.org/show_bug.cgi?id=40063>. */
53 ASSERT (memcmp_ptr ("\100", "\201", 1) < 0);
54 ASSERT (memcmp_ptr ("\201", "\100", 1) > 0);
55 ASSERT (memcmp_ptr ("\200", "\201", 1) < 0);
56 ASSERT (memcmp_ptr ("\201", "\200", 1) > 0);
58 /* The Next x86 OpenStep bug shows up only when comparing 16 bytes
59 or more and with at least one buffer not starting on a 4-byte boundary.
60 William Lewis provided this test program. */
65 for (i
= 0; i
< 4; i
++)
69 strcpy (a
, "--------01111111");
70 strcpy (b
, "--------10000000");
71 ASSERT (memcmp (a
, b
, 16) < 0);