(root)/
gettext-0.22.4/
gettext-tools/
gnulib-tests/
test-isinf.c
       1  /* Test of isinf() 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, 2008, using Bruno Haible's code as a
      18     template. */
      19  
      20  #include <config.h>
      21  
      22  #include <math.h>
      23  
      24  /* isinf must be a macro.  */
      25  #ifndef isinf
      26  # error missing declaration
      27  #endif
      28  
      29  #include <float.h>
      30  #include <limits.h>
      31  
      32  #include "infinity.h"
      33  #include "snan.h"
      34  #include "macros.h"
      35  
      36  float zerof = 0.0f;
      37  double zerod = 0.0;
      38  long double zerol = 0.0L;
      39  
      40  static void
      41  test_isinff ()
      42  {
      43    /* Zero. */
      44    ASSERT (!isinf (0.0f));
      45    /* Subnormal values. */
      46    ASSERT (!isinf (FLT_MIN / 2));
      47    ASSERT (!isinf (-FLT_MIN / 2));
      48    /* Finite values.  */
      49    ASSERT (!isinf (3.141f));
      50    ASSERT (!isinf (3.141e30f));
      51    ASSERT (!isinf (3.141e-30f));
      52    ASSERT (!isinf (-2.718f));
      53    ASSERT (!isinf (-2.718e30f));
      54    ASSERT (!isinf (-2.718e-30f));
      55    ASSERT (!isinf (FLT_MAX));
      56    ASSERT (!isinf (-FLT_MAX));
      57    /* Infinite values.  */
      58    ASSERT (isinf (Infinityf ()));
      59    ASSERT (isinf (- Infinityf ()));
      60    /* Quiet NaN.  */
      61    ASSERT (!isinf (zerof / zerof));
      62  #if HAVE_SNANF
      63    /* Signalling NaN.  */
      64    ASSERT (!isinf (SNaNf ()));
      65  #endif
      66  }
      67  
      68  static void
      69  test_isinfd ()
      70  {
      71    /* Zero. */
      72    ASSERT (!isinf (0.0));
      73    /* Subnormal values. */
      74    ASSERT (!isinf (DBL_MIN / 2));
      75    ASSERT (!isinf (-DBL_MIN / 2));
      76    /* Finite values. */
      77    ASSERT (!isinf (3.141));
      78    ASSERT (!isinf (3.141e30));
      79    ASSERT (!isinf (3.141e-30));
      80    ASSERT (!isinf (-2.718));
      81    ASSERT (!isinf (-2.718e30));
      82    ASSERT (!isinf (-2.718e-30));
      83    ASSERT (!isinf (DBL_MAX));
      84    ASSERT (!isinf (-DBL_MAX));
      85    /* Infinite values.  */
      86    ASSERT (isinf (Infinityd ()));
      87    ASSERT (isinf (- Infinityd ()));
      88    /* Quiet NaN.  */
      89    ASSERT (!isinf (zerod / zerod));
      90  #if HAVE_SNAND
      91    /* Signalling NaN.  */
      92    ASSERT (!isinf (SNaNd ()));
      93  #endif
      94  }
      95  
      96  static void
      97  test_isinfl ()
      98  {
      99    /* Zero. */
     100    ASSERT (!isinf (0.0L));
     101    /* Subnormal values. */
     102    ASSERT (!isinf (LDBL_MIN / 2));
     103    ASSERT (!isinf (-LDBL_MIN / 2));
     104    /* Finite values. */
     105    ASSERT (!isinf (3.141L));
     106    ASSERT (!isinf (3.141e30L));
     107    ASSERT (!isinf (3.141e-30L));
     108    ASSERT (!isinf (-2.718L));
     109    ASSERT (!isinf (-2.718e30L));
     110    ASSERT (!isinf (-2.718e-30L));
     111    ASSERT (!isinf (LDBL_MAX));
     112    ASSERT (!isinf (-LDBL_MAX));
     113    /* Infinite values.  */
     114    ASSERT (isinf (Infinityl ()));
     115    ASSERT (isinf (- Infinityl ()));
     116    /* Quiet NaN.  */
     117    ASSERT (!isinf (zerol / zerol));
     118  #if HAVE_SNANL
     119    /* Signalling NaN.  */
     120    ASSERT (!isinf (SNaNl ()));
     121  #endif
     122  
     123  #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
     124    #define NWORDS \
     125      ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
     126    typedef union { unsigned int word[NWORDS]; long double value; }
     127            memory_long_double;
     128  /* Representation of an 80-bit 'long double' as an initializer for a sequence
     129     of 'unsigned int' words.  */
     130  # ifdef WORDS_BIGENDIAN
     131  #  define LDBL80_WORDS(exponent,manthi,mantlo) \
     132       { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
     133         ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16),   \
     134         (unsigned int) (mantlo) << 16                                        \
     135       }
     136  # else
     137  #  define LDBL80_WORDS(exponent,manthi,mantlo) \
     138       { mantlo, manthi, exponent }
     139  # endif
     140    { /* Quiet NaN.  */
     141      static memory_long_double x =
     142        { .word = LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
     143      ASSERT (!isinf (x.value));
     144    }
     145    {
     146      /* Signalling NaN.  */
     147      static memory_long_double x =
     148        { .word = LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
     149      ASSERT (!isinf (x.value));
     150    }
     151    /* isinf should return something for noncanonical values.  */
     152    { /* Pseudo-NaN.  */
     153      static memory_long_double x =
     154        { .word = LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
     155      ASSERT (isinf (x.value) || !isinf (x.value));
     156    }
     157    { /* Pseudo-Infinity.  */
     158      static memory_long_double x =
     159        { .word = LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
     160      ASSERT (isinf (x.value) || !isinf (x.value));
     161    }
     162    { /* Pseudo-Zero.  */
     163      static memory_long_double x =
     164        { .word = LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
     165      ASSERT (isinf (x.value) || !isinf (x.value));
     166    }
     167    { /* Unnormalized number.  */
     168      static memory_long_double x =
     169        { .word = LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
     170      ASSERT (isinf (x.value) || !isinf (x.value));
     171    }
     172    { /* Pseudo-Denormal.  */
     173      static memory_long_double x =
     174        { .word = LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
     175      ASSERT (isinf (x.value) || !isinf (x.value));
     176    }
     177    #undef NWORDS
     178  #endif
     179  }
     180  
     181  int
     182  main ()
     183  {
     184    test_isinff ();
     185    test_isinfd ();
     186    test_isinfl ();
     187    return 0;
     188  }