Implement 'isfinite' module.
[gnulib.git] / tests / test-isfinite.c
1 /* Test of isfinite() substitute.
2    Copyright (C) 2007 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 <http://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 <float.h>
23 #include <limits.h>
24 #include <math.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #define ASSERT(expr) \
30   do                                                                         \
31     {                                                                        \
32       if (!(expr))                                                           \
33         {                                                                    \
34           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
35           abort ();                                                          \
36         }                                                                    \
37     }                                                                        \
38   while (0)
39
40 float zerof = 0.0f;
41 double zerod = 0.0;
42 long double zerol = 0.0L;
43
44 static void
45 test_isfinitef ()
46 {
47   /* Zero. */
48   ASSERT (isfinite (0.0f));
49   /* Subnormal values. */
50   ASSERT (isfinite (FLT_MIN / 2));
51   ASSERT (isfinite (-FLT_MIN / 2));
52   /* Finite values.  */
53   ASSERT (isfinite (3.141f));
54   ASSERT (isfinite (3.141e30f));
55   ASSERT (isfinite (3.141e-30f));
56   ASSERT (isfinite (-2.718f));
57   ASSERT (isfinite (-2.718e30f));
58   ASSERT (isfinite (-2.718e-30f));
59   /* Infinite values.  */
60   ASSERT (!isfinite (1.0f / 0.0f));
61   ASSERT (!isfinite (-1.0f / 0.0f));
62   /* Quiet NaN.  */
63   ASSERT (!isfinite (zerof / zerof));
64 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
65   /* Signalling NaN.  */
66   {
67     #define NWORDS \
68       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
69     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
70     memory_float m;
71     m.value = zerof / zerof;
72 # if FLT_EXPBIT0_BIT > 0
73     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
74 # else
75     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
76       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
77 # endif
78     if (FLT_EXPBIT0_WORD < NWORDS / 2)
79       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
80     else
81       m.word[0] |= (unsigned int) 1;
82     ASSERT (!isfinite (m.value));
83     #undef NWORDS
84   }
85 #endif
86 }
87
88 static void
89 test_isfinited ()
90 {
91   /* Zero. */
92   ASSERT (isfinite (0.0));
93   /* Subnormal values. */
94   ASSERT (isfinite (DBL_MIN / 2));
95   ASSERT (isfinite (-DBL_MIN / 2));
96   /* Finite values. */
97   ASSERT (isfinite (3.141));
98   ASSERT (isfinite (3.141e30));
99   ASSERT (isfinite (3.141e-30));
100   ASSERT (isfinite (-2.718));
101   ASSERT (isfinite (-2.718e30));
102   ASSERT (isfinite (-2.718e-30));
103   /* Infinite values.  */
104   ASSERT (!isfinite (1.0 / 0.0));
105   ASSERT (!isfinite (-1.0 / 0.0));
106   /* Quiet NaN.  */
107   ASSERT (!isfinite (zerod / zerod));
108 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
109   /* Signalling NaN.  */
110   {
111     #define NWORDS \
112       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
113     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
114     memory_double m;
115     m.value = zerod / zerod;
116 # if DBL_EXPBIT0_BIT > 0
117     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
118 # else
119     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
120       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
121 # endif
122     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
123       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
124     ASSERT (!isfinite (m.value));
125     #undef NWORDS
126   }
127 #endif
128 }
129
130 static void
131 test_isfinitel ()
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 (1.0L / 0.0L));
147   ASSERT (!isfinite (-1.0L / 0.0L));
148   /* Quiet NaN.  */
149   ASSERT (!isfinite (zerol / zerol));
150 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
151   /* A bit pattern that is different from a Quiet NaN.  With a bit of luck,
152      it's a Signalling NaN.  */
153   {
154     #define NWORDS \
155       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
156     typedef union { unsigned int word[NWORDS]; long double value; }
157           memory_long_double;
158     memory_long_double m;
159     m.value = zerol / zerol;
160 # if LDBL_EXPBIT0_BIT > 0
161     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
162 # else
163     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
164       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
165 # endif
166     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
167       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
168     ASSERT (!isfinite (m.value));
169     #undef NWORDS
170   }
171 #endif
172 }
173
174 int
175 main ()
176 {
177   test_isfinitef ();
178   test_isfinited ();
179   test_isfinitel ();
180   return 0;
181 }