aboutsummaryrefslogtreecommitdiff
path: root/math/basic-test.c
blob: 44145a75f618e9356a248599582883e24d9cd6ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* Copyright (C) 1999-2013 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Andreas Jaeger <aj@suse.de>, 1999.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <math.h>
#include <float.h>
#include <stdio.h>

#include <math-tests.h>


static int errors = 0;


static void
check (const char *testname, int result)
{
  if (!result) {
    printf ("Failure: %s\n", testname);
    errors++;
  }
}

#define TEST_FUNC(NAME, FLOAT, SUFFIX, EPSILON, HUGEVAL)		      \
static void								      \
NAME (void)								      \
{									      \
  /* Variables are declared volatile to forbid some compiler		      \
     optimizations.  */							      \
  volatile FLOAT Inf_var, qNaN_var, zero_var, one_var;			      \
  /* A sNaN is only guaranteed to be representable in variables with */	      \
  /* static (or thread-local) storage duration.  */			      \
  static volatile FLOAT sNaN_var = __builtin_nans ## SUFFIX ("");	      \
  static volatile FLOAT minus_sNaN_var = -__builtin_nans ## SUFFIX ("");      \
  FLOAT x1, x2;								      \
									      \
  zero_var = 0.0;							      \
  one_var = 1.0;							      \
  qNaN_var = __builtin_nan ## SUFFIX ("");				      \
  Inf_var = one_var / zero_var;						      \
									      \
  (void) &zero_var;							      \
  (void) &one_var;							      \
  (void) &qNaN_var;							      \
  (void) &sNaN_var;							      \
  (void) &minus_sNaN_var;						      \
  (void) &Inf_var;							      \
									      \
									      \
  check (#FLOAT " isinf (inf) == 1", isinf (Inf_var) == 1);		      \
  check (#FLOAT " isinf (-inf) == -1", isinf (-Inf_var) == -1);		      \
  check (#FLOAT " !isinf (1)", !(isinf (one_var)));			      \
  check (#FLOAT " !isinf (qNaN)", !(isinf (qNaN_var)));			      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " !isinf (sNaN)", !(isinf (sNaN_var)));		      \
									      \
  check (#FLOAT " isnan (qNaN)", isnan (qNaN_var));			      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " isnan (sNaN)", isnan (sNaN_var));			      \
  check (#FLOAT " isnan (-qNaN)", isnan (-qNaN_var));			      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " isnan (-sNaN)", isnan (minus_sNaN_var));		      \
  check (#FLOAT " !isnan (1)", !(isnan (one_var)));			      \
  check (#FLOAT " !isnan (inf)", !(isnan (Inf_var)));			      \
									      \
  check (#FLOAT " !issignaling (qNaN)", !(issignaling (qNaN_var)));	      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " issignaling (sNaN)", issignaling (sNaN_var));	      \
  check (#FLOAT " !issignaling (-qNaN)", !(issignaling (-qNaN_var)));	      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " issignaling (-sNaN)", issignaling (minus_sNaN_var));      \
  check (#FLOAT " !issignaling (1)", !(issignaling (one_var)));		      \
  check (#FLOAT " !issignaling (inf)", !(issignaling (Inf_var)));	      \
									      \
  check (#FLOAT " inf == inf", Inf_var == Inf_var);			      \
  check (#FLOAT " -inf == -inf", -Inf_var == -Inf_var);			      \
  check (#FLOAT " inf != -inf", Inf_var != -Inf_var);			      \
  check (#FLOAT " qNaN != qNaN", qNaN_var != qNaN_var);			      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " sNaN != sNaN", sNaN_var != sNaN_var);		      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " qNaN != sNaN", qNaN_var != sNaN_var);		      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " -sNaN != -sNaN", minus_sNaN_var != minus_sNaN_var);	      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " sNaN != -sNaN", sNaN_var != minus_sNaN_var);	      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " qNaN != -sNaN", qNaN_var != minus_sNaN_var);	      \
									      \
  /*									      \
     the same tests but this time with NAN from <bits/nan.h>		      \
     NAN is a double const						      \
   */									      \
  check (#FLOAT " isnan (NAN)", isnan (NAN));				      \
  check (#FLOAT " isnan (-NAN)", isnan (-NAN));				      \
  check (#FLOAT " !isinf (NAN)", !(isinf (NAN)));			      \
  check (#FLOAT " !isinf (-NAN)", !(isinf (-NAN)));			      \
  check (#FLOAT " NAN != NAN", NAN != NAN);				      \
  check (#FLOAT " NAN != qNaN", NAN != qNaN_var);			      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " NAN != sNaN", NAN != sNaN_var);			      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " NAN != -sNaN", NAN != minus_sNaN_var);		      \
									      \
  /*									      \
     And again with the value returned by the `nan' function.		      \
   */									      \
  check (#FLOAT " isnan (nan (\"\"))", isnan (nan ## SUFFIX ("")));	      \
  check (#FLOAT " isnan (-nan (\"\"))", isnan (-nan ## SUFFIX ("")));	      \
  check (#FLOAT " !isinf (nan (\"\"))", !(isinf (nan ## SUFFIX (""))));	      \
  check (#FLOAT " !isinf (-nan (\"\"))", !(isinf (-nan ## SUFFIX (""))));     \
  check (#FLOAT " nan (\"\") != nan (\"\")",				      \
	 nan ## SUFFIX ("") != nan ## SUFFIX (""));			      \
  check (#FLOAT " nan (\"\") != qNaN", nan ## SUFFIX ("") != qNaN_var);	      \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " nan (\"\") != sNaN", nan ## SUFFIX ("") != sNaN_var);     \
  if (SNAN_TESTS (FLOAT))						      \
    check (#FLOAT " nan (\"\") != -sNaN",				      \
	   nan ## SUFFIX ("") != minus_sNaN_var);			      \
									      \
  /* test if EPSILON is ok */						      \
  x1 = 1.0;								      \
  x2 = x1 + EPSILON;							      \
  check (#FLOAT " 1 != 1+EPSILON", x1 != x2);				      \
									      \
  x1 = 1.0;								      \
  x2 = x1 - EPSILON;							      \
  check (#FLOAT " 1 != 1-EPSILON", x1 != x2);				      \
									      \
  /* test if HUGE_VALx is ok */						      \
  x1 = HUGEVAL;								      \
  check (#FLOAT " isinf (HUGE_VALx) == +1", isinf (x1) == +1);		      \
  x1 = - HUGEVAL;							      \
  check (#FLOAT " isinf (-HUGE_VALx) == -1", isinf (x1) == -1);		      \
}

#define TEST_CONVERT(NAME, FLOAT, DOUBLE, SUFFIX)			      \
void									      \
NAME (void)								      \
{									      \
  volatile DOUBLE Inf_var, qNaN_var, zero_var, one_var;			      \
  /* A sNaN is only guaranteed to be representable in variables with */	      \
  /* static (or thread-local) storage duration.  */			      \
  static volatile DOUBLE sNaN_var = __builtin_nans ## SUFFIX ("");	      \
  FLOAT x1, x2;								      \
									      \
  zero_var = 0.0;							      \
  one_var = 1.0;							      \
  qNaN_var = __builtin_nan ## SUFFIX ("");				      \
  Inf_var = one_var / zero_var;						      \
									      \
  (void) &qNaN_var;							      \
  (void) &sNaN_var;							      \
  (void) &Inf_var;							      \
									      \
  x1 = (FLOAT) qNaN_var;						      \
  check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") qNaN, isnan", isnan (x1));     \
  check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") qNaN, !issignaling",	      \
	 !issignaling (x1));						      \
  if (SNAN_TESTS (FLOAT))						      \
    {									      \
      x1 = (FLOAT) sNaN_var;						      \
      check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") sNaN, isnan", isnan (x1)); \
      if (SNAN_TESTS_TYPE_CAST)						      \
	{								      \
	  /* Upon type conversion, a sNaN is converted into a qNaN plus an */ \
	  /* INVALID exception (not checked here).  */			      \
	  check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") sNaN, !issignaling",   \
		 !issignaling (x1));					      \
	}								      \
      }									      \
  x2 = (FLOAT) Inf_var;							      \
  check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") Inf", isinf (x2) != 0);	      \
}

TEST_FUNC (float_test, float, f, FLT_EPSILON, HUGE_VALF)
TEST_FUNC (double_test, double, , DBL_EPSILON, HUGE_VAL)
TEST_CONVERT (convert_dfsf_test, float, double, )
TEST_CONVERT (convert_sfdf_test, double, float, f)
#ifndef NO_LONG_DOUBLE
TEST_FUNC (ldouble_test, long double, l, LDBL_EPSILON, HUGE_VALL)
TEST_CONVERT (convert_tfsf_test, float, long double, l)
TEST_CONVERT (convert_sftf_test, long double, float, f)
TEST_CONVERT (convert_tfdf_test, double, long double, l)
TEST_CONVERT (convert_dftf_test, long double, double, )
#endif

int
do_test (void)
{
  float_test ();
  double_test ();
  convert_dfsf_test();
  convert_sfdf_test();

#ifndef NO_LONG_DOUBLE
  ldouble_test ();
  convert_tfsf_test();
  convert_sftf_test();
  convert_tfdf_test();
  convert_dftf_test();
#endif

  return errors != 0;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"