95ba61305c95667bfc84f3eb5625014456c2676d
[pspp] / tests / test-signbit.c
1 /* Test of signbit() 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 Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <math.h>
22
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #define ASSERT(expr) \
28   do                                                                         \
29     {                                                                        \
30       if (!(expr))                                                           \
31         {                                                                    \
32           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
33           fflush (stderr);                                                   \
34           abort ();                                                          \
35         }                                                                    \
36     }                                                                        \
37   while (0)
38
39 float zerof = 0.0f;
40 double zerod = 0.0;
41 long double zerol = 0.0L;
42
43 static void
44 test_signbitf ()
45 {
46   /* Finite values.  */
47   ASSERT (!signbit (3.141f));
48   ASSERT (!signbit (3.141e30f));
49   ASSERT (!signbit (3.141e-30f));
50   ASSERT (signbit (-2.718f));
51   ASSERT (signbit (-2.718e30f));
52   ASSERT (signbit (-2.718e-30f));
53   /* Zeros.  */
54   ASSERT (!signbit (0.0f));
55   if (1.0f / -zerof < 0)
56     ASSERT (signbit (-zerof));
57   else
58     ASSERT (!signbit (-zerof));
59   /* Infinite values.  */
60   ASSERT (!signbit (1.0f / 0.0f));
61   ASSERT (signbit (-1.0f / 0.0f));
62   /* Quiet NaN.  */
63   (void) signbit (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     (void) signbit (m.value);
83     #undef NWORDS
84   }
85 #endif
86 }
87
88 static void
89 test_signbitd ()
90 {
91   /* Finite values.  */
92   ASSERT (!signbit (3.141));
93   ASSERT (!signbit (3.141e30));
94   ASSERT (!signbit (3.141e-30));
95   ASSERT (signbit (-2.718));
96   ASSERT (signbit (-2.718e30));
97   ASSERT (signbit (-2.718e-30));
98   /* Zeros.  */
99   ASSERT (!signbit (0.0));
100   if (1.0 / -zerod < 0)
101     ASSERT (signbit (-zerod));
102   else
103     ASSERT (!signbit (-zerod));
104   /* Infinite values.  */
105   ASSERT (!signbit (1.0 / 0.0));
106   ASSERT (signbit (-1.0 / 0.0));
107   /* Quiet NaN.  */
108   (void) signbit (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     (void) signbit (m.value);
126     #undef NWORDS
127   }
128 #endif
129 }
130
131 static void
132 test_signbitl ()
133 {
134   /* Finite values.  */
135   ASSERT (!signbit (3.141L));
136   ASSERT (!signbit (3.141e30L));
137   ASSERT (!signbit (3.141e-30L));
138   ASSERT (signbit (-2.718L));
139   ASSERT (signbit (-2.718e30L));
140   ASSERT (signbit (-2.718e-30L));
141   /* Zeros.  */
142   ASSERT (!signbit (0.0L));
143   if (1.0L / -zerol < 0)
144     ASSERT (signbit (-zerol));
145   else
146     ASSERT (!signbit (-zerol));
147   /* Infinite values.  */
148   ASSERT (!signbit (1.0L / 0.0L));
149   ASSERT (signbit (-1.0L / 0.0L));
150   /* Quiet NaN.  */
151   (void) signbit (zerol / zerol);
152 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
153   /* Signalling NaN.  */
154   {
155     #define NWORDS \
156       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
157     typedef union { long double value; unsigned int word[NWORDS]; } 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     (void) signbit (m.value);
169     #undef NWORDS
170   }
171 #endif
172 }
173
174 int
175 main ()
176 {
177   test_signbitf ();
178   test_signbitd ();
179   test_signbitl ();
180   return 0;
181 }