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