root/maint/gnulib/tests/unistr/test-cmp.h

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

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. test_cmp

   1 /* Test of uN_cmp() functions.
   2    Copyright (C) 2008-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3 of the License, or
   7    (at your option) any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* Written by Simon Josefsson and Bruno Haible <bruno@clisp.org>, 2010.  */
  18 
  19 static void
  20 test_cmp (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  21 {
  22   /* Test equal / not equal distinction.  */
  23   void *page_boundary1 = zerosize_ptr ();
  24   void *page_boundary2 = zerosize_ptr ();
  25   if (page_boundary1 && page_boundary2)
  26     ASSERT (U_CMP (page_boundary1, page_boundary2, 0) == 0);
  27   {
  28     static const UNIT input1[] = { 'f', 'o', 'o', 0 };
  29     static const UNIT input2[] = { 'f', 'o', 'o', 'b', 'a', 'r', 0 };
  30     ASSERT (U_CMP (input1, input2, 2) == 0);
  31     ASSERT (U_CMP (input1, input2, 3) == 0);
  32     ASSERT (U_CMP (input1, input2, 4) != 0);
  33   }
  34   {
  35     static const UNIT input1[] = { 'f', 'o', 'o', 0 };
  36     static const UNIT input2[] = { 'b', 'a', 'r', 0 };
  37     ASSERT (U_CMP (input1, input2, 1) != 0);
  38     ASSERT (U_CMP (input1, input2, 3) != 0);
  39   }
  40 
  41   /* Test less / equal / greater distinction.  */
  42   {
  43     static const UNIT input1[] = { 'f', 'o', 'o', 0 };
  44     static const UNIT input2[] = { 'm', 'o', 'o', 0 };
  45     ASSERT (U_CMP (input1, input2, 4) < 0);
  46     ASSERT (U_CMP (input2, input1, 4) > 0);
  47   }
  48   {
  49     static const UNIT input1[] = { 'o', 'o', 'm', 'p', 'h', 0 };
  50     static const UNIT input2[] = { 'o', 'o', 'p', 's', 0 };
  51     ASSERT (U_CMP (input1, input2, 3) < 0);
  52     ASSERT (U_CMP (input2, input1, 3) > 0);
  53   }
  54   {
  55     static const UNIT input1[] = { 'f', 'o', 'o', 0 };
  56     static const UNIT input2[] = { 'f', 'o', 'o', 'b', 'a', 'r', 0 };
  57     ASSERT (U_CMP (input1, input2, 4) < 0);
  58     ASSERT (U_CMP (input2, input1, 4) > 0);
  59   }
  60 
  61   /* Some old versions of memcmp were not 8-bit clean.  */
  62   {
  63     static const UNIT input1[] = { 0x40 };
  64     static const UNIT input2[] = { 0xC2 };
  65     ASSERT (U_CMP (input1, input2, 1) < 0);
  66     ASSERT (U_CMP (input2, input1, 1) > 0);
  67   }
  68   {
  69     static const UNIT input1[] = { 0xC2 };
  70     static const UNIT input2[] = { 0xC3 };
  71     ASSERT (U_CMP (input1, input2, 1) < 0);
  72     ASSERT (U_CMP (input2, input1, 1) > 0);
  73   }
  74 
  75   /* The Next x86 OpenStep bug shows up only when comparing 16 bytes
  76      or more and with at least one buffer not starting on a 4-byte boundary.
  77      William Lewis provided this test program.   */
  78   {
  79     UNIT foo[21];
  80     UNIT bar[21];
  81     int i;
  82     for (i = 0; i < 4; i++)
  83       {
  84         UNIT *a = foo + i;
  85         UNIT *b = bar + i;
  86         int j;
  87         for (j = 0; j < 8; j++)
  88           a[j] = '-';
  89         a[8] = '0';
  90         for (j = 9; j < 16; j++)
  91           a[j] = '1';
  92         for (j = 0; j < 8; j++)
  93           b[j] = '-';
  94         b[8] = '1';
  95         for (j = 9; j < 16; j++)
  96           b[j] = '0';
  97         ASSERT (U_CMP (a, b, 16) < 0);
  98       }
  99   }
 100 }

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