tests: add signature checks
[gnulib.git] / tests / test-signbit.c
1 /* Test of signbit() substitute.
2    Copyright (C) 2007, 2008, 2009 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 Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <math.h>
22
23 /* signbit must be a macro.  */
24 #ifndef signbit
25 # error missing declaration
26 #endif
27
28 #include <float.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #define ASSERT(expr) \
34   do                                                                         \
35     {                                                                        \
36       if (!(expr))                                                           \
37         {                                                                    \
38           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
39           fflush (stderr);                                                   \
40           abort ();                                                          \
41         }                                                                    \
42     }                                                                        \
43   while (0)
44
45 float zerof = 0.0f;
46 double zerod = 0.0;
47 long double zerol = 0.0L;
48
49 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0f.
50    So we use -zerof instead.  */
51
52 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
53    So we use -zerod instead.  */
54
55 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
56    So we use minus_zerol instead.
57    IRIX cc can't put -0.0L into .data, but can compute at runtime.
58    Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
59    platforms, such as when cross-compiling to PowerPC on MacOS X 10.5.  */
60 #if defined __hpux || defined __sgi
61 static long double
62 compute_minus_zerol (void)
63 {
64   return -LDBL_MIN * LDBL_MIN;
65 }
66 # define minus_zerol compute_minus_zerol ()
67 #else
68 long double minus_zerol = -0.0L;
69 #endif
70
71 static void
72 test_signbitf ()
73 {
74   /* Finite values.  */
75   ASSERT (!signbit (3.141f));
76   ASSERT (!signbit (3.141e30f));
77   ASSERT (!signbit (3.141e-30f));
78   ASSERT (signbit (-2.718f));
79   ASSERT (signbit (-2.718e30f));
80   ASSERT (signbit (-2.718e-30f));
81   /* Zeros.  */
82   ASSERT (!signbit (0.0f));
83   if (1.0f / -zerof < 0)
84     ASSERT (signbit (-zerof));
85   else
86     ASSERT (!signbit (-zerof));
87   /* Infinite values.  */
88   ASSERT (!signbit (1.0f / 0.0f));
89   ASSERT (signbit (-1.0f / 0.0f));
90   /* Quiet NaN.  */
91   (void) signbit (zerof / zerof);
92 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
93   /* Signalling NaN.  */
94   {
95     #define NWORDS \
96       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
97     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
98     memory_float m;
99     m.value = zerof / zerof;
100 # if FLT_EXPBIT0_BIT > 0
101     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
102 # else
103     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
104       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
105 # endif
106     if (FLT_EXPBIT0_WORD < NWORDS / 2)
107       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
108     else
109       m.word[0] |= (unsigned int) 1;
110     (void) signbit (m.value);
111     #undef NWORDS
112   }
113 #endif
114 }
115
116 static void
117 test_signbitd ()
118 {
119   /* Finite values.  */
120   ASSERT (!signbit (3.141));
121   ASSERT (!signbit (3.141e30));
122   ASSERT (!signbit (3.141e-30));
123   ASSERT (signbit (-2.718));
124   ASSERT (signbit (-2.718e30));
125   ASSERT (signbit (-2.718e-30));
126   /* Zeros.  */
127   ASSERT (!signbit (0.0));
128   if (1.0 / -zerod < 0)
129     ASSERT (signbit (-zerod));
130   else
131     ASSERT (!signbit (-zerod));
132   /* Infinite values.  */
133   ASSERT (!signbit (1.0 / 0.0));
134   ASSERT (signbit (-1.0 / 0.0));
135   /* Quiet NaN.  */
136   (void) signbit (zerod / zerod);
137 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
138   /* Signalling NaN.  */
139   {
140     #define NWORDS \
141       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
142     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
143     memory_double m;
144     m.value = zerod / zerod;
145 # if DBL_EXPBIT0_BIT > 0
146     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
147 # else
148     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
149       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
150 # endif
151     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
152       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
153     (void) signbit (m.value);
154     #undef NWORDS
155   }
156 #endif
157 }
158
159 static void
160 test_signbitl ()
161 {
162   /* Finite values.  */
163   ASSERT (!signbit (3.141L));
164   ASSERT (!signbit (3.141e30L));
165   ASSERT (!signbit (3.141e-30L));
166   ASSERT (signbit (-2.718L));
167   ASSERT (signbit (-2.718e30L));
168   ASSERT (signbit (-2.718e-30L));
169   /* Zeros.  */
170   ASSERT (!signbit (0.0L));
171   if (1.0L / minus_zerol < 0)
172     ASSERT (signbit (minus_zerol));
173   else
174     ASSERT (!signbit (minus_zerol));
175   /* Infinite values.  */
176   ASSERT (!signbit (1.0L / 0.0L));
177   ASSERT (signbit (-1.0L / 0.0L));
178   /* Quiet NaN.  */
179   (void) signbit (zerol / zerol);
180 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
181   /* Signalling NaN.  */
182   {
183     #define NWORDS \
184       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
185     typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
186     memory_long_double m;
187     m.value = zerol / zerol;
188 # if LDBL_EXPBIT0_BIT > 0
189     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
190 # else
191     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
192       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
193 # endif
194     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
195       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
196     (void) signbit (m.value);
197     #undef NWORDS
198   }
199 #endif
200 }
201
202 int
203 main ()
204 {
205   test_signbitf ();
206   test_signbitd ();
207   test_signbitl ();
208   return 0;
209 }