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

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

DEFINITIONS

This source file includes following definitions.
  1. test_isfinitef
  2. test_isfinited
  3. test_isfinitel
  4. main

   1 /* Test of isfinite() substitute.
   2    Copyright (C) 2007-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 Ben Pfaff, 2007, using Bruno Haible's code as a
  18    template. */
  19 
  20 #include <config.h>
  21 
  22 #include <math.h>
  23 
  24 /* isfinite must be a macro.  */
  25 #ifndef isfinite
  26 # error missing declaration
  27 #endif
  28 
  29 #include <float.h>
  30 #include <limits.h>
  31 
  32 #include "infinity.h"
  33 #include "macros.h"
  34 
  35 float zerof = 0.0f;
  36 double zerod = 0.0;
  37 long double zerol = 0.0L;
  38 
  39 static void
  40 test_isfinitef ()
     /* [previous][next][first][last][top][bottom][index][help] */
  41 {
  42   /* Zero. */
  43   ASSERT (isfinite (0.0f));
  44   /* Subnormal values. */
  45   ASSERT (isfinite (FLT_MIN / 2));
  46   ASSERT (isfinite (-FLT_MIN / 2));
  47   /* Finite values.  */
  48   ASSERT (isfinite (3.141f));
  49   ASSERT (isfinite (3.141e30f));
  50   ASSERT (isfinite (3.141e-30f));
  51   ASSERT (isfinite (-2.718f));
  52   ASSERT (isfinite (-2.718e30f));
  53   ASSERT (isfinite (-2.718e-30f));
  54   /* Infinite values.  */
  55   ASSERT (!isfinite (Infinityf ()));
  56   ASSERT (!isfinite (- Infinityf ()));
  57   /* Quiet NaN.  */
  58   ASSERT (!isfinite (zerof / zerof));
  59 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
  60   /* Signalling NaN.  */
  61   {
  62     #define NWORDS \
  63       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
  64     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
  65     memory_float m;
  66     m.value = zerof / zerof;
  67 # if FLT_EXPBIT0_BIT > 0
  68     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
  69 # else
  70     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
  71       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
  72 # endif
  73     if (FLT_EXPBIT0_WORD < NWORDS / 2)
  74       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
  75     else
  76       m.word[0] |= (unsigned int) 1;
  77     ASSERT (!isfinite (m.value));
  78     #undef NWORDS
  79   }
  80 #endif
  81 }
  82 
  83 static void
  84 test_isfinited ()
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86   /* Zero. */
  87   ASSERT (isfinite (0.0));
  88   /* Subnormal values. */
  89   ASSERT (isfinite (DBL_MIN / 2));
  90   ASSERT (isfinite (-DBL_MIN / 2));
  91   /* Finite values. */
  92   ASSERT (isfinite (3.141));
  93   ASSERT (isfinite (3.141e30));
  94   ASSERT (isfinite (3.141e-30));
  95   ASSERT (isfinite (-2.718));
  96   ASSERT (isfinite (-2.718e30));
  97   ASSERT (isfinite (-2.718e-30));
  98   /* Infinite values.  */
  99   ASSERT (!isfinite (Infinityd ()));
 100   ASSERT (!isfinite (- Infinityd ()));
 101   /* Quiet NaN.  */
 102   ASSERT (!isfinite (zerod / zerod));
 103 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
 104   /* Signalling NaN.  */
 105   {
 106     #define NWORDS \
 107       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
 108     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
 109     memory_double m;
 110     m.value = zerod / zerod;
 111 # if DBL_EXPBIT0_BIT > 0
 112     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
 113 # else
 114     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
 115       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
 116 # endif
 117     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
 118       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
 119     ASSERT (!isfinite (m.value));
 120     #undef NWORDS
 121   }
 122 #endif
 123 }
 124 
 125 static void
 126 test_isfinitel ()
     /* [previous][next][first][last][top][bottom][index][help] */
 127 {
 128   #define NWORDS \
 129     ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
 130   typedef union { unsigned int word[NWORDS]; long double value; }
 131           memory_long_double;
 132 
 133   /* Zero. */
 134   ASSERT (isfinite (0.0L));
 135   /* Subnormal values. */
 136   ASSERT (isfinite (LDBL_MIN / 2));
 137   ASSERT (isfinite (-LDBL_MIN / 2));
 138   /* Finite values. */
 139   ASSERT (isfinite (3.141L));
 140   ASSERT (isfinite (3.141e30L));
 141   ASSERT (isfinite (3.141e-30L));
 142   ASSERT (isfinite (-2.718L));
 143   ASSERT (isfinite (-2.718e30L));
 144   ASSERT (isfinite (-2.718e-30L));
 145   /* Infinite values.  */
 146   ASSERT (!isfinite (Infinityl ()));
 147   ASSERT (!isfinite (- Infinityl ()));
 148   /* Quiet NaN.  */
 149   ASSERT (!isfinite (zerol / zerol));
 150 
 151 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
 152   /* A bit pattern that is different from a Quiet NaN.  With a bit of luck,
 153      it's a Signalling NaN.  */
 154   {
 155 #if defined __powerpc__ && LDBL_MANT_DIG == 106
 156     /* This is PowerPC "double double", a pair of two doubles.  Inf and Nan are
 157        represented as the corresponding 64-bit IEEE values in the first double;
 158        the second is ignored.  Manipulate only the first double.  */
 159     #undef NWORDS
 160     #define NWORDS \
 161       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
 162 #endif
 163 
 164     memory_long_double m;
 165     m.value = zerol / zerol;
 166 # if LDBL_EXPBIT0_BIT > 0
 167     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
 168 # else
 169     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
 170       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
 171 # endif
 172     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
 173       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
 174     ASSERT (!isfinite (m.value));
 175   }
 176 #endif
 177 
 178 #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
 179 /* Representation of an 80-bit 'long double' as an initializer for a sequence
 180    of 'unsigned int' words.  */
 181 # ifdef WORDS_BIGENDIAN
 182 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
 183      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
 184        ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16),   \
 185        (unsigned int) (mantlo) << 16                                        \
 186      }
 187 # else
 188 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
 189      { mantlo, manthi, exponent }
 190 # endif
 191   { /* Quiet NaN.  */
 192     static memory_long_double x =
 193       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
 194     ASSERT (!isfinite (x.value));
 195   }
 196   {
 197     /* Signalling NaN.  */
 198     static memory_long_double x =
 199       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
 200     ASSERT (!isfinite (x.value));
 201   }
 202   /* isfinite should return something for noncanonical values.  */
 203   { /* Pseudo-NaN.  */
 204     static memory_long_double x =
 205       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
 206     ASSERT (isfinite (x.value) || !isfinite (x.value));
 207   }
 208   { /* Pseudo-Infinity.  */
 209     static memory_long_double x =
 210       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
 211     ASSERT (isfinite (x.value) || !isfinite (x.value));
 212   }
 213   { /* Pseudo-Zero.  */
 214     static memory_long_double x =
 215       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
 216     ASSERT (isfinite (x.value) || !isfinite (x.value));
 217   }
 218   { /* Unnormalized number.  */
 219     static memory_long_double x =
 220       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
 221     ASSERT (isfinite (x.value) || !isfinite (x.value));
 222   }
 223   { /* Pseudo-Denormal.  */
 224     static memory_long_double x =
 225       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
 226     ASSERT (isfinite (x.value) || !isfinite (x.value));
 227   }
 228 #endif
 229 
 230   #undef NWORDS
 231 }
 232 
 233 int
 234 main ()
     /* [previous][next][first][last][top][bottom][index][help] */
 235 {
 236   test_isfinitef ();
 237   test_isfinited ();
 238   test_isfinitel ();
 239   return 0;
 240 }

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