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