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