(root)/
gettext-0.22.4/
gettext-tools/
gnulib-tests/
test-isnan.c
       1  /* Test of isnand() substitute.
       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 Ben Pfaff <blp@cs.stanford.edu>, from code by Bruno
      18     Haible <bruno@clisp.org>.  */
      19  
      20  #include <config.h>
      21  
      22  #include <math.h>
      23  
      24  /* isnan must be a macro.  */
      25  #ifndef isnan
      26  # error missing declaration
      27  #endif
      28  
      29  #include <float.h>
      30  #include <limits.h>
      31  
      32  #include "minus-zero.h"
      33  #include "infinity.h"
      34  #include "nan.h"
      35  #include "snan.h"
      36  #include "macros.h"
      37  
      38  static void
      39  test_float (void)
      40  {
      41    /* Finite values.  */
      42    ASSERT (!isnan (3.141f));
      43    ASSERT (!isnan (3.141e30f));
      44    ASSERT (!isnan (3.141e-30f));
      45    ASSERT (!isnan (-2.718f));
      46    ASSERT (!isnan (-2.718e30f));
      47    ASSERT (!isnan (-2.718e-30f));
      48    ASSERT (!isnan (0.0f));
      49    ASSERT (!isnan (minus_zerof));
      50    /* Infinite values.  */
      51    ASSERT (!isnan (Infinityf ()));
      52    ASSERT (!isnan (- Infinityf ()));
      53    /* Quiet NaN.  */
      54    ASSERT (isnan (NaNf ()));
      55  #if HAVE_SNANF
      56    /* Signalling NaN.  */
      57    ASSERT (isnan (SNaNf ()));
      58  #endif
      59  }
      60  
      61  static void
      62  test_double (void)
      63  {
      64    /* Finite values.  */
      65    ASSERT (!isnan (3.141));
      66    ASSERT (!isnan (3.141e30));
      67    ASSERT (!isnan (3.141e-30));
      68    ASSERT (!isnan (-2.718));
      69    ASSERT (!isnan (-2.718e30));
      70    ASSERT (!isnan (-2.718e-30));
      71    ASSERT (!isnan (0.0));
      72    ASSERT (!isnan (minus_zerod));
      73    /* Infinite values.  */
      74    ASSERT (!isnan (Infinityd ()));
      75    ASSERT (!isnan (- Infinityd ()));
      76    /* Quiet NaN.  */
      77    ASSERT (isnan (NaNd ()));
      78  #if HAVE_SNAND
      79    /* Signalling NaN.  */
      80    ASSERT (isnan (SNaNd ()));
      81  #endif
      82  }
      83  
      84  static void
      85  test_long_double (void)
      86  {
      87    /* Finite values.  */
      88    ASSERT (!isnan (3.141L));
      89    ASSERT (!isnan (3.141e30L));
      90    ASSERT (!isnan (3.141e-30L));
      91    ASSERT (!isnan (-2.718L));
      92    ASSERT (!isnan (-2.718e30L));
      93    ASSERT (!isnan (-2.718e-30L));
      94    ASSERT (!isnan (0.0L));
      95    ASSERT (!isnan (minus_zerol));
      96    /* Infinite values.  */
      97    ASSERT (!isnan (Infinityl ()));
      98    ASSERT (!isnan (- Infinityl ()));
      99    /* Quiet NaN.  */
     100    ASSERT (isnan (NaNl ()));
     101  #if HAVE_SNANL
     102    /* Signalling NaN.  */
     103    ASSERT (isnan (SNaNl ()));
     104  #endif
     105  
     106  #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_)) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
     107    #define NWORDS \
     108      ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
     109    typedef union { unsigned int word[NWORDS]; long double value; }
     110            memory_long_double;
     111  /* Representation of an 80-bit 'long double' as an initializer for a sequence
     112     of 'unsigned int' words.  */
     113  # ifdef WORDS_BIGENDIAN
     114  #  define LDBL80_WORDS(exponent,manthi,mantlo) \
     115       { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
     116         ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16),   \
     117         (unsigned int) (mantlo) << 16                                        \
     118       }
     119  # else
     120  #  define LDBL80_WORDS(exponent,manthi,mantlo) \
     121       { mantlo, manthi, exponent }
     122  # endif
     123    { /* Quiet NaN.  */
     124      static memory_long_double x =
     125        { .word = LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
     126      ASSERT (isnan (x.value));
     127    }
     128    {
     129      /* Signalling NaN.  */
     130      static memory_long_double x =
     131        { .word = LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
     132      ASSERT (isnan (x.value));
     133    }
     134    /* isnan should return something for noncanonical values.  */
     135    { /* Pseudo-NaN.  */
     136      static memory_long_double x =
     137        { .word = LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
     138      ASSERT (isnan (x.value) || !isnan (x.value));
     139    }
     140    { /* Pseudo-Infinity.  */
     141      static memory_long_double x =
     142        { .word = LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
     143      ASSERT (isnan (x.value) || !isnan (x.value));
     144    }
     145    { /* Pseudo-Zero.  */
     146      static memory_long_double x =
     147        { .word = LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
     148      ASSERT (isnan (x.value) || !isnan (x.value));
     149    }
     150    { /* Unnormalized number.  */
     151      static memory_long_double x =
     152        { .word = LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
     153      ASSERT (isnan (x.value) || !isnan (x.value));
     154    }
     155    { /* Pseudo-Denormal.  */
     156      static memory_long_double x =
     157        { .word = LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
     158      ASSERT (isnan (x.value) || !isnan (x.value));
     159    }
     160    #undef NWORDS
     161  #endif
     162  }
     163  
     164  int
     165  main ()
     166  {
     167    test_float ();
     168    test_double ();
     169    test_long_double ();
     170    return 0;
     171  }