(root)/
gettext-0.22.4/
gettext-tools/
gnulib-tests/
test-fstrcmp.c
       1  /* Test of fuzzy string comparison.
       2     Copyright (C) 2007-2023 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 Bruno Haible <bruno@clisp.org>, 2007.  */
      18  
      19  #include <config.h>
      20  
      21  #include "fstrcmp.h"
      22  
      23  
      24  #include "macros.h"
      25  
      26  static bool
      27  check_fstrcmp (const char *string1, const char *string2, double expected)
      28  {
      29    /* The use of 'volatile' guarantees that excess precision bits are dropped
      30       before the addition and before the following comparison at the caller's
      31       site.  It is necessary on x86 systems where double-floats are not IEEE
      32       compliant by default, to avoid that msgmerge results become platform and
      33       compiler option dependent.  'volatile' is a portable alternative to gcc's
      34       -ffloat-store option.  */
      35    {
      36      volatile double result = fstrcmp (string1, string2);
      37      if (!(result == expected))
      38        return false;
      39    }
      40    {
      41      volatile double result = fstrcmp_bounded (string1, string2, expected);
      42      if (!(result == expected))
      43        return false;
      44    }
      45    {
      46      double bound = expected * 0.5; /* implies bound <= expected */
      47      volatile double result = fstrcmp_bounded (string1, string2, bound);
      48      if (!(result == expected))
      49        return false;
      50    }
      51    {
      52      double bound = (1 + expected) * 0.5;
      53      if (expected < bound)
      54        {
      55          volatile double result = fstrcmp_bounded (string1, string2, bound);
      56          if (!(result < bound))
      57            return false;
      58        }
      59    }
      60  
      61    return true;
      62  }
      63  
      64  int
      65  main (int argc, char *argv[])
      66  {
      67    ASSERT (check_fstrcmp ("Langstrumpf", "Langstrumpf", 1.0));
      68    ASSERT (check_fstrcmp ("Levenshtein", "Levenstein", 20./21.));
      69    ASSERT (check_fstrcmp ("Levenstein", "Levenshtein", 20./21.));
      70    ASSERT (check_fstrcmp ("xy", "yx", 1./2.));
      71    ASSERT (check_fstrcmp ("George Bush", "Abraham Lincoln", 2./13.));
      72    ASSERT (check_fstrcmp ("George Bush", "George \"Bugs\" Moran", 2./3.));
      73  
      74    return 0;
      75  }