9581190cc67a09a6b55b9fdcd18b8cc568d8cd04
[platform/upstream/libunistring.git] / tests / test-signbit.c
1 /* Test of signbit() substitute.
2    Copyright (C) 2007, 2008, 2009, 2010 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 "macros.h"
32
33 float zerof = 0.0f;
34 double zerod = 0.0;
35 long double zerol = 0.0L;
36
37 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0f.
38    So we use -zerof instead.  */
39
40 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
41    So we use -zerod instead.  */
42
43 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
44    So we use minus_zerol instead.
45    IRIX cc can't put -0.0L into .data, but can compute at runtime.
46    Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
47    platforms, such as when cross-compiling to PowerPC on MacOS X 10.5.  */
48 #if defined __hpux || defined __sgi
49 static long double
50 compute_minus_zerol (void)
51 {
52   return -LDBL_MIN * LDBL_MIN;
53 }
54 # define minus_zerol compute_minus_zerol ()
55 #else
56 long double minus_zerol = -0.0L;
57 #endif
58
59 static void
60 test_signbitf ()
61 {
62   /* Finite values.  */
63   ASSERT (!signbit (3.141f));
64   ASSERT (!signbit (3.141e30f));
65   ASSERT (!signbit (3.141e-30f));
66   ASSERT (signbit (-2.718f));
67   ASSERT (signbit (-2.718e30f));
68   ASSERT (signbit (-2.718e-30f));
69   /* Zeros.  */
70   ASSERT (!signbit (0.0f));
71   if (1.0f / -zerof < 0)
72     ASSERT (signbit (-zerof));
73   else
74     ASSERT (!signbit (-zerof));
75   /* Infinite values.  */
76   ASSERT (!signbit (1.0f / 0.0f));
77   ASSERT (signbit (-1.0f / 0.0f));
78   /* Quiet NaN.  */
79   (void) signbit (zerof / zerof);
80 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
81   /* Signalling NaN.  */
82   {
83     #define NWORDS \
84       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
85     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
86     memory_float m;
87     m.value = zerof / zerof;
88 # if FLT_EXPBIT0_BIT > 0
89     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
90 # else
91     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
92       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
93 # endif
94     if (FLT_EXPBIT0_WORD < NWORDS / 2)
95       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
96     else
97       m.word[0] |= (unsigned int) 1;
98     (void) signbit (m.value);
99     #undef NWORDS
100   }
101 #endif
102 }
103
104 static void
105 test_signbitd ()
106 {
107   /* Finite values.  */
108   ASSERT (!signbit (3.141));
109   ASSERT (!signbit (3.141e30));
110   ASSERT (!signbit (3.141e-30));
111   ASSERT (signbit (-2.718));
112   ASSERT (signbit (-2.718e30));
113   ASSERT (signbit (-2.718e-30));
114   /* Zeros.  */
115   ASSERT (!signbit (0.0));
116   if (1.0 / -zerod < 0)
117     ASSERT (signbit (-zerod));
118   else
119     ASSERT (!signbit (-zerod));
120   /* Infinite values.  */
121   ASSERT (!signbit (1.0 / 0.0));
122   ASSERT (signbit (-1.0 / 0.0));
123   /* Quiet NaN.  */
124   (void) signbit (zerod / zerod);
125 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
126   /* Signalling NaN.  */
127   {
128     #define NWORDS \
129       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
130     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
131     memory_double m;
132     m.value = zerod / zerod;
133 # if DBL_EXPBIT0_BIT > 0
134     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
135 # else
136     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
137       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
138 # endif
139     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
140       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
141     (void) signbit (m.value);
142     #undef NWORDS
143   }
144 #endif
145 }
146
147 static void
148 test_signbitl ()
149 {
150   /* Finite values.  */
151   ASSERT (!signbit (3.141L));
152   ASSERT (!signbit (3.141e30L));
153   ASSERT (!signbit (3.141e-30L));
154   ASSERT (signbit (-2.718L));
155   ASSERT (signbit (-2.718e30L));
156   ASSERT (signbit (-2.718e-30L));
157   /* Zeros.  */
158   ASSERT (!signbit (0.0L));
159   if (1.0L / minus_zerol < 0)
160     ASSERT (signbit (minus_zerol));
161   else
162     ASSERT (!signbit (minus_zerol));
163   /* Infinite values.  */
164   ASSERT (!signbit (1.0L / 0.0L));
165   ASSERT (signbit (-1.0L / 0.0L));
166   /* Quiet NaN.  */
167   (void) signbit (zerol / zerol);
168 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
169   /* Signalling NaN.  */
170   {
171     #define NWORDS \
172       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
173     typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
174     memory_long_double m;
175     m.value = zerol / zerol;
176 # if LDBL_EXPBIT0_BIT > 0
177     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
178 # else
179     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
180       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
181 # endif
182     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
183       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
184     (void) signbit (m.value);
185     #undef NWORDS
186   }
187 #endif
188 }
189
190 int
191 main ()
192 {
193   test_signbitf ();
194   test_signbitd ();
195   test_signbitl ();
196   return 0;
197 }