root/maint/gnulib/tests/test-memcmp.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /*
   2  * Copyright (C) 2008-2021 Free Software Foundation, Inc.
   3  * Written by Simon Josefsson
   4  *
   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.
   9  *
  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.
  14  *
  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/>.  */
  17 
  18 #include <config.h>
  19 
  20 #include <string.h>
  21 
  22 #include "signature.h"
  23 SIGNATURE_CHECK (memcmp, int, (void const *, void const *, size_t));
  24 
  25 #include "zerosize-ptr.h"
  26 #include "macros.h"
  27 
  28 int
  29 main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  30 {
  31   int (* volatile memcmp_ptr) (const void *, const void *, size_t) = memcmp;
  32 
  33   /* Test equal / not equal distinction.  */
  34   void *page_boundary1 = zerosize_ptr ();
  35   void *page_boundary2 = zerosize_ptr ();
  36   if (page_boundary1 && page_boundary2)
  37     ASSERT (memcmp (page_boundary1, page_boundary2, 0) == 0);
  38   ASSERT (memcmp ("foo", "foobar", 2) == 0);
  39   ASSERT (memcmp ("foo", "foobar", 3) == 0);
  40   ASSERT (memcmp ("foo", "foobar", 4) != 0);
  41   ASSERT (memcmp ("foo", "bar", 1) != 0);
  42   ASSERT (memcmp ("foo", "bar", 3) != 0);
  43 
  44   /* Test less / equal / greater distinction.  */
  45   ASSERT (memcmp ("foo", "moo", 4) < 0);
  46   ASSERT (memcmp ("moo", "foo", 4) > 0);
  47   ASSERT (memcmp ("oomph", "oops", 3) < 0);
  48   ASSERT (memcmp ("oops", "oomph", 3) > 0);
  49   ASSERT (memcmp ("foo", "foobar", 4) < 0);
  50   ASSERT (memcmp ("foobar", "foo", 4) > 0);
  51 
  52   /* Some old versions of memcmp were not 8-bit clean.  */
  53   /* Use the function pointer here, because otherwise this test is sometimes
  54      miscompiled by 'clang'.
  55      See <https://bugs.llvm.org/show_bug.cgi?id=40063>.  */
  56   ASSERT (memcmp_ptr ("\100", "\201", 1) < 0);
  57   ASSERT (memcmp_ptr ("\201", "\100", 1) > 0);
  58   ASSERT (memcmp_ptr ("\200", "\201", 1) < 0);
  59   ASSERT (memcmp_ptr ("\201", "\200", 1) > 0);
  60 
  61   /* The Next x86 OpenStep bug shows up only when comparing 16 bytes
  62      or more and with at least one buffer not starting on a 4-byte boundary.
  63      William Lewis provided this test program.   */
  64   {
  65     char foo[21];
  66     char bar[21];
  67     int i;
  68     for (i = 0; i < 4; i++)
  69       {
  70         char *a = foo + i;
  71         char *b = bar + i;
  72         strcpy (a, "--------01111111");
  73         strcpy (b, "--------10000000");
  74         ASSERT (memcmp (a, b, 16) < 0);
  75       }
  76   }
  77 
  78   return 0;
  79 }

/* [previous][next][first][last][top][bottom][index][help] */