Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-isinf.c
1 /* Test of isinf() 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, 2008, 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_isinff ()
47 {
48   /* Zero. */
49   ASSERT (!isinf (0.0f));
50   /* Subnormal values. */
51   ASSERT (!isinf (FLT_MIN / 2));
52   ASSERT (!isinf (-FLT_MIN / 2));
53   /* Finite values.  */
54   ASSERT (!isinf (3.141f));
55   ASSERT (!isinf (3.141e30f));
56   ASSERT (!isinf (3.141e-30f));
57   ASSERT (!isinf (-2.718f));
58   ASSERT (!isinf (-2.718e30f));
59   ASSERT (!isinf (-2.718e-30f));
60   ASSERT (!isinf (FLT_MAX));
61   ASSERT (!isinf (-FLT_MAX));
62   /* Infinite values.  */
63   ASSERT (isinf (1.0f / 0.0f));
64   ASSERT (isinf (-1.0f / 0.0f));
65   /* Quiet NaN.  */
66   ASSERT (!isinf (zerof / zerof));
67 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
68   /* Signalling NaN.  */
69   {
70     #define NWORDS \
71       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
72     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
73     memory_float m;
74     m.value = zerof / zerof;
75 # if FLT_EXPBIT0_BIT > 0
76     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
77 # else
78     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
79       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
80 # endif
81     if (FLT_EXPBIT0_WORD < NWORDS / 2)
82       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
83     else
84       m.word[0] |= (unsigned int) 1;
85     ASSERT (!isinf (m.value));
86     #undef NWORDS
87   }
88 #endif
89 }
90
91 static void
92 test_isinfd ()
93 {
94   /* Zero. */
95   ASSERT (!isinf (0.0));
96   /* Subnormal values. */
97   ASSERT (!isinf (DBL_MIN / 2));
98   ASSERT (!isinf (-DBL_MIN / 2));
99   /* Finite values. */
100   ASSERT (!isinf (3.141));
101   ASSERT (!isinf (3.141e30));
102   ASSERT (!isinf (3.141e-30));
103   ASSERT (!isinf (-2.718));
104   ASSERT (!isinf (-2.718e30));
105   ASSERT (!isinf (-2.718e-30));
106   ASSERT (!isinf (DBL_MAX));
107   ASSERT (!isinf (-DBL_MAX));
108   /* Infinite values.  */
109   ASSERT (isinf (1.0 / 0.0));
110   ASSERT (isinf (-1.0 / 0.0));
111   /* Quiet NaN.  */
112   ASSERT (!isinf (zerod / zerod));
113 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
114   /* Signalling NaN.  */
115   {
116     #define NWORDS \
117       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
118     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
119     memory_double m;
120     m.value = zerod / zerod;
121 # if DBL_EXPBIT0_BIT > 0
122     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
123 # else
124     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
125       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
126 # endif
127     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
128       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
129     ASSERT (!isinf (m.value));
130     #undef NWORDS
131   }
132 #endif
133 }
134
135 static void
136 test_isinfl ()
137 {
138   #define NWORDS \
139     ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
140   typedef union { unsigned int word[NWORDS]; long double value; }
141           memory_long_double;
142
143   /* Zero. */
144   ASSERT (!isinf (0.0L));
145   /* Subnormal values. */
146   ASSERT (!isinf (LDBL_MIN / 2));
147   ASSERT (!isinf (-LDBL_MIN / 2));
148   /* Finite values. */
149   ASSERT (!isinf (3.141L));
150   ASSERT (!isinf (3.141e30L));
151   ASSERT (!isinf (3.141e-30L));
152   ASSERT (!isinf (-2.718L));
153   ASSERT (!isinf (-2.718e30L));
154   ASSERT (!isinf (-2.718e-30L));
155   ASSERT (!isinf (LDBL_MAX));
156   ASSERT (!isinf (-LDBL_MAX));
157   /* Infinite values.  */
158   ASSERT (isinf (1.0L / 0.0L));
159   ASSERT (isinf (-1.0L / 0.0L));
160   /* Quiet NaN.  */
161   ASSERT (!isinf (zerol / zerol));
162
163 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
164   /* A bit pattern that is different from a Quiet NaN.  With a bit of luck,
165      it's a Signalling NaN.  */
166   {
167     memory_long_double m;
168     m.value = zerol / zerol;
169 # if LDBL_EXPBIT0_BIT > 0
170     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
171 # else
172     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
173       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
174 # endif
175     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
176       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
177     ASSERT (!isinf (m.value));
178   }
179 #endif
180
181 #if ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_))
182 /* Representation of an 80-bit 'long double' as an initializer for a sequence
183    of 'unsigned int' words.  */
184 # ifdef WORDS_BIGENDIAN
185 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
186      { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
187        ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16),    \
188        (unsigned int) (mantlo) << 16                                        \
189      }
190 # else
191 #  define LDBL80_WORDS(exponent,manthi,mantlo) \
192      { mantlo, manthi, exponent }
193 # endif
194   { /* Quiet NaN.  */
195     static memory_long_double x =
196       { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
197     ASSERT (!isinf (x.value));
198   }
199   {
200     /* Signalling NaN.  */
201     static memory_long_double x =
202       { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
203     ASSERT (!isinf (x.value));
204   }
205   /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities,
206      Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in
207        Intel IA-64 Architecture Software Developer's Manual, Volume 1:
208        Application Architecture.
209        Table 5-2 "Floating-Point Register Encodings"
210        Figure 5-6 "Memory to Floating-Point Register Data Translation"
211    */
212   { /* Pseudo-NaN.  */
213     static memory_long_double x =
214       { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
215     ASSERT (!isinf (x.value));
216   }
217   { /* Pseudo-Infinity.  */
218     static memory_long_double x =
219       { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
220     ASSERT (!isinf (x.value));
221   }
222   { /* Pseudo-Zero.  */
223     static memory_long_double x =
224       { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
225     ASSERT (!isinf (x.value));
226   }
227   { /* Unnormalized number.  */
228     static memory_long_double x =
229       { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
230     ASSERT (!isinf (x.value));
231   }
232   { /* Pseudo-Denormal.  */
233     static memory_long_double x =
234       { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
235     ASSERT (!isinf (x.value));
236   }
237 #endif
238
239   #undef NWORDS
240 }
241
242 int
243 main ()
244 {
245   test_isinff ();
246   test_isinfd ();
247   test_isinfl ();
248   return 0;
249 }