blob: ed0109a2164e0df2deae88228787499ad83a66f5 [file] [log] [blame]
Elliott Hughesb8ee16f2014-11-06 11:16:55 -08001/*
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
Josh Gaoff504e62016-04-29 11:52:39 -070017// 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
Dan Albert5a7331a2024-10-29 21:47:37 +000023#if !defined(__BIONIC_AVAILABILITY_GUARD)
24#define __BIONIC_AVAILABILITY_GUARD(x) 1
25#endif
Josh Gaoff504e62016-04-29 11:52:39 -070026
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080027// libc++ actively gets in the way of including <complex.h> from C++, so we
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080028// have to be naughty.
Dan Albertb2ca0312017-08-04 15:37:39 -070029#include "../libc/include/complex.h"
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080030
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080031// (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
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080035#include <math.h> // For M_PI_2/M_PI_2l.
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080036
Yi Kong43ccab82018-01-05 16:22:10 -080037// Prettify gtest Complex printing.
Zijuneae85ad2024-06-10 19:45:33 +000038// Macro 'complex' defined in complex.h conflicts with iostream.
39#pragma push_macro("complex")
40#undef complex
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080041#include <iostream>
Zijuneae85ad2024-06-10 19:45:33 +000042#pragma pop_macro("complex")
Yi Kong43ccab82018-01-05 16:22:10 -080043namespace testing {
44namespace internal {
45inline void PrintTo(const double _Complex& c, std::ostream* os) {
46 *os << "(" << creal(c) << "," << cimag(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080047}
Yi Kong43ccab82018-01-05 16:22:10 -080048inline void PrintTo(const float _Complex& c, std::ostream* os) {
49 *os << "(" << crealf(c) << "," << cimagf(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080050}
Yi Kong43ccab82018-01-05 16:22:10 -080051inline void PrintTo(const long double _Complex& c, std::ostream* os) {
52 *os << "(" << creall(c) << "," << cimagl(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080053}
Yi Kong43ccab82018-01-05 16:22:10 -080054}
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")
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080062
Elliott Hughesab2d3e12023-06-07 17:16:34 +000063TEST(complex_h, cabs) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080064 ASSERT_EQ(0.0, cabs(0));
65}
66
Elliott Hughesab2d3e12023-06-07 17:16:34 +000067TEST(complex_h, cabsf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080068 ASSERT_EQ(0.0, cabsf(0));
69}
70
Elliott Hughesab2d3e12023-06-07 17:16:34 +000071TEST(complex_h, cabsl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080072 ASSERT_EQ(0.0, cabsl(0));
73}
74
Elliott Hughesab2d3e12023-06-07 17:16:34 +000075TEST(complex_h, cacos) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080076 ASSERT_EQ(M_PI_2, cacos(0.0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080077}
78
Elliott Hughesab2d3e12023-06-07 17:16:34 +000079TEST(complex_h, cacosf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080080 ASSERT_EQ(static_cast<float>(M_PI_2), cacosf(0.0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080081}
82
Elliott Hughesab2d3e12023-06-07 17:16:34 +000083TEST(complex_h, cacosl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080084 ASSERT_EQ(M_PI_2l, cacosl(0.0));
85}
86
Elliott Hughesab2d3e12023-06-07 17:16:34 +000087TEST(complex_h, cacosh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080088 ASSERT_EQ(0.0, cacosh(1.0));
89}
90
Elliott Hughesab2d3e12023-06-07 17:16:34 +000091TEST(complex_h, cacoshl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080092 ASSERT_EQ(0.0, cacoshl(1.0));
93}
94
Elliott Hughesab2d3e12023-06-07 17:16:34 +000095TEST(complex_h, cacoshf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080096 ASSERT_EQ(0.0, cacoshf(1.0));
97}
98
Elliott Hughesab2d3e12023-06-07 17:16:34 +000099TEST(complex_h, carg) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800100 ASSERT_EQ(0.0, carg(0));
101}
102
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000103TEST(complex_h, cargf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800104 ASSERT_EQ(0.0, cargf(0));
105}
106
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000107TEST(complex_h, cargl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800108 ASSERT_EQ(0.0, cargl(0));
109}
110
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000111TEST(complex_h, casin) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800112 ASSERT_EQ(0.0, casin(0));
113}
114
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000115TEST(complex_h, casinf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800116 ASSERT_EQ(0.0, casinf(0));
117}
118
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000119TEST(complex_h, casinl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800120 ASSERT_EQ(0.0, casinl(0));
121}
122
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000123TEST(complex_h, casinh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800124 ASSERT_EQ(0.0, casinh(0));
125}
126
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000127TEST(complex_h, casinhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800128 ASSERT_EQ(0.0, casinhf(0));
129}
130
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000131TEST(complex_h, casinhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800132 ASSERT_EQ(0.0, casinhl(0));
133}
134
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000135TEST(complex_h, catan) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800136 ASSERT_EQ(0.0, catan(0));
137}
138
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000139TEST(complex_h, catanf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800140 ASSERT_EQ(0.0, catanf(0));
141}
142
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000143TEST(complex_h, catanl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800144 ASSERT_EQ(0.0, catanl(0));
145}
146
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000147TEST(complex_h, catanh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800148 ASSERT_EQ(0.0, catanh(0));
149}
150
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000151TEST(complex_h, catanhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800152 ASSERT_EQ(0.0, catanhf(0));
153}
154
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000155TEST(complex_h, catanhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800156 ASSERT_EQ(0.0, catanhl(0));
157}
158
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000159TEST(complex_h, ccos) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800160 ASSERT_EQ(1.0, ccos(0));
161}
162
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000163TEST(complex_h, ccosf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800164 ASSERT_EQ(1.0, ccosf(0));
165}
166
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000167TEST(complex_h, ccosl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800168 ASSERT_EQ(1.0, ccosl(0));
169}
170
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000171TEST(complex_h, ccosh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800172 ASSERT_EQ(1.0, ccosh(0));
173}
174
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000175TEST(complex_h, ccoshf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800176 ASSERT_EQ(1.0, ccoshf(0));
177}
178
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000179TEST(complex_h, ccoshl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800180 ASSERT_EQ(1.0, ccoshl(0));
181}
182
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000183TEST(complex_h, cexp) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800184 ASSERT_EQ(1.0, cexp(0));
185}
186
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000187TEST(complex_h, cexpf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800188 ASSERT_EQ(1.0, cexpf(0));
189}
190
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000191TEST(complex_h, cexpl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800192 ASSERT_EQ(1.0, cexpl(0));
193}
194
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000195TEST(complex_h, cimag) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800196 ASSERT_EQ(0.0, cimag(0));
197}
198
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000199TEST(complex_h, cimagf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800200 ASSERT_EQ(0.0f, cimagf(0));
201}
202
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000203TEST(complex_h, cimagl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800204 ASSERT_EQ(0.0, cimagl(0));
205}
206
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000207TEST(complex_h, clog) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800208 ASSERT_EQ(0.0, clog(1.0));
209}
210
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000211TEST(complex_h, clogf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800212 ASSERT_EQ(0.0f, clogf(1.0f));
213}
214
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000215TEST(complex_h, clogl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800216 ASSERT_EQ(0.0L, clogl(1.0L));
217}
218
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000219TEST(complex_h, conj) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800220 ASSERT_EQ(0.0, conj(0));
221}
222
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000223TEST(complex_h, conjf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800224 ASSERT_EQ(0.0f, conjf(0));
225}
226
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000227TEST(complex_h, conjl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800228 ASSERT_EQ(0.0, conjl(0));
229}
230
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000231TEST(complex_h, cpow) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800232 ASSERT_EQ(8.0, cpow(2.0, 3.0));
233}
234
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000235TEST(complex_h, cpowf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800236 ASSERT_EQ(8.0f, cpowf(2.0f, 3.0f));
237}
238
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000239TEST(complex_h, cpowl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800240 ASSERT_EQ(8.0L, cpowl(2.0L, 3.0L));
241}
242
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000243TEST(complex_h, cproj) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800244 ASSERT_EQ(0.0, cproj(0));
245}
246
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000247TEST(complex_h, cprojf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800248 ASSERT_EQ(0.0f, cprojf(0));
249}
250
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000251TEST(complex_h, cprojl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800252 ASSERT_EQ(0.0, cprojl(0));
253}
254
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000255TEST(complex_h, creal) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800256 ASSERT_EQ(2.0, creal(2.0 + 3.0I));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800257}
258
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000259TEST(complex_h, crealf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800260 ASSERT_EQ(2.0f, crealf(2.0f + 3.0fI));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800261}
262
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000263TEST(complex_h, creall) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800264 ASSERT_EQ(2.0, creall(2.0L + 3.0LI));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800265}
266
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000267TEST(complex_h, csin) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800268 ASSERT_EQ(0.0, csin(0));
269}
270
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000271TEST(complex_h, csinf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800272 ASSERT_EQ(0.0, csinf(0));
273}
274
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000275TEST(complex_h, csinl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800276 ASSERT_EQ(0.0, csinl(0));
277}
278
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000279TEST(complex_h, csinh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800280 ASSERT_EQ(0.0, csinh(0));
281}
282
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000283TEST(complex_h, csinhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800284 ASSERT_EQ(0.0, csinhf(0));
285}
286
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000287TEST(complex_h, csinhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800288 ASSERT_EQ(0.0, csinhl(0));
289}
290
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000291TEST(complex_h, csqrt) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800292 ASSERT_EQ(0.0, csqrt(0));
293}
294
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000295TEST(complex_h, csqrtf) {
Elliott Hughese9719f32016-09-26 09:35:04 -0700296 ASSERT_EQ(0.0f, csqrtf(0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800297}
298
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000299TEST(complex_h, csqrtl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800300 ASSERT_EQ(0.0, csqrtl(0));
301}
302
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000303TEST(complex_h, ctan) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800304 ASSERT_EQ(0.0, ctan(0));
305}
306
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000307TEST(complex_h, ctanf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800308 ASSERT_EQ(0.0, ctanf(0));
309}
310
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000311TEST(complex_h, ctanl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800312 ASSERT_EQ(0.0, ctanl(0));
313}
314
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000315TEST(complex_h, ctanh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800316 ASSERT_EQ(0.0, ctanh(0));
Elliott Hughes505ecd62018-07-23 14:25:36 -0700317
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)));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800334}
335
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000336TEST(complex_h, ctanhf) {
Elliott Hughes505ecd62018-07-23 14:25:36 -0700337 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)));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800355}
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800356
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000357TEST(complex_h, ctanhl) {
Elliott Hughes505ecd62018-07-23 14:25:36 -0700358 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)));
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800377}