Change copyright notice from GPLv2+ to GPLv3+.
[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 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           abort ();                                                          \
34         }                                                                    \
35     }                                                                        \
36   while (0)
37
38 float zerof = 0.0f;
39 double zerod = 0.0;
40 long double zerol = 0.0L;
41
42 static void
43 test_signbitf ()
44 {
45   /* Finite values.  */
46   ASSERT (!signbit (3.141f));
47   ASSERT (!signbit (3.141e30f));
48   ASSERT (!signbit (3.141e-30f));
49   ASSERT (signbit (-2.718f));
50   ASSERT (signbit (-2.718e30f));
51   ASSERT (signbit (-2.718e-30f));
52   /* Infinite values.  */
53   ASSERT (!signbit (1.0f / 0.0f));
54   ASSERT (signbit (-1.0f / 0.0f));
55   /* Quiet NaN.  */
56   (void) signbit (zerof / zerof);
57 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
58   /* Signalling NaN.  */
59   {
60     #define NWORDS \
61       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
62     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
63     memory_float m;
64     m.value = zerof / zerof;
65 # if FLT_EXPBIT0_BIT > 0
66     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
67 # else
68     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
69       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
70 # endif
71     if (FLT_EXPBIT0_WORD < NWORDS / 2)
72       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
73     else
74       m.word[0] |= (unsigned int) 1;
75     (void) signbit (m.value);
76     #undef NWORDS
77   }
78 #endif
79 }
80
81 static void
82 test_signbitd ()
83 {
84   /* Finite values.  */
85   ASSERT (!signbit (3.141));
86   ASSERT (!signbit (3.141e30));
87   ASSERT (!signbit (3.141e-30));
88   ASSERT (signbit (-2.718));
89   ASSERT (signbit (-2.718e30));
90   ASSERT (signbit (-2.718e-30));
91   /* Infinite values.  */
92   ASSERT (!signbit (1.0 / 0.0));
93   ASSERT (signbit (-1.0 / 0.0));
94   /* Quiet NaN.  */
95   (void) signbit (zerod / zerod);
96 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
97   /* Signalling NaN.  */
98   {
99     #define NWORDS \
100       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
101     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
102     memory_double m;
103     m.value = zerod / zerod;
104 # if DBL_EXPBIT0_BIT > 0
105     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
106 # else
107     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
108       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
109 # endif
110     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
111       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
112     (void) signbit (m.value);
113     #undef NWORDS
114   }
115 #endif
116 }
117
118 static void
119 test_signbitl ()
120 {
121   /* Finite values.  */
122   ASSERT (!signbit (3.141L));
123   ASSERT (!signbit (3.141e30L));
124   ASSERT (!signbit (3.141e-30L));
125   ASSERT (signbit (-2.718L));
126   ASSERT (signbit (-2.718e30L));
127   ASSERT (signbit (-2.718e-30L));
128   /* Infinite values.  */
129   ASSERT (!signbit (1.0L / 0.0L));
130   ASSERT (signbit (-1.0L / 0.0L));
131   /* Quiet NaN.  */
132   (void) signbit (zerol / zerol);
133 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
134   /* Signalling NaN.  */
135   {
136     #define NWORDS \
137       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
138     typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
139     memory_long_double m;
140     m.value = zerol / zerol;
141 # if LDBL_EXPBIT0_BIT > 0
142     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
143 # else
144     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
145       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
146 # endif
147     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
148       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
149     (void) signbit (m.value);
150     #undef NWORDS
151   }
152 #endif
153 }
154
155 int
156 main ()
157 {
158   test_signbitf ();
159   test_signbitd ();
160   test_signbitl ();
161   return 0;
162 }