Better ASSERT macro.
[gnulib.git] / tests / test-signbit.c
1 /* Test of signbit() substitute.
2    Copyright (C) 2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include <math.h>
23
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #define ASSERT(expr) \
29   do                                                                         \
30     {                                                                        \
31       if (!(expr))                                                           \
32         {                                                                    \
33           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
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   /* Infinite values.  */
54   ASSERT (!signbit (1.0f / 0.0f));
55   ASSERT (signbit (-1.0f / 0.0f));
56   /* Quiet NaN.  */
57   (void) signbit (zerof / zerof);
58 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
59   /* Signalling NaN.  */
60   {
61     #define NWORDS \
62       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
63     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
64     memory_float m;
65     m.value = zerof / zerof;
66 # if FLT_EXPBIT0_BIT > 0
67     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
68 # else
69     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
70       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
71 # endif
72     if (FLT_EXPBIT0_WORD < NWORDS / 2)
73       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
74     else
75       m.word[0] |= (unsigned int) 1;
76     (void) signbit (m.value);
77     #undef NWORDS
78   }
79 #endif
80 }
81
82 static void
83 test_signbitd ()
84 {
85   /* Finite values.  */
86   ASSERT (!signbit (3.141));
87   ASSERT (!signbit (3.141e30));
88   ASSERT (!signbit (3.141e-30));
89   ASSERT (signbit (-2.718));
90   ASSERT (signbit (-2.718e30));
91   ASSERT (signbit (-2.718e-30));
92   /* Infinite values.  */
93   ASSERT (!signbit (1.0 / 0.0));
94   ASSERT (signbit (-1.0 / 0.0));
95   /* Quiet NaN.  */
96   (void) signbit (zerod / zerod);
97 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
98   /* Signalling NaN.  */
99   {
100     #define NWORDS \
101       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
102     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
103     memory_double m;
104     m.value = zerod / zerod;
105 # if DBL_EXPBIT0_BIT > 0
106     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
107 # else
108     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
109       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
110 # endif
111     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
112       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
113     (void) signbit (m.value);
114     #undef NWORDS
115   }
116 #endif
117 }
118
119 static void
120 test_signbitl ()
121 {
122   /* Finite values.  */
123   ASSERT (!signbit (3.141L));
124   ASSERT (!signbit (3.141e30L));
125   ASSERT (!signbit (3.141e-30L));
126   ASSERT (signbit (-2.718L));
127   ASSERT (signbit (-2.718e30L));
128   ASSERT (signbit (-2.718e-30L));
129   /* Infinite values.  */
130   ASSERT (!signbit (1.0L / 0.0L));
131   ASSERT (signbit (-1.0L / 0.0L));
132   /* Quiet NaN.  */
133   (void) signbit (zerol / zerol);
134 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
135   /* Signalling NaN.  */
136   {
137     #define NWORDS \
138       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
139     typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
140     memory_long_double m;
141     m.value = zerol / zerol;
142 # if LDBL_EXPBIT0_BIT > 0
143     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
144 # else
145     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
146       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
147 # endif
148     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
149       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
150     (void) signbit (m.value);
151     #undef NWORDS
152   }
153 #endif
154 }
155
156 int
157 main ()
158 {
159   test_signbitf ();
160   test_signbitd ();
161   test_signbitl ();
162   return 0;
163 }