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