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