summaryrefslogtreecommitdiff
path: root/test/math/basic-test.c
blob: d073abb0fb3637c4d04623866ac139429982187c (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
/* Copyright (C) 1999 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/>.  */

#define _ISOC99_SOURCE

#include <math.h>
#include <float.h>
#include <stdio.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, NANFUNC, EPSILON, HUGEVAL) \
static void								      \
NAME (void)								      \
{									      \
  /* Variables are declared volatile to forbid some compiler		      \
     optimizations.  */							      \
  volatile FLOAT Inf_var, NaN_var, zero_var, one_var;			      \
  FLOAT x1, x2;								      \
									      \
  zero_var = 0.0;							      \
  one_var = 1.0;							      \
  NaN_var = zero_var/zero_var;						      \
  Inf_var = one_var / zero_var;						      \
									      \
  (void) &zero_var;							      \
  (void) &one_var;							      \
  (void) &NaN_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 (NaN)", !(isinf (NaN_var)));			      \
									      \
  check (#FLOAT " isnan (NaN)", isnan (NaN_var));			      \
  check (#FLOAT " isnan (-NaN)", isnan (-NaN_var));			      \
  check (#FLOAT " !isnan (1)", !(isnan (one_var)));			      \
  check (#FLOAT " !isnan (inf)", !(isnan (Inf_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)));			      \
									      \
  /*									      \
     And again with the value returned by the `nan' function.		      \
   */									      \
  check (#FLOAT " isnan (NAN)", isnan (NANFUNC ("")));			      \
  check (#FLOAT " isnan (-NAN)", isnan (-NANFUNC ("")));		      \
  check (#FLOAT " !isinf (NAN)", !(isinf (NANFUNC (""))));		      \
  check (#FLOAT " !isinf (-NAN)", !(isinf (-NANFUNC (""))));		      \
  check (#FLOAT " NAN != NAN", NANFUNC ("") != NANFUNC (""));		      \
									      \
  /* 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);		      \
}
#ifndef DO_C99_MATH
# undef TEST_FUNC
# define TEST_FUNC(NAME, FLOAT, NANFUNC, EPSILON, HUGEVAL) \
static void								      \
NAME(void)								      \
{ /* nothing */ }
#endif

#define TEST_VAL(NAME, FLOAT, NANFUNC, EPSILON, HUGEVAL) \
static void								      \
NAME (void)								      \
{									      \
  /* Variables are declared volatile to forbid some compiler		      \
     optimizations.  */							      \
  volatile FLOAT Inf_var, NaN_var, zero_var, one_var;			      \
  FLOAT x1, x2;								      \
									      \
  zero_var = 0.0;							      \
  one_var = 1.0;							      \
  NaN_var = zero_var/zero_var;						      \
  Inf_var = one_var / zero_var;						      \
									      \
  (void) &zero_var;							      \
  (void) &one_var;							      \
  (void) &NaN_var;							      \
  (void) &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 " NaN != NaN", NaN_var != NaN_var);			      \
									      \
  check (#FLOAT " NAN != NAN", NAN != NAN);				      \
									      \
									      \
  /* 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_VAL (float_test_value, float, nanf, FLT_EPSILON, HUGE_VALF)
TEST_FUNC (float_test_call, float, nanf, FLT_EPSILON, HUGE_VALF)
TEST_VAL (double_test_value, double, nan, DBL_EPSILON, HUGE_VAL)
TEST_FUNC (double_test_call, double, nan, DBL_EPSILON, HUGE_VAL)
#ifndef NO_LONG_DOUBLE
TEST_VAL (ldouble_test_value, long double, nanl, LDBL_EPSILON, HUGE_VALL)
TEST_FUNC (ldouble_test_call, long double, nanl, LDBL_EPSILON, HUGE_VALL)
#endif

int
main (void)
{
  float_test_value ();
  float_test_call ();
  double_test_value ();
  double_test_call ();

#ifndef NO_LONG_DOUBLE
  ldouble_test_value ();
  ldouble_test_call ();
#endif

  return errors != 0;
}