blob: 8fdb2b2e10300e437ca01b386dc78b47586bf0df [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
23
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080024// libc++ actively gets in the way of including <complex.h> from C++, so we
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080025// have to be naughty.
Dan Albertb2ca0312017-08-04 15:37:39 -070026#include "../libc/include/complex.h"
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080027
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080028// (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
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080032#include <math.h> // For M_PI_2/M_PI_2l.
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080033
Yi Kong43ccab82018-01-05 16:22:10 -080034// Prettify gtest Complex printing.
Zijuneae85ad2024-06-10 19:45:33 +000035// Macro 'complex' defined in complex.h conflicts with iostream.
36#pragma push_macro("complex")
37#undef complex
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080038#include <iostream>
Zijuneae85ad2024-06-10 19:45:33 +000039#pragma pop_macro("complex")
Yi Kong43ccab82018-01-05 16:22:10 -080040namespace testing {
41namespace internal {
42inline void PrintTo(const double _Complex& c, std::ostream* os) {
43 *os << "(" << creal(c) << "," << cimag(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080044}
Yi Kong43ccab82018-01-05 16:22:10 -080045inline void PrintTo(const float _Complex& c, std::ostream* os) {
46 *os << "(" << crealf(c) << "," << cimagf(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080047}
Yi Kong43ccab82018-01-05 16:22:10 -080048inline void PrintTo(const long double _Complex& c, std::ostream* os) {
49 *os << "(" << creall(c) << "," << cimagl(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080050}
Yi Kong43ccab82018-01-05 16:22:10 -080051}
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")
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080059
Elliott Hughesab2d3e12023-06-07 17:16:34 +000060TEST(complex_h, cabs) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080061 ASSERT_EQ(0.0, cabs(0));
62}
63
Elliott Hughesab2d3e12023-06-07 17:16:34 +000064TEST(complex_h, cabsf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080065 ASSERT_EQ(0.0, cabsf(0));
66}
67
Elliott Hughesab2d3e12023-06-07 17:16:34 +000068TEST(complex_h, cabsl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080069 ASSERT_EQ(0.0, cabsl(0));
70}
71
Elliott Hughesab2d3e12023-06-07 17:16:34 +000072TEST(complex_h, cacos) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080073 ASSERT_EQ(M_PI_2, cacos(0.0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080074}
75
Elliott Hughesab2d3e12023-06-07 17:16:34 +000076TEST(complex_h, cacosf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080077 ASSERT_EQ(static_cast<float>(M_PI_2), cacosf(0.0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080078}
79
Elliott Hughesab2d3e12023-06-07 17:16:34 +000080TEST(complex_h, cacosl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080081 ASSERT_EQ(M_PI_2l, cacosl(0.0));
82}
83
Elliott Hughesab2d3e12023-06-07 17:16:34 +000084TEST(complex_h, cacosh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080085 ASSERT_EQ(0.0, cacosh(1.0));
86}
87
Elliott Hughesab2d3e12023-06-07 17:16:34 +000088TEST(complex_h, cacoshl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080089 ASSERT_EQ(0.0, cacoshl(1.0));
90}
91
Elliott Hughesab2d3e12023-06-07 17:16:34 +000092TEST(complex_h, cacoshf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080093 ASSERT_EQ(0.0, cacoshf(1.0));
94}
95
Elliott Hughesab2d3e12023-06-07 17:16:34 +000096TEST(complex_h, carg) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080097 ASSERT_EQ(0.0, carg(0));
98}
99
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000100TEST(complex_h, cargf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800101 ASSERT_EQ(0.0, cargf(0));
102}
103
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000104TEST(complex_h, cargl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800105 ASSERT_EQ(0.0, cargl(0));
106}
107
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000108TEST(complex_h, casin) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800109 ASSERT_EQ(0.0, casin(0));
110}
111
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000112TEST(complex_h, casinf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800113 ASSERT_EQ(0.0, casinf(0));
114}
115
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000116TEST(complex_h, casinl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800117 ASSERT_EQ(0.0, casinl(0));
118}
119
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000120TEST(complex_h, casinh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800121 ASSERT_EQ(0.0, casinh(0));
122}
123
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000124TEST(complex_h, casinhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800125 ASSERT_EQ(0.0, casinhf(0));
126}
127
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000128TEST(complex_h, casinhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800129 ASSERT_EQ(0.0, casinhl(0));
130}
131
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000132TEST(complex_h, catan) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800133 ASSERT_EQ(0.0, catan(0));
134}
135
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000136TEST(complex_h, catanf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800137 ASSERT_EQ(0.0, catanf(0));
138}
139
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000140TEST(complex_h, catanl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800141 ASSERT_EQ(0.0, catanl(0));
142}
143
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000144TEST(complex_h, catanh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800145 ASSERT_EQ(0.0, catanh(0));
146}
147
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000148TEST(complex_h, catanhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800149 ASSERT_EQ(0.0, catanhf(0));
150}
151
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000152TEST(complex_h, catanhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800153 ASSERT_EQ(0.0, catanhl(0));
154}
155
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000156TEST(complex_h, ccos) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800157 ASSERT_EQ(1.0, ccos(0));
158}
159
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000160TEST(complex_h, ccosf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800161 ASSERT_EQ(1.0, ccosf(0));
162}
163
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000164TEST(complex_h, ccosl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800165 ASSERT_EQ(1.0, ccosl(0));
166}
167
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000168TEST(complex_h, ccosh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800169 ASSERT_EQ(1.0, ccosh(0));
170}
171
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000172TEST(complex_h, ccoshf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800173 ASSERT_EQ(1.0, ccoshf(0));
174}
175
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000176TEST(complex_h, ccoshl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800177 ASSERT_EQ(1.0, ccoshl(0));
178}
179
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000180TEST(complex_h, cexp) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800181 ASSERT_EQ(1.0, cexp(0));
182}
183
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000184TEST(complex_h, cexpf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800185 ASSERT_EQ(1.0, cexpf(0));
186}
187
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000188TEST(complex_h, cexpl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800189 ASSERT_EQ(1.0, cexpl(0));
190}
191
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000192TEST(complex_h, cimag) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800193 ASSERT_EQ(0.0, cimag(0));
194}
195
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000196TEST(complex_h, cimagf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800197 ASSERT_EQ(0.0f, cimagf(0));
198}
199
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000200TEST(complex_h, cimagl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800201 ASSERT_EQ(0.0, cimagl(0));
202}
203
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000204TEST(complex_h, clog) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800205 ASSERT_EQ(0.0, clog(1.0));
206}
207
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000208TEST(complex_h, clogf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800209 ASSERT_EQ(0.0f, clogf(1.0f));
210}
211
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000212TEST(complex_h, clogl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800213 ASSERT_EQ(0.0L, clogl(1.0L));
214}
215
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000216TEST(complex_h, conj) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800217 ASSERT_EQ(0.0, conj(0));
218}
219
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000220TEST(complex_h, conjf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800221 ASSERT_EQ(0.0f, conjf(0));
222}
223
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000224TEST(complex_h, conjl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800225 ASSERT_EQ(0.0, conjl(0));
226}
227
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000228TEST(complex_h, cpow) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800229 ASSERT_EQ(8.0, cpow(2.0, 3.0));
230}
231
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000232TEST(complex_h, cpowf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800233 ASSERT_EQ(8.0f, cpowf(2.0f, 3.0f));
234}
235
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000236TEST(complex_h, cpowl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800237 ASSERT_EQ(8.0L, cpowl(2.0L, 3.0L));
238}
239
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000240TEST(complex_h, cproj) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800241 ASSERT_EQ(0.0, cproj(0));
242}
243
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000244TEST(complex_h, cprojf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800245 ASSERT_EQ(0.0f, cprojf(0));
246}
247
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000248TEST(complex_h, cprojl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800249 ASSERT_EQ(0.0, cprojl(0));
250}
251
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000252TEST(complex_h, creal) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800253 ASSERT_EQ(2.0, creal(2.0 + 3.0I));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800254}
255
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000256TEST(complex_h, crealf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800257 ASSERT_EQ(2.0f, crealf(2.0f + 3.0fI));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800258}
259
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000260TEST(complex_h, creall) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800261 ASSERT_EQ(2.0, creall(2.0L + 3.0LI));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800262}
263
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000264TEST(complex_h, csin) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800265 ASSERT_EQ(0.0, csin(0));
266}
267
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000268TEST(complex_h, csinf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800269 ASSERT_EQ(0.0, csinf(0));
270}
271
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000272TEST(complex_h, csinl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800273 ASSERT_EQ(0.0, csinl(0));
274}
275
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000276TEST(complex_h, csinh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800277 ASSERT_EQ(0.0, csinh(0));
278}
279
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000280TEST(complex_h, csinhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800281 ASSERT_EQ(0.0, csinhf(0));
282}
283
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000284TEST(complex_h, csinhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800285 ASSERT_EQ(0.0, csinhl(0));
286}
287
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000288TEST(complex_h, csqrt) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800289 ASSERT_EQ(0.0, csqrt(0));
290}
291
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000292TEST(complex_h, csqrtf) {
Elliott Hughese9719f32016-09-26 09:35:04 -0700293 ASSERT_EQ(0.0f, csqrtf(0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800294}
295
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000296TEST(complex_h, csqrtl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800297 ASSERT_EQ(0.0, csqrtl(0));
298}
299
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000300TEST(complex_h, ctan) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800301 ASSERT_EQ(0.0, ctan(0));
302}
303
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000304TEST(complex_h, ctanf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800305 ASSERT_EQ(0.0, ctanf(0));
306}
307
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000308TEST(complex_h, ctanl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800309 ASSERT_EQ(0.0, ctanl(0));
310}
311
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000312TEST(complex_h, ctanh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800313 ASSERT_EQ(0.0, ctanh(0));
Elliott Hughes505ecd62018-07-23 14:25:36 -0700314
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)));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800331}
332
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000333TEST(complex_h, ctanhf) {
Elliott Hughes505ecd62018-07-23 14:25:36 -0700334 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)));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800352}
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800353
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000354TEST(complex_h, ctanhl) {
Elliott Hughes505ecd62018-07-23 14:25:36 -0700355 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)));
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800374}