1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <complex>
11 
12 // test cases
13 
14 #ifndef CASES_H
15 #define CASES_H
16 
17 #include <complex>
18 #include <cassert>
19 
20 std::complex<double> x[] =
21 {
22     std::complex<double>( 1.e-6,  1.e-6),
23     std::complex<double>(-1.e-6,  1.e-6),
24     std::complex<double>(-1.e-6, -1.e-6),
25     std::complex<double>( 1.e-6, -1.e-6),
26 
27     std::complex<double>( 1.e+6,  1.e-6),
28     std::complex<double>(-1.e+6,  1.e-6),
29     std::complex<double>(-1.e+6, -1.e-6),
30     std::complex<double>( 1.e+6, -1.e-6),
31 
32     std::complex<double>( 1.e-6,  1.e+6),
33     std::complex<double>(-1.e-6,  1.e+6),
34     std::complex<double>(-1.e-6, -1.e+6),
35     std::complex<double>( 1.e-6, -1.e+6),
36 
37     std::complex<double>( 1.e+6,  1.e+6),
38     std::complex<double>(-1.e+6,  1.e+6),
39     std::complex<double>(-1.e+6, -1.e+6),
40     std::complex<double>( 1.e+6, -1.e+6),
41 
42     std::complex<double>(NAN, NAN),
43     std::complex<double>(-INFINITY, NAN),
44     std::complex<double>(-2, NAN),
45     std::complex<double>(-1, NAN),
46     std::complex<double>(-0.5, NAN),
47     std::complex<double>(-0., NAN),
48     std::complex<double>(+0., NAN),
49     std::complex<double>(0.5, NAN),
50     std::complex<double>(1, NAN),
51     std::complex<double>(2, NAN),
52     std::complex<double>(INFINITY, NAN),
53 
54     std::complex<double>(NAN, -INFINITY),
55     std::complex<double>(-INFINITY, -INFINITY),
56     std::complex<double>(-2, -INFINITY),
57     std::complex<double>(-1, -INFINITY),
58     std::complex<double>(-0.5, -INFINITY),
59     std::complex<double>(-0., -INFINITY),
60     std::complex<double>(+0., -INFINITY),
61     std::complex<double>(0.5, -INFINITY),
62     std::complex<double>(1, -INFINITY),
63     std::complex<double>(2, -INFINITY),
64     std::complex<double>(INFINITY, -INFINITY),
65 
66     std::complex<double>(NAN, -2),
67     std::complex<double>(-INFINITY, -2),
68     std::complex<double>(-2, -2),
69     std::complex<double>(-1, -2),
70     std::complex<double>(-0.5, -2),
71     std::complex<double>(-0., -2),
72     std::complex<double>(+0., -2),
73     std::complex<double>(0.5, -2),
74     std::complex<double>(1, -2),
75     std::complex<double>(2, -2),
76     std::complex<double>(INFINITY, -2),
77 
78     std::complex<double>(NAN, -1),
79     std::complex<double>(-INFINITY, -1),
80     std::complex<double>(-2, -1),
81     std::complex<double>(-1, -1),
82     std::complex<double>(-0.5, -1),
83     std::complex<double>(-0., -1),
84     std::complex<double>(+0., -1),
85     std::complex<double>(0.5, -1),
86     std::complex<double>(1, -1),
87     std::complex<double>(2, -1),
88     std::complex<double>(INFINITY, -1),
89 
90     std::complex<double>(NAN, -0.5),
91     std::complex<double>(-INFINITY, -0.5),
92     std::complex<double>(-2, -0.5),
93     std::complex<double>(-1, -0.5),
94     std::complex<double>(-0.5, -0.5),
95     std::complex<double>(-0., -0.5),
96     std::complex<double>(+0., -0.5),
97     std::complex<double>(0.5, -0.5),
98     std::complex<double>(1, -0.5),
99     std::complex<double>(2, -0.5),
100     std::complex<double>(INFINITY, -0.5),
101 
102     std::complex<double>(NAN, -0.),
103     std::complex<double>(-INFINITY, -0.),
104     std::complex<double>(-2, -0.),
105     std::complex<double>(-1, -0.),
106     std::complex<double>(-0.5, -0.),
107     std::complex<double>(-0., -0.),
108     std::complex<double>(+0., -0.),
109     std::complex<double>(0.5, -0.),
110     std::complex<double>(1, -0.),
111     std::complex<double>(2, -0.),
112     std::complex<double>(INFINITY, -0.),
113 
114     std::complex<double>(NAN, +0.),
115     std::complex<double>(-INFINITY, +0.),
116     std::complex<double>(-2, +0.),
117     std::complex<double>(-1, +0.),
118     std::complex<double>(-0.5, +0.),
119     std::complex<double>(-0., +0.),
120     std::complex<double>(+0., +0.),
121     std::complex<double>(0.5, +0.),
122     std::complex<double>(1, +0.),
123     std::complex<double>(2, +0.),
124     std::complex<double>(INFINITY, +0.),
125 
126     std::complex<double>(NAN, 0.5),
127     std::complex<double>(-INFINITY, 0.5),
128     std::complex<double>(-2, 0.5),
129     std::complex<double>(-1, 0.5),
130     std::complex<double>(-0.5, 0.5),
131     std::complex<double>(-0., 0.5),
132     std::complex<double>(+0., 0.5),
133     std::complex<double>(0.5, 0.5),
134     std::complex<double>(1, 0.5),
135     std::complex<double>(2, 0.5),
136     std::complex<double>(INFINITY, 0.5),
137 
138     std::complex<double>(NAN, 1),
139     std::complex<double>(-INFINITY, 1),
140     std::complex<double>(-2, 1),
141     std::complex<double>(-1, 1),
142     std::complex<double>(-0.5, 1),
143     std::complex<double>(-0., 1),
144     std::complex<double>(+0., 1),
145     std::complex<double>(0.5, 1),
146     std::complex<double>(1, 1),
147     std::complex<double>(2, 1),
148     std::complex<double>(INFINITY, 1),
149 
150     std::complex<double>(NAN, 2),
151     std::complex<double>(-INFINITY, 2),
152     std::complex<double>(-2, 2),
153     std::complex<double>(-1, 2),
154     std::complex<double>(-0.5, 2),
155     std::complex<double>(-0., 2),
156     std::complex<double>(+0., 2),
157     std::complex<double>(0.5, 2),
158     std::complex<double>(1, 2),
159     std::complex<double>(2, 2),
160     std::complex<double>(INFINITY, 2),
161 
162     std::complex<double>(NAN, INFINITY),
163     std::complex<double>(-INFINITY, INFINITY),
164     std::complex<double>(-2, INFINITY),
165     std::complex<double>(-1, INFINITY),
166     std::complex<double>(-0.5, INFINITY),
167     std::complex<double>(-0., INFINITY),
168     std::complex<double>(+0., INFINITY),
169     std::complex<double>(0.5, INFINITY),
170     std::complex<double>(1, INFINITY),
171     std::complex<double>(2, INFINITY),
172     std::complex<double>(INFINITY, INFINITY)
173 };
174 
175 enum {zero, non_zero, inf, NaN, non_zero_nan};
176 
177 template <class T>
178 int
classify(const std::complex<T> & x)179 classify(const std::complex<T>& x)
180 {
181     if (x == std::complex<T>())
182         return zero;
183     if (std::isinf(x.real()) || std::isinf(x.imag()))
184         return inf;
185     if (std::isnan(x.real()) && std::isnan(x.imag()))
186         return NaN;
187     if (std::isnan(x.real()))
188     {
189         if (x.imag() == T(0))
190             return NaN;
191         return non_zero_nan;
192     }
193     if (std::isnan(x.imag()))
194     {
195         if (x.real() == T(0))
196             return NaN;
197         return non_zero_nan;
198     }
199     return non_zero;
200 }
201 
202 inline
203 int
classify(double x)204 classify(double x)
205 {
206     if (x == 0)
207         return zero;
208     if (std::isinf(x))
209         return inf;
210     if (std::isnan(x))
211         return NaN;
212     return non_zero;
213 }
214 
is_about(float x,float y)215 void is_about(float x, float y)
216 {
217     assert(std::abs((x-y)/(x+y)) < 1.e-6);
218 }
219 
is_about(double x,double y)220 void is_about(double x, double y)
221 {
222     assert(std::abs((x-y)/(x+y)) < 1.e-14);
223 }
224 
is_about(long double x,long double y)225 void is_about(long double x, long double y)
226 {
227     assert(std::abs((x-y)/(x+y)) < 1.e-14);
228 }
229 
230 #endif  // CASES_H
231