1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // This file is compiled against both glibc and bionic, and our complex.h
18 // depends on bionic-specific macros, so hack around that.
19 #include <sys/cdefs.h>
20 #if !defined(__INTRODUCED_IN)
21 #define __INTRODUCED_IN(x)
22 #endif
23
24 // libc++ actively gets in the way of including <complex.h> from C++, so we
25 // have to be naughty.
26 #include "../libc/include/complex.h"
27
28 // (libc++ also seems to have really bad implementations of its own that ignore
29 // the intricacies of floating point math.)
30 // http://llvm.org/bugs/show_bug.cgi?id=21504
31
32 #include <math.h> // For M_PI_2/M_PI_2l.
33
34 // Prettify gtest Complex printing.
35 // Macro 'complex' defined in complex.h conflicts with iostream.
36 #pragma push_macro("complex")
37 #undef complex
38 #include <iostream>
39 #pragma pop_macro("complex")
40 namespace testing {
41 namespace internal {
PrintTo(const double _Complex & c,std::ostream * os)42 inline void PrintTo(const double _Complex& c, std::ostream* os) {
43 *os << "(" << creal(c) << "," << cimag(c) << "i)";
44 }
PrintTo(const float _Complex & c,std::ostream * os)45 inline void PrintTo(const float _Complex& c, std::ostream* os) {
46 *os << "(" << crealf(c) << "," << cimagf(c) << "i)";
47 }
PrintTo(const long double _Complex & c,std::ostream * os)48 inline void PrintTo(const long double _Complex& c, std::ostream* os) {
49 *os << "(" << creall(c) << "," << cimagl(c) << "i)";
50 }
51 }
52 }
53
54 // Macro 'I' defined in complex.h conflicts with gtest.h.
55 #pragma push_macro("I")
56 #undef I
57 #include <gtest/gtest.h>
58 #pragma pop_macro("I")
59
TEST(complex_h,cabs)60 TEST(complex_h, cabs) {
61 ASSERT_EQ(0.0, cabs(0));
62 }
63
TEST(complex_h,cabsf)64 TEST(complex_h, cabsf) {
65 ASSERT_EQ(0.0, cabsf(0));
66 }
67
TEST(complex_h,cabsl)68 TEST(complex_h, cabsl) {
69 ASSERT_EQ(0.0, cabsl(0));
70 }
71
TEST(complex_h,cacos)72 TEST(complex_h, cacos) {
73 ASSERT_EQ(M_PI_2, cacos(0.0));
74 }
75
TEST(complex_h,cacosf)76 TEST(complex_h, cacosf) {
77 ASSERT_EQ(static_cast<float>(M_PI_2), cacosf(0.0));
78 }
79
TEST(complex_h,cacosl)80 TEST(complex_h, cacosl) {
81 ASSERT_EQ(M_PI_2l, cacosl(0.0));
82 }
83
TEST(complex_h,cacosh)84 TEST(complex_h, cacosh) {
85 ASSERT_EQ(0.0, cacosh(1.0));
86 }
87
TEST(complex_h,cacoshl)88 TEST(complex_h, cacoshl) {
89 ASSERT_EQ(0.0, cacoshl(1.0));
90 }
91
TEST(complex_h,cacoshf)92 TEST(complex_h, cacoshf) {
93 ASSERT_EQ(0.0, cacoshf(1.0));
94 }
95
TEST(complex_h,carg)96 TEST(complex_h, carg) {
97 ASSERT_EQ(0.0, carg(0));
98 }
99
TEST(complex_h,cargf)100 TEST(complex_h, cargf) {
101 ASSERT_EQ(0.0, cargf(0));
102 }
103
TEST(complex_h,cargl)104 TEST(complex_h, cargl) {
105 ASSERT_EQ(0.0, cargl(0));
106 }
107
TEST(complex_h,casin)108 TEST(complex_h, casin) {
109 ASSERT_EQ(0.0, casin(0));
110 }
111
TEST(complex_h,casinf)112 TEST(complex_h, casinf) {
113 ASSERT_EQ(0.0, casinf(0));
114 }
115
TEST(complex_h,casinl)116 TEST(complex_h, casinl) {
117 ASSERT_EQ(0.0, casinl(0));
118 }
119
TEST(complex_h,casinh)120 TEST(complex_h, casinh) {
121 ASSERT_EQ(0.0, casinh(0));
122 }
123
TEST(complex_h,casinhf)124 TEST(complex_h, casinhf) {
125 ASSERT_EQ(0.0, casinhf(0));
126 }
127
TEST(complex_h,casinhl)128 TEST(complex_h, casinhl) {
129 ASSERT_EQ(0.0, casinhl(0));
130 }
131
TEST(complex_h,catan)132 TEST(complex_h, catan) {
133 ASSERT_EQ(0.0, catan(0));
134 }
135
TEST(complex_h,catanf)136 TEST(complex_h, catanf) {
137 ASSERT_EQ(0.0, catanf(0));
138 }
139
TEST(complex_h,catanl)140 TEST(complex_h, catanl) {
141 ASSERT_EQ(0.0, catanl(0));
142 }
143
TEST(complex_h,catanh)144 TEST(complex_h, catanh) {
145 ASSERT_EQ(0.0, catanh(0));
146 }
147
TEST(complex_h,catanhf)148 TEST(complex_h, catanhf) {
149 ASSERT_EQ(0.0, catanhf(0));
150 }
151
TEST(complex_h,catanhl)152 TEST(complex_h, catanhl) {
153 ASSERT_EQ(0.0, catanhl(0));
154 }
155
TEST(complex_h,ccos)156 TEST(complex_h, ccos) {
157 ASSERT_EQ(1.0, ccos(0));
158 }
159
TEST(complex_h,ccosf)160 TEST(complex_h, ccosf) {
161 ASSERT_EQ(1.0, ccosf(0));
162 }
163
TEST(complex_h,ccosl)164 TEST(complex_h, ccosl) {
165 ASSERT_EQ(1.0, ccosl(0));
166 }
167
TEST(complex_h,ccosh)168 TEST(complex_h, ccosh) {
169 ASSERT_EQ(1.0, ccosh(0));
170 }
171
TEST(complex_h,ccoshf)172 TEST(complex_h, ccoshf) {
173 ASSERT_EQ(1.0, ccoshf(0));
174 }
175
TEST(complex_h,ccoshl)176 TEST(complex_h, ccoshl) {
177 ASSERT_EQ(1.0, ccoshl(0));
178 }
179
TEST(complex_h,cexp)180 TEST(complex_h, cexp) {
181 ASSERT_EQ(1.0, cexp(0));
182 }
183
TEST(complex_h,cexpf)184 TEST(complex_h, cexpf) {
185 ASSERT_EQ(1.0, cexpf(0));
186 }
187
TEST(complex_h,cexpl)188 TEST(complex_h, cexpl) {
189 ASSERT_EQ(1.0, cexpl(0));
190 }
191
TEST(complex_h,cimag)192 TEST(complex_h, cimag) {
193 ASSERT_EQ(0.0, cimag(0));
194 }
195
TEST(complex_h,cimagf)196 TEST(complex_h, cimagf) {
197 ASSERT_EQ(0.0f, cimagf(0));
198 }
199
TEST(complex_h,cimagl)200 TEST(complex_h, cimagl) {
201 ASSERT_EQ(0.0, cimagl(0));
202 }
203
TEST(complex_h,clog)204 TEST(complex_h, clog) {
205 ASSERT_EQ(0.0, clog(1.0));
206 }
207
TEST(complex_h,clogf)208 TEST(complex_h, clogf) {
209 ASSERT_EQ(0.0f, clogf(1.0f));
210 }
211
TEST(complex_h,clogl)212 TEST(complex_h, clogl) {
213 ASSERT_EQ(0.0L, clogl(1.0L));
214 }
215
TEST(complex_h,conj)216 TEST(complex_h, conj) {
217 ASSERT_EQ(0.0, conj(0));
218 }
219
TEST(complex_h,conjf)220 TEST(complex_h, conjf) {
221 ASSERT_EQ(0.0f, conjf(0));
222 }
223
TEST(complex_h,conjl)224 TEST(complex_h, conjl) {
225 ASSERT_EQ(0.0, conjl(0));
226 }
227
TEST(complex_h,cpow)228 TEST(complex_h, cpow) {
229 ASSERT_EQ(8.0, cpow(2.0, 3.0));
230 }
231
TEST(complex_h,cpowf)232 TEST(complex_h, cpowf) {
233 ASSERT_EQ(8.0f, cpowf(2.0f, 3.0f));
234 }
235
TEST(complex_h,cpowl)236 TEST(complex_h, cpowl) {
237 ASSERT_EQ(8.0L, cpowl(2.0L, 3.0L));
238 }
239
TEST(complex_h,cproj)240 TEST(complex_h, cproj) {
241 ASSERT_EQ(0.0, cproj(0));
242 }
243
TEST(complex_h,cprojf)244 TEST(complex_h, cprojf) {
245 ASSERT_EQ(0.0f, cprojf(0));
246 }
247
TEST(complex_h,cprojl)248 TEST(complex_h, cprojl) {
249 ASSERT_EQ(0.0, cprojl(0));
250 }
251
TEST(complex_h,creal)252 TEST(complex_h, creal) {
253 ASSERT_EQ(2.0, creal(2.0 + 3.0I));
254 }
255
TEST(complex_h,crealf)256 TEST(complex_h, crealf) {
257 ASSERT_EQ(2.0f, crealf(2.0f + 3.0fI));
258 }
259
TEST(complex_h,creall)260 TEST(complex_h, creall) {
261 ASSERT_EQ(2.0, creall(2.0L + 3.0LI));
262 }
263
TEST(complex_h,csin)264 TEST(complex_h, csin) {
265 ASSERT_EQ(0.0, csin(0));
266 }
267
TEST(complex_h,csinf)268 TEST(complex_h, csinf) {
269 ASSERT_EQ(0.0, csinf(0));
270 }
271
TEST(complex_h,csinl)272 TEST(complex_h, csinl) {
273 ASSERT_EQ(0.0, csinl(0));
274 }
275
TEST(complex_h,csinh)276 TEST(complex_h, csinh) {
277 ASSERT_EQ(0.0, csinh(0));
278 }
279
TEST(complex_h,csinhf)280 TEST(complex_h, csinhf) {
281 ASSERT_EQ(0.0, csinhf(0));
282 }
283
TEST(complex_h,csinhl)284 TEST(complex_h, csinhl) {
285 ASSERT_EQ(0.0, csinhl(0));
286 }
287
TEST(complex_h,csqrt)288 TEST(complex_h, csqrt) {
289 ASSERT_EQ(0.0, csqrt(0));
290 }
291
TEST(complex_h,csqrtf)292 TEST(complex_h, csqrtf) {
293 ASSERT_EQ(0.0f, csqrtf(0));
294 }
295
TEST(complex_h,csqrtl)296 TEST(complex_h, csqrtl) {
297 ASSERT_EQ(0.0, csqrtl(0));
298 }
299
TEST(complex_h,ctan)300 TEST(complex_h, ctan) {
301 ASSERT_EQ(0.0, ctan(0));
302 }
303
TEST(complex_h,ctanf)304 TEST(complex_h, ctanf) {
305 ASSERT_EQ(0.0, ctanf(0));
306 }
307
TEST(complex_h,ctanl)308 TEST(complex_h, ctanl) {
309 ASSERT_EQ(0.0, ctanl(0));
310 }
311
TEST(complex_h,ctanh)312 TEST(complex_h, ctanh) {
313 ASSERT_EQ(0.0, ctanh(0));
314
315 double complex z;
316
317 // If z is NaN+0i, the result is NaN+0i.
318 z = ctanh(nan("") + 0i);
319 ASSERT_TRUE(isnan(creal(z)));
320 ASSERT_EQ(0.0, cimag(z));
321
322 // If z is NaN+yi, the result is NaN+NaNi.
323 z = ctanh(nan("") + 2.0i);
324 ASSERT_TRUE(isnan(creal(z)));
325 ASSERT_TRUE(isnan(cimag(z)));
326
327 // If z is NaN+NaNi, the result is NaN+NaNi.
328 z = ctanh(nan("") + nan("") * I);
329 ASSERT_TRUE(isnan(creal(z)));
330 ASSERT_TRUE(isnan(cimag(z)));
331 }
332
TEST(complex_h,ctanhf)333 TEST(complex_h, ctanhf) {
334 ASSERT_EQ(0.0f, ctanhf(0.0f));
335
336 float complex z;
337
338 // If z is NaN+0i, the result is NaN+0i.
339 z = ctanhf(nanf("") + 0.0fi);
340 ASSERT_TRUE(isnan(crealf(z)));
341 ASSERT_EQ(0.0f, cimagf(z));
342
343 // If z is NaN+yi, the result is NaN+NaNi.
344 z = ctanhf(nanf("") + 2.0fi);
345 ASSERT_TRUE(isnan(crealf(z)));
346 ASSERT_TRUE(isnan(cimagf(z)));
347
348 // If z is NaN+NaNi, the result is NaN+NaNi.
349 z = ctanhf(nanf("") + nanf("") * I);
350 ASSERT_TRUE(isnan(crealf(z)));
351 ASSERT_TRUE(isnan(cimagf(z)));
352 }
353
TEST(complex_h,ctanhl)354 TEST(complex_h, ctanhl) {
355 ASSERT_EQ(0.0L, ctanhl(0.0L));
356
357 long double complex z;
358
359 // If z is NaN+0i, the result is NaN+0i.
360 z = ctanhl(nanl("") + 0.0Li);
361 ASSERT_TRUE(isnan(creall(z)));
362 // TODO: this case is currently broken in the netbsd ctanhl.
363 // ASSERT_EQ(0.0L, cimagl(z));
364
365 // If z is NaN+yi, the result is NaN+NaNi.
366 z = ctanhl(nanl("") + 2.0Li);
367 ASSERT_TRUE(isnan(creall(z)));
368 ASSERT_TRUE(isnan(cimagl(z)));
369
370 // If z is NaN+NaNi, the result is NaN+NaNi.
371 z = ctanhl(nanl("") + nanl("") * I);
372 ASSERT_TRUE(isnan(creall(z)));
373 ASSERT_TRUE(isnan(cimagl(z)));
374 }
375