math: make test-fenv-preserve.c a no-op if FE_ALL_EXCEPT == 0.
[glibc.git] / math / basic-test.c
blob77f1093f31f67c7f570d5fd03cbef7ca1b17fa9a
1 /* Copyright (C) 1999-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Andreas Jaeger <aj@suse.de>, 1999.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <math.h>
20 #include <float.h>
21 #include <stdio.h>
23 #include <math-tests.h>
26 static int errors = 0;
29 static void
30 check (const char *testname, int result)
32 if (!result) {
33 printf ("Failure: %s\n", testname);
34 errors++;
38 #define TEST_FUNC(NAME, FLOAT, SUFFIX, EPSILON, HUGEVAL) \
39 static void \
40 NAME (void) \
41 { \
42 /* Variables are declared volatile to forbid some compiler \
43 optimizations. */ \
44 volatile FLOAT Inf_var, qNaN_var, zero_var, one_var; \
45 /* A sNaN is only guaranteed to be representable in variables with */ \
46 /* static (or thread-local) storage duration. */ \
47 static volatile FLOAT sNaN_var = __builtin_nans ## SUFFIX (""); \
48 static volatile FLOAT minus_sNaN_var = -__builtin_nans ## SUFFIX (""); \
49 FLOAT x1, x2; \
51 zero_var = 0.0; \
52 one_var = 1.0; \
53 qNaN_var = __builtin_nan ## SUFFIX (""); \
54 Inf_var = one_var / zero_var; \
56 (void) &zero_var; \
57 (void) &one_var; \
58 (void) &qNaN_var; \
59 (void) &sNaN_var; \
60 (void) &minus_sNaN_var; \
61 (void) &Inf_var; \
64 check (#FLOAT " isinf (inf) == 1", isinf (Inf_var) == 1); \
65 check (#FLOAT " isinf (-inf) == -1", isinf (-Inf_var) == -1); \
66 check (#FLOAT " !isinf (1)", !(isinf (one_var))); \
67 check (#FLOAT " !isinf (qNaN)", !(isinf (qNaN_var))); \
68 if (SNAN_TESTS (FLOAT)) \
69 check (#FLOAT " !isinf (sNaN)", !(isinf (sNaN_var))); \
71 check (#FLOAT " isnan (qNaN)", isnan (qNaN_var)); \
72 if (SNAN_TESTS (FLOAT)) \
73 check (#FLOAT " isnan (sNaN)", isnan (sNaN_var)); \
74 check (#FLOAT " isnan (-qNaN)", isnan (-qNaN_var)); \
75 if (SNAN_TESTS (FLOAT)) \
76 check (#FLOAT " isnan (-sNaN)", isnan (minus_sNaN_var)); \
77 check (#FLOAT " !isnan (1)", !(isnan (one_var))); \
78 check (#FLOAT " !isnan (inf)", !(isnan (Inf_var))); \
80 check (#FLOAT " !issignaling (qNaN)", !(issignaling (qNaN_var))); \
81 if (SNAN_TESTS (FLOAT)) \
82 check (#FLOAT " issignaling (sNaN)", issignaling (sNaN_var)); \
83 check (#FLOAT " !issignaling (-qNaN)", !(issignaling (-qNaN_var))); \
84 if (SNAN_TESTS (FLOAT)) \
85 check (#FLOAT " issignaling (-sNaN)", issignaling (minus_sNaN_var)); \
86 check (#FLOAT " !issignaling (1)", !(issignaling (one_var))); \
87 check (#FLOAT " !issignaling (inf)", !(issignaling (Inf_var))); \
89 check (#FLOAT " inf == inf", Inf_var == Inf_var); \
90 check (#FLOAT " -inf == -inf", -Inf_var == -Inf_var); \
91 check (#FLOAT " inf != -inf", Inf_var != -Inf_var); \
92 check (#FLOAT " qNaN != qNaN", qNaN_var != qNaN_var); \
93 if (SNAN_TESTS (FLOAT)) \
94 check (#FLOAT " sNaN != sNaN", sNaN_var != sNaN_var); \
95 if (SNAN_TESTS (FLOAT)) \
96 check (#FLOAT " qNaN != sNaN", qNaN_var != sNaN_var); \
97 if (SNAN_TESTS (FLOAT)) \
98 check (#FLOAT " -sNaN != -sNaN", minus_sNaN_var != minus_sNaN_var); \
99 if (SNAN_TESTS (FLOAT)) \
100 check (#FLOAT " sNaN != -sNaN", sNaN_var != minus_sNaN_var); \
101 if (SNAN_TESTS (FLOAT)) \
102 check (#FLOAT " qNaN != -sNaN", qNaN_var != minus_sNaN_var); \
104 /* \
105 the same tests but this time with NAN from <bits/nan.h> \
106 NAN is a double const \
107 */ \
108 check (#FLOAT " isnan (NAN)", isnan (NAN)); \
109 check (#FLOAT " isnan (-NAN)", isnan (-NAN)); \
110 check (#FLOAT " !isinf (NAN)", !(isinf (NAN))); \
111 check (#FLOAT " !isinf (-NAN)", !(isinf (-NAN))); \
112 check (#FLOAT " NAN != NAN", NAN != NAN); \
113 check (#FLOAT " NAN != qNaN", NAN != qNaN_var); \
114 if (SNAN_TESTS (FLOAT)) \
115 check (#FLOAT " NAN != sNaN", NAN != sNaN_var); \
116 if (SNAN_TESTS (FLOAT)) \
117 check (#FLOAT " NAN != -sNaN", NAN != minus_sNaN_var); \
119 /* \
120 And again with the value returned by the `nan' function. \
121 */ \
122 check (#FLOAT " isnan (nan (\"\"))", isnan (nan ## SUFFIX (""))); \
123 check (#FLOAT " isnan (-nan (\"\"))", isnan (-nan ## SUFFIX (""))); \
124 check (#FLOAT " !isinf (nan (\"\"))", !(isinf (nan ## SUFFIX ("")))); \
125 check (#FLOAT " !isinf (-nan (\"\"))", !(isinf (-nan ## SUFFIX ("")))); \
126 check (#FLOAT " nan (\"\") != nan (\"\")", \
127 nan ## SUFFIX ("") != nan ## SUFFIX ("")); \
128 check (#FLOAT " nan (\"\") != qNaN", nan ## SUFFIX ("") != qNaN_var); \
129 if (SNAN_TESTS (FLOAT)) \
130 check (#FLOAT " nan (\"\") != sNaN", nan ## SUFFIX ("") != sNaN_var); \
131 if (SNAN_TESTS (FLOAT)) \
132 check (#FLOAT " nan (\"\") != -sNaN", \
133 nan ## SUFFIX ("") != minus_sNaN_var); \
135 /* test if EPSILON is ok */ \
136 x1 = 1.0; \
137 x2 = x1 + EPSILON; \
138 check (#FLOAT " 1 != 1+EPSILON", x1 != x2); \
140 x1 = 1.0; \
141 x2 = x1 - EPSILON; \
142 check (#FLOAT " 1 != 1-EPSILON", x1 != x2); \
144 /* test if HUGE_VALx is ok */ \
145 x1 = HUGEVAL; \
146 check (#FLOAT " isinf (HUGE_VALx) == +1", isinf (x1) == +1); \
147 x1 = - HUGEVAL; \
148 check (#FLOAT " isinf (-HUGE_VALx) == -1", isinf (x1) == -1); \
151 #define TEST_CONVERT(NAME, FLOAT, DOUBLE, SUFFIX) \
152 void \
153 NAME (void) \
155 volatile DOUBLE Inf_var, qNaN_var, zero_var, one_var; \
156 /* A sNaN is only guaranteed to be representable in variables with */ \
157 /* static (or thread-local) storage duration. */ \
158 static volatile DOUBLE sNaN_var = __builtin_nans ## SUFFIX (""); \
159 FLOAT x1, x2; \
161 zero_var = 0.0; \
162 one_var = 1.0; \
163 qNaN_var = __builtin_nan ## SUFFIX (""); \
164 Inf_var = one_var / zero_var; \
166 (void) &qNaN_var; \
167 (void) &sNaN_var; \
168 (void) &Inf_var; \
170 x1 = (FLOAT) qNaN_var; \
171 check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") qNaN, isnan", isnan (x1)); \
172 check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") qNaN, !issignaling", \
173 !issignaling (x1)); \
174 if (SNAN_TESTS (FLOAT)) \
176 x1 = (FLOAT) sNaN_var; \
177 check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") sNaN, isnan", isnan (x1)); \
178 if (SNAN_TESTS_TYPE_CAST) \
180 /* Upon type conversion, a sNaN is converted into a qNaN plus an */ \
181 /* INVALID exception (not checked here). */ \
182 check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") sNaN, !issignaling", \
183 !issignaling (x1)); \
186 x2 = (FLOAT) Inf_var; \
187 check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") Inf", isinf (x2) != 0); \
190 TEST_FUNC (float_test, float, f, FLT_EPSILON, HUGE_VALF)
191 TEST_FUNC (double_test, double, , DBL_EPSILON, HUGE_VAL)
192 TEST_CONVERT (convert_dfsf_test, float, double, )
193 TEST_CONVERT (convert_sfdf_test, double, float, f)
194 #ifndef NO_LONG_DOUBLE
195 TEST_FUNC (ldouble_test, long double, l, LDBL_EPSILON, HUGE_VALL)
196 TEST_CONVERT (convert_tfsf_test, float, long double, l)
197 TEST_CONVERT (convert_sftf_test, long double, float, f)
198 TEST_CONVERT (convert_tfdf_test, double, long double, l)
199 TEST_CONVERT (convert_dftf_test, long double, double, )
200 #endif
203 do_test (void)
205 float_test ();
206 double_test ();
207 convert_dfsf_test();
208 convert_sfdf_test();
210 #ifndef NO_LONG_DOUBLE
211 ldouble_test ();
212 convert_tfsf_test();
213 convert_sftf_test();
214 convert_tfdf_test();
215 convert_dftf_test();
216 #endif
218 return errors != 0;
221 #define TEST_FUNCTION do_test ()
222 #include "../test-skeleton.c"