Flush the standard error stream before aborting.
[gnulib.git] / tests / test-isfinite.c
1 /* Test of isfinite() substitute.
2    Copyright (C) 2007-2008 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           fflush (stderr);                                                   \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 float zerof = 0.0f;
42 double zerod = 0.0;
43 long double zerol = 0.0L;
44
45 static void
46 test_isfinitef ()
47 {
48   /* Zero. */
49   ASSERT (isfinite (0.0f));
50   /* Subnormal values. */
51   ASSERT (isfinite (FLT_MIN / 2));
52   ASSERT (isfinite (-FLT_MIN / 2));
53   /* Finite values.  */
54   ASSERT (isfinite (3.141f));
55   ASSERT (isfinite (3.141e30f));
56   ASSERT (isfinite (3.141e-30f));
57   ASSERT (isfinite (-2.718f));
58   ASSERT (isfinite (-2.718e30f));
59   ASSERT (isfinite (-2.718e-30f));
60   /* Infinite values.  */
61   ASSERT (!isfinite (1.0f / 0.0f));
62   ASSERT (!isfinite (-1.0f / 0.0f));
63   /* Quiet NaN.  */
64   ASSERT (!isfinite (zerof / zerof));
65 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
66   /* Signalling NaN.  */
67   {
68     #define NWORDS \
69       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
70     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
71     memory_float m;
72     m.value = zerof / zerof;
73 # if FLT_EXPBIT0_BIT > 0
74     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
75 # else
76     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
77       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
78 # endif
79     if (FLT_EXPBIT0_WORD < NWORDS / 2)
80       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
81     else
82       m.word[0] |= (unsigned int) 1;
83     ASSERT (!isfinite (m.value));
84     #undef NWORDS
85   }
86 #endif
87 }
88
89 static void
90 test_isfinited ()
91 {
92   /* Zero. */
93   ASSERT (isfinite (0.0));
94   /* Subnormal values. */
95   ASSERT (isfinite (DBL_MIN / 2));
96   ASSERT (isfinite (-DBL_MIN / 2));
97   /* Finite values. */
98   ASSERT (isfinite (3.141));
99   ASSERT (isfinite (3.141e30));
100   ASSERT (isfinite (3.141e-30));
101   ASSERT (isfinite (-2.718));
102   ASSERT (isfinite (-2.718e30));
103   ASSERT (isfinite (-2.718e-30));
104   /* Infinite values.  */
105   ASSERT (!isfinite (1.0 / 0.0));
106   ASSERT (!isfinite (-1.0 / 0.0));
107   /* Quiet NaN.  */
108   ASSERT (!isfinite (zerod / zerod));
109 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
110   /* Signalling NaN.  */
111   {
112     #define NWORDS \
113       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
114     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
115     memory_double m;
116     m.value = zerod / zerod;
117 # if DBL_EXPBIT0_BIT > 0
118     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
119 # else
120     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
121       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
122 # endif
123     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
124       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
125     ASSERT (!isfinite (m.value));
126     #undef NWORDS
127   }
128 #endif
129 }
130
131 static void
132 test_isfinitel ()
133 {
134   /* Zero. */
135   ASSERT (isfinite (0.0L));
136   /* Subnormal values. */
137   ASSERT (isfinite (LDBL_MIN / 2));
138   ASSERT (isfinite (-LDBL_MIN / 2));
139   /* Finite values. */
140   ASSERT (isfinite (3.141L));
141   ASSERT (isfinite (3.141e30L));
142   ASSERT (isfinite (3.141e-30L));
143   ASSERT (isfinite (-2.718L));
144   ASSERT (isfinite (-2.718e30L));
145   ASSERT (isfinite (-2.718e-30L));
146   /* Infinite values.  */
147   ASSERT (!isfinite (1.0L / 0.0L));
148   ASSERT (!isfinite (-1.0L / 0.0L));
149   /* Quiet NaN.  */
150   ASSERT (!isfinite (zerol / zerol));
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     #define NWORDS \
156       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
157     typedef union { unsigned int word[NWORDS]; long double value; }
158           memory_long_double;
159     memory_long_double m;
160     m.value = zerol / zerol;
161 # if LDBL_EXPBIT0_BIT > 0
162     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
163 # else
164     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
165       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
166 # endif
167     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
168       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
169     ASSERT (!isfinite (m.value));
170     #undef NWORDS
171   }
172 #endif
173 }
174
175 int
176 main ()
177 {
178   test_isfinitef ();
179   test_isfinited ();
180   test_isfinitel ();
181   return 0;
182 }