ChangeLog: fix wrong attribution in last commit
[gnulib.git] / tests / test-signbit.c
1 /* Test of signbit() substitute.
2    Copyright (C) 2007-2011 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
31 #include "minus-zero.h"
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_signbitf ()
40 {
41   /* Finite values.  */
42   ASSERT (!signbit (3.141f));
43   ASSERT (!signbit (3.141e30f));
44   ASSERT (!signbit (3.141e-30f));
45   ASSERT (signbit (-2.718f));
46   ASSERT (signbit (-2.718e30f));
47   ASSERT (signbit (-2.718e-30f));
48   /* Zeros.  */
49   ASSERT (!signbit (0.0f));
50   if (1.0f / minus_zerof < 0)
51     ASSERT (signbit (minus_zerof));
52   else
53     ASSERT (!signbit (minus_zerof));
54   /* Infinite values.  */
55   ASSERT (!signbit (1.0f / 0.0f));
56   ASSERT (signbit (-1.0f / 0.0f));
57   /* Quiet NaN.  */
58   (void) signbit (zerof / zerof);
59 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
60   /* Signalling NaN.  */
61   {
62     #define NWORDS \
63       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
64     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
65     memory_float m;
66     m.value = zerof / zerof;
67 # if FLT_EXPBIT0_BIT > 0
68     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
69 # else
70     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
71       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
72 # endif
73     if (FLT_EXPBIT0_WORD < NWORDS / 2)
74       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
75     else
76       m.word[0] |= (unsigned int) 1;
77     (void) signbit (m.value);
78     #undef NWORDS
79   }
80 #endif
81 }
82
83 static void
84 test_signbitd ()
85 {
86   /* Finite values.  */
87   ASSERT (!signbit (3.141));
88   ASSERT (!signbit (3.141e30));
89   ASSERT (!signbit (3.141e-30));
90   ASSERT (signbit (-2.718));
91   ASSERT (signbit (-2.718e30));
92   ASSERT (signbit (-2.718e-30));
93   /* Zeros.  */
94   ASSERT (!signbit (0.0));
95   if (1.0 / minus_zerod < 0)
96     ASSERT (signbit (minus_zerod));
97   else
98     ASSERT (!signbit (minus_zerod));
99   /* Infinite values.  */
100   ASSERT (!signbit (1.0 / 0.0));
101   ASSERT (signbit (-1.0 / 0.0));
102   /* Quiet NaN.  */
103   (void) signbit (zerod / zerod);
104 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
105   /* Signalling NaN.  */
106   {
107     #define NWORDS \
108       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
109     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
110     memory_double m;
111     m.value = zerod / zerod;
112 # if DBL_EXPBIT0_BIT > 0
113     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
114 # else
115     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
116       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
117 # endif
118     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
119       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
120     (void) signbit (m.value);
121     #undef NWORDS
122   }
123 #endif
124 }
125
126 static void
127 test_signbitl ()
128 {
129   /* Finite values.  */
130   ASSERT (!signbit (3.141L));
131   ASSERT (!signbit (3.141e30L));
132   ASSERT (!signbit (3.141e-30L));
133   ASSERT (signbit (-2.718L));
134   ASSERT (signbit (-2.718e30L));
135   ASSERT (signbit (-2.718e-30L));
136   /* Zeros.  */
137   ASSERT (!signbit (0.0L));
138   if (1.0L / minus_zerol < 0)
139     ASSERT (signbit (minus_zerol));
140   else
141     ASSERT (!signbit (minus_zerol));
142   /* Infinite values.  */
143   ASSERT (!signbit (1.0L / 0.0L));
144   ASSERT (signbit (-1.0L / 0.0L));
145   /* Quiet NaN.  */
146   (void) signbit (zerol / zerol);
147 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
148   /* Signalling NaN.  */
149   {
150     #define NWORDS \
151       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
152     typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
153     memory_long_double m;
154     m.value = zerol / zerol;
155 # if LDBL_EXPBIT0_BIT > 0
156     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
157 # else
158     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
159       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
160 # endif
161     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
162       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
163     (void) signbit (m.value);
164     #undef NWORDS
165   }
166 #endif
167 }
168
169 int
170 main ()
171 {
172   test_signbitf ();
173   test_signbitd ();
174   test_signbitl ();
175   return 0;
176 }