blob: 456efa757cbf1eae634babe845811294bb36aec0 [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
Ryan Prichard1c3e4202025-01-15 16:57:27 -080031// Ensure that libc++'s complex.h and __fwd/complex.h headers are no-ops.
32#define _LIBCPP_COMPLEX_H
33#define _LIBCPP___FWD_COMPLEX_H
34
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080035// (libc++ also seems to have really bad implementations of its own that ignore
36// the intricacies of floating point math.)
37// http://llvm.org/bugs/show_bug.cgi?id=21504
38
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080039#include <math.h> // For M_PI_2/M_PI_2l.
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080040
Yi Kong43ccab82018-01-05 16:22:10 -080041// Prettify gtest Complex printing.
Zijuneae85ad2024-06-10 19:45:33 +000042// Macro 'complex' defined in complex.h conflicts with iostream.
43#pragma push_macro("complex")
44#undef complex
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080045#include <iostream>
Zijuneae85ad2024-06-10 19:45:33 +000046#pragma pop_macro("complex")
Yi Kong43ccab82018-01-05 16:22:10 -080047namespace testing {
48namespace internal {
49inline void PrintTo(const double _Complex& c, std::ostream* os) {
50 *os << "(" << creal(c) << "," << cimag(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080051}
Yi Kong43ccab82018-01-05 16:22:10 -080052inline void PrintTo(const float _Complex& c, std::ostream* os) {
53 *os << "(" << crealf(c) << "," << cimagf(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080054}
Yi Kong43ccab82018-01-05 16:22:10 -080055inline void PrintTo(const long double _Complex& c, std::ostream* os) {
56 *os << "(" << creall(c) << "," << cimagl(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080057}
Yi Kong43ccab82018-01-05 16:22:10 -080058}
59}
60
61// Macro 'I' defined in complex.h conflicts with gtest.h.
62#pragma push_macro("I")
63#undef I
64#include <gtest/gtest.h>
65#pragma pop_macro("I")
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080066
Elliott Hughesab2d3e12023-06-07 17:16:34 +000067TEST(complex_h, cabs) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080068 ASSERT_EQ(0.0, cabs(0));
69}
70
Elliott Hughesab2d3e12023-06-07 17:16:34 +000071TEST(complex_h, cabsf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080072 ASSERT_EQ(0.0, cabsf(0));
73}
74
Elliott Hughesab2d3e12023-06-07 17:16:34 +000075TEST(complex_h, cabsl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080076 ASSERT_EQ(0.0, cabsl(0));
77}
78
Elliott Hughesab2d3e12023-06-07 17:16:34 +000079TEST(complex_h, cacos) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080080 ASSERT_EQ(M_PI_2, cacos(0.0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080081}
82
Elliott Hughesab2d3e12023-06-07 17:16:34 +000083TEST(complex_h, cacosf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080084 ASSERT_EQ(static_cast<float>(M_PI_2), cacosf(0.0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080085}
86
Elliott Hughesab2d3e12023-06-07 17:16:34 +000087TEST(complex_h, cacosl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080088 ASSERT_EQ(M_PI_2l, cacosl(0.0));
89}
90
Elliott Hughesab2d3e12023-06-07 17:16:34 +000091TEST(complex_h, cacosh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080092 ASSERT_EQ(0.0, cacosh(1.0));
93}
94
Elliott Hughesab2d3e12023-06-07 17:16:34 +000095TEST(complex_h, cacoshl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080096 ASSERT_EQ(0.0, cacoshl(1.0));
97}
98
Elliott Hughesab2d3e12023-06-07 17:16:34 +000099TEST(complex_h, cacoshf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800100 ASSERT_EQ(0.0, cacoshf(1.0));
101}
102
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000103TEST(complex_h, carg) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800104 ASSERT_EQ(0.0, carg(0));
105}
106
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000107TEST(complex_h, cargf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800108 ASSERT_EQ(0.0, cargf(0));
109}
110
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000111TEST(complex_h, cargl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800112 ASSERT_EQ(0.0, cargl(0));
113}
114
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000115TEST(complex_h, casin) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800116 ASSERT_EQ(0.0, casin(0));
117}
118
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000119TEST(complex_h, casinf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800120 ASSERT_EQ(0.0, casinf(0));
121}
122
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000123TEST(complex_h, casinl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800124 ASSERT_EQ(0.0, casinl(0));
125}
126
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000127TEST(complex_h, casinh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800128 ASSERT_EQ(0.0, casinh(0));
129}
130
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000131TEST(complex_h, casinhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800132 ASSERT_EQ(0.0, casinhf(0));
133}
134
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000135TEST(complex_h, casinhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800136 ASSERT_EQ(0.0, casinhl(0));
137}
138
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000139TEST(complex_h, catan) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800140 ASSERT_EQ(0.0, catan(0));
141}
142
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000143TEST(complex_h, catanf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800144 ASSERT_EQ(0.0, catanf(0));
145}
146
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000147TEST(complex_h, catanl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800148 ASSERT_EQ(0.0, catanl(0));
149}
150
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000151TEST(complex_h, catanh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800152 ASSERT_EQ(0.0, catanh(0));
153}
154
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000155TEST(complex_h, catanhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800156 ASSERT_EQ(0.0, catanhf(0));
157}
158
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000159TEST(complex_h, catanhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800160 ASSERT_EQ(0.0, catanhl(0));
161}
162
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000163TEST(complex_h, ccos) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800164 ASSERT_EQ(1.0, ccos(0));
165}
166
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000167TEST(complex_h, ccosf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800168 ASSERT_EQ(1.0, ccosf(0));
169}
170
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000171TEST(complex_h, ccosl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800172 ASSERT_EQ(1.0, ccosl(0));
173}
174
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000175TEST(complex_h, ccosh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800176 ASSERT_EQ(1.0, ccosh(0));
177}
178
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000179TEST(complex_h, ccoshf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800180 ASSERT_EQ(1.0, ccoshf(0));
181}
182
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000183TEST(complex_h, ccoshl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800184 ASSERT_EQ(1.0, ccoshl(0));
185}
186
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000187TEST(complex_h, cexp) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800188 ASSERT_EQ(1.0, cexp(0));
189}
190
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000191TEST(complex_h, cexpf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800192 ASSERT_EQ(1.0, cexpf(0));
193}
194
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000195TEST(complex_h, cexpl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800196 ASSERT_EQ(1.0, cexpl(0));
197}
198
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000199TEST(complex_h, cimag) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800200 ASSERT_EQ(0.0, cimag(0));
201}
202
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000203TEST(complex_h, cimagf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800204 ASSERT_EQ(0.0f, cimagf(0));
205}
206
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000207TEST(complex_h, cimagl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800208 ASSERT_EQ(0.0, cimagl(0));
209}
210
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000211TEST(complex_h, clog) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800212 ASSERT_EQ(0.0, clog(1.0));
213}
214
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000215TEST(complex_h, clogf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800216 ASSERT_EQ(0.0f, clogf(1.0f));
217}
218
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000219TEST(complex_h, clogl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800220 ASSERT_EQ(0.0L, clogl(1.0L));
221}
222
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000223TEST(complex_h, conj) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800224 ASSERT_EQ(0.0, conj(0));
225}
226
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000227TEST(complex_h, conjf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800228 ASSERT_EQ(0.0f, conjf(0));
229}
230
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000231TEST(complex_h, conjl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800232 ASSERT_EQ(0.0, conjl(0));
233}
234
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000235TEST(complex_h, cpow) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800236 ASSERT_EQ(8.0, cpow(2.0, 3.0));
237}
238
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000239TEST(complex_h, cpowf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800240 ASSERT_EQ(8.0f, cpowf(2.0f, 3.0f));
241}
242
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000243TEST(complex_h, cpowl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800244 ASSERT_EQ(8.0L, cpowl(2.0L, 3.0L));
245}
246
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000247TEST(complex_h, cproj) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800248 ASSERT_EQ(0.0, cproj(0));
249}
250
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000251TEST(complex_h, cprojf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800252 ASSERT_EQ(0.0f, cprojf(0));
253}
254
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000255TEST(complex_h, cprojl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800256 ASSERT_EQ(0.0, cprojl(0));
257}
258
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000259TEST(complex_h, creal) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800260 ASSERT_EQ(2.0, creal(2.0 + 3.0I));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800261}
262
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000263TEST(complex_h, crealf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800264 ASSERT_EQ(2.0f, crealf(2.0f + 3.0fI));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800265}
266
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000267TEST(complex_h, creall) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800268 ASSERT_EQ(2.0, creall(2.0L + 3.0LI));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800269}
270
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000271TEST(complex_h, csin) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800272 ASSERT_EQ(0.0, csin(0));
273}
274
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000275TEST(complex_h, csinf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800276 ASSERT_EQ(0.0, csinf(0));
277}
278
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000279TEST(complex_h, csinl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800280 ASSERT_EQ(0.0, csinl(0));
281}
282
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000283TEST(complex_h, csinh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800284 ASSERT_EQ(0.0, csinh(0));
285}
286
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000287TEST(complex_h, csinhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800288 ASSERT_EQ(0.0, csinhf(0));
289}
290
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000291TEST(complex_h, csinhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800292 ASSERT_EQ(0.0, csinhl(0));
293}
294
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000295TEST(complex_h, csqrt) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800296 ASSERT_EQ(0.0, csqrt(0));
297}
298
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000299TEST(complex_h, csqrtf) {
Elliott Hughese9719f32016-09-26 09:35:04 -0700300 ASSERT_EQ(0.0f, csqrtf(0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800301}
302
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000303TEST(complex_h, csqrtl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800304 ASSERT_EQ(0.0, csqrtl(0));
305}
306
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000307TEST(complex_h, ctan) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800308 ASSERT_EQ(0.0, ctan(0));
309}
310
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000311TEST(complex_h, ctanf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800312 ASSERT_EQ(0.0, ctanf(0));
313}
314
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000315TEST(complex_h, ctanl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800316 ASSERT_EQ(0.0, ctanl(0));
317}
318
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000319TEST(complex_h, ctanh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800320 ASSERT_EQ(0.0, ctanh(0));
Elliott Hughes505ecd62018-07-23 14:25:36 -0700321
322 double complex z;
323
324 // If z is NaN+0i, the result is NaN+0i.
325 z = ctanh(nan("") + 0i);
326 ASSERT_TRUE(isnan(creal(z)));
327 ASSERT_EQ(0.0, cimag(z));
328
329 // If z is NaN+yi, the result is NaN+NaNi.
330 z = ctanh(nan("") + 2.0i);
331 ASSERT_TRUE(isnan(creal(z)));
332 ASSERT_TRUE(isnan(cimag(z)));
333
334 // If z is NaN+NaNi, the result is NaN+NaNi.
335 z = ctanh(nan("") + nan("") * I);
336 ASSERT_TRUE(isnan(creal(z)));
337 ASSERT_TRUE(isnan(cimag(z)));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800338}
339
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000340TEST(complex_h, ctanhf) {
Elliott Hughes505ecd62018-07-23 14:25:36 -0700341 ASSERT_EQ(0.0f, ctanhf(0.0f));
342
343 float complex z;
344
345 // If z is NaN+0i, the result is NaN+0i.
346 z = ctanhf(nanf("") + 0.0fi);
347 ASSERT_TRUE(isnan(crealf(z)));
348 ASSERT_EQ(0.0f, cimagf(z));
349
350 // If z is NaN+yi, the result is NaN+NaNi.
351 z = ctanhf(nanf("") + 2.0fi);
352 ASSERT_TRUE(isnan(crealf(z)));
353 ASSERT_TRUE(isnan(cimagf(z)));
354
355 // If z is NaN+NaNi, the result is NaN+NaNi.
356 z = ctanhf(nanf("") + nanf("") * I);
357 ASSERT_TRUE(isnan(crealf(z)));
358 ASSERT_TRUE(isnan(cimagf(z)));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800359}
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800360
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000361TEST(complex_h, ctanhl) {
Elliott Hughes505ecd62018-07-23 14:25:36 -0700362 ASSERT_EQ(0.0L, ctanhl(0.0L));
363
364 long double complex z;
365
366 // If z is NaN+0i, the result is NaN+0i.
367 z = ctanhl(nanl("") + 0.0Li);
368 ASSERT_TRUE(isnan(creall(z)));
369 // TODO: this case is currently broken in the netbsd ctanhl.
370 // ASSERT_EQ(0.0L, cimagl(z));
371
372 // If z is NaN+yi, the result is NaN+NaNi.
373 z = ctanhl(nanl("") + 2.0Li);
374 ASSERT_TRUE(isnan(creall(z)));
375 ASSERT_TRUE(isnan(cimagl(z)));
376
377 // If z is NaN+NaNi, the result is NaN+NaNi.
378 z = ctanhl(nanl("") + nanl("") * I);
379 ASSERT_TRUE(isnan(creall(z)));
380 ASSERT_TRUE(isnan(cimagl(z)));
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800381}