blob: 6a1831f782f654162f1e060381cea9cbee5e6352 [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.
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080035#include <iostream>
Yi Kong43ccab82018-01-05 16:22:10 -080036namespace testing {
37namespace internal {
38inline void PrintTo(const double _Complex& c, std::ostream* os) {
39 *os << "(" << creal(c) << "," << cimag(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080040}
Yi Kong43ccab82018-01-05 16:22:10 -080041inline void PrintTo(const float _Complex& c, std::ostream* os) {
42 *os << "(" << crealf(c) << "," << cimagf(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080043}
Yi Kong43ccab82018-01-05 16:22:10 -080044inline void PrintTo(const long double _Complex& c, std::ostream* os) {
45 *os << "(" << creall(c) << "," << cimagl(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080046}
Yi Kong43ccab82018-01-05 16:22:10 -080047}
48}
49
50// Macro 'I' defined in complex.h conflicts with gtest.h.
51#pragma push_macro("I")
52#undef I
53#include <gtest/gtest.h>
54#pragma pop_macro("I")
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080055
Elliott Hughesab2d3e12023-06-07 17:16:34 +000056TEST(complex_h, cabs) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080057 ASSERT_EQ(0.0, cabs(0));
58}
59
Elliott Hughesab2d3e12023-06-07 17:16:34 +000060TEST(complex_h, cabsf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080061 ASSERT_EQ(0.0, cabsf(0));
62}
63
Elliott Hughesab2d3e12023-06-07 17:16:34 +000064TEST(complex_h, cabsl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080065 ASSERT_EQ(0.0, cabsl(0));
66}
67
Elliott Hughesab2d3e12023-06-07 17:16:34 +000068TEST(complex_h, cacos) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080069 ASSERT_EQ(M_PI_2, cacos(0.0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080070}
71
Elliott Hughesab2d3e12023-06-07 17:16:34 +000072TEST(complex_h, cacosf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080073 ASSERT_EQ(static_cast<float>(M_PI_2), cacosf(0.0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080074}
75
Elliott Hughesab2d3e12023-06-07 17:16:34 +000076TEST(complex_h, cacosl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080077 ASSERT_EQ(M_PI_2l, cacosl(0.0));
78}
79
Elliott Hughesab2d3e12023-06-07 17:16:34 +000080TEST(complex_h, cacosh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080081 ASSERT_EQ(0.0, cacosh(1.0));
82}
83
Elliott Hughesab2d3e12023-06-07 17:16:34 +000084TEST(complex_h, cacoshl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080085 ASSERT_EQ(0.0, cacoshl(1.0));
86}
87
Elliott Hughesab2d3e12023-06-07 17:16:34 +000088TEST(complex_h, cacoshf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080089 ASSERT_EQ(0.0, cacoshf(1.0));
90}
91
Elliott Hughesab2d3e12023-06-07 17:16:34 +000092TEST(complex_h, carg) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080093 ASSERT_EQ(0.0, carg(0));
94}
95
Elliott Hughesab2d3e12023-06-07 17:16:34 +000096TEST(complex_h, cargf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080097 ASSERT_EQ(0.0, cargf(0));
98}
99
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000100TEST(complex_h, cargl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800101 ASSERT_EQ(0.0, cargl(0));
102}
103
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000104TEST(complex_h, casin) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800105 ASSERT_EQ(0.0, casin(0));
106}
107
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000108TEST(complex_h, casinf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800109 ASSERT_EQ(0.0, casinf(0));
110}
111
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000112TEST(complex_h, casinl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800113 ASSERT_EQ(0.0, casinl(0));
114}
115
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000116TEST(complex_h, casinh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800117 ASSERT_EQ(0.0, casinh(0));
118}
119
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000120TEST(complex_h, casinhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800121 ASSERT_EQ(0.0, casinhf(0));
122}
123
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000124TEST(complex_h, casinhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800125 ASSERT_EQ(0.0, casinhl(0));
126}
127
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000128TEST(complex_h, catan) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800129 ASSERT_EQ(0.0, catan(0));
130}
131
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000132TEST(complex_h, catanf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800133 ASSERT_EQ(0.0, catanf(0));
134}
135
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000136TEST(complex_h, catanl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800137 ASSERT_EQ(0.0, catanl(0));
138}
139
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000140TEST(complex_h, catanh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800141 ASSERT_EQ(0.0, catanh(0));
142}
143
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000144TEST(complex_h, catanhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800145 ASSERT_EQ(0.0, catanhf(0));
146}
147
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000148TEST(complex_h, catanhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800149 ASSERT_EQ(0.0, catanhl(0));
150}
151
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000152TEST(complex_h, ccos) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800153 ASSERT_EQ(1.0, ccos(0));
154}
155
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000156TEST(complex_h, ccosf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800157 ASSERT_EQ(1.0, ccosf(0));
158}
159
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000160TEST(complex_h, ccosl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800161 ASSERT_EQ(1.0, ccosl(0));
162}
163
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000164TEST(complex_h, ccosh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800165 ASSERT_EQ(1.0, ccosh(0));
166}
167
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000168TEST(complex_h, ccoshf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800169 ASSERT_EQ(1.0, ccoshf(0));
170}
171
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000172TEST(complex_h, ccoshl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800173 ASSERT_EQ(1.0, ccoshl(0));
174}
175
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000176TEST(complex_h, cexp) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800177 ASSERT_EQ(1.0, cexp(0));
178}
179
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000180TEST(complex_h, cexpf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800181 ASSERT_EQ(1.0, cexpf(0));
182}
183
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000184TEST(complex_h, cexpl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800185 ASSERT_EQ(1.0, cexpl(0));
186}
187
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000188TEST(complex_h, cimag) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800189 ASSERT_EQ(0.0, cimag(0));
190}
191
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000192TEST(complex_h, cimagf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800193 ASSERT_EQ(0.0f, cimagf(0));
194}
195
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000196TEST(complex_h, cimagl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800197 ASSERT_EQ(0.0, cimagl(0));
198}
199
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000200TEST(complex_h, clog) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800201 ASSERT_EQ(0.0, clog(1.0));
202}
203
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000204TEST(complex_h, clogf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800205 ASSERT_EQ(0.0f, clogf(1.0f));
206}
207
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000208TEST(complex_h, clogl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800209 ASSERT_EQ(0.0L, clogl(1.0L));
210}
211
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000212TEST(complex_h, conj) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800213 ASSERT_EQ(0.0, conj(0));
214}
215
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000216TEST(complex_h, conjf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800217 ASSERT_EQ(0.0f, conjf(0));
218}
219
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000220TEST(complex_h, conjl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800221 ASSERT_EQ(0.0, conjl(0));
222}
223
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000224TEST(complex_h, cpow) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800225 ASSERT_EQ(8.0, cpow(2.0, 3.0));
226}
227
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000228TEST(complex_h, cpowf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800229 ASSERT_EQ(8.0f, cpowf(2.0f, 3.0f));
230}
231
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000232TEST(complex_h, cpowl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800233 ASSERT_EQ(8.0L, cpowl(2.0L, 3.0L));
234}
235
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000236TEST(complex_h, cproj) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800237 ASSERT_EQ(0.0, cproj(0));
238}
239
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000240TEST(complex_h, cprojf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800241 ASSERT_EQ(0.0f, cprojf(0));
242}
243
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000244TEST(complex_h, cprojl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800245 ASSERT_EQ(0.0, cprojl(0));
246}
247
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000248TEST(complex_h, creal) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800249 ASSERT_EQ(2.0, creal(2.0 + 3.0I));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800250}
251
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000252TEST(complex_h, crealf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800253 ASSERT_EQ(2.0f, crealf(2.0f + 3.0fI));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800254}
255
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000256TEST(complex_h, creall) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800257 ASSERT_EQ(2.0, creall(2.0L + 3.0LI));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800258}
259
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000260TEST(complex_h, csin) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800261 ASSERT_EQ(0.0, csin(0));
262}
263
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000264TEST(complex_h, csinf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800265 ASSERT_EQ(0.0, csinf(0));
266}
267
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000268TEST(complex_h, csinl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800269 ASSERT_EQ(0.0, csinl(0));
270}
271
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000272TEST(complex_h, csinh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800273 ASSERT_EQ(0.0, csinh(0));
274}
275
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000276TEST(complex_h, csinhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800277 ASSERT_EQ(0.0, csinhf(0));
278}
279
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000280TEST(complex_h, csinhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800281 ASSERT_EQ(0.0, csinhl(0));
282}
283
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000284TEST(complex_h, csqrt) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800285 ASSERT_EQ(0.0, csqrt(0));
286}
287
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000288TEST(complex_h, csqrtf) {
Elliott Hughese9719f32016-09-26 09:35:04 -0700289 ASSERT_EQ(0.0f, csqrtf(0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800290}
291
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000292TEST(complex_h, csqrtl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800293 ASSERT_EQ(0.0, csqrtl(0));
294}
295
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000296TEST(complex_h, ctan) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800297 ASSERT_EQ(0.0, ctan(0));
298}
299
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000300TEST(complex_h, ctanf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800301 ASSERT_EQ(0.0, ctanf(0));
302}
303
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000304TEST(complex_h, ctanl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800305 ASSERT_EQ(0.0, ctanl(0));
306}
307
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000308TEST(complex_h, ctanh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800309 ASSERT_EQ(0.0, ctanh(0));
Elliott Hughes505ecd62018-07-23 14:25:36 -0700310
311 double complex z;
312
313 // If z is NaN+0i, the result is NaN+0i.
314 z = ctanh(nan("") + 0i);
315 ASSERT_TRUE(isnan(creal(z)));
316 ASSERT_EQ(0.0, cimag(z));
317
318 // If z is NaN+yi, the result is NaN+NaNi.
319 z = ctanh(nan("") + 2.0i);
320 ASSERT_TRUE(isnan(creal(z)));
321 ASSERT_TRUE(isnan(cimag(z)));
322
323 // If z is NaN+NaNi, the result is NaN+NaNi.
324 z = ctanh(nan("") + nan("") * I);
325 ASSERT_TRUE(isnan(creal(z)));
326 ASSERT_TRUE(isnan(cimag(z)));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800327}
328
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000329TEST(complex_h, ctanhf) {
Elliott Hughes505ecd62018-07-23 14:25:36 -0700330 ASSERT_EQ(0.0f, ctanhf(0.0f));
331
332 float complex z;
333
334 // If z is NaN+0i, the result is NaN+0i.
335 z = ctanhf(nanf("") + 0.0fi);
336 ASSERT_TRUE(isnan(crealf(z)));
337 ASSERT_EQ(0.0f, cimagf(z));
338
339 // If z is NaN+yi, the result is NaN+NaNi.
340 z = ctanhf(nanf("") + 2.0fi);
341 ASSERT_TRUE(isnan(crealf(z)));
342 ASSERT_TRUE(isnan(cimagf(z)));
343
344 // If z is NaN+NaNi, the result is NaN+NaNi.
345 z = ctanhf(nanf("") + nanf("") * I);
346 ASSERT_TRUE(isnan(crealf(z)));
347 ASSERT_TRUE(isnan(cimagf(z)));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800348}
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800349
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000350TEST(complex_h, ctanhl) {
Elliott Hughes505ecd62018-07-23 14:25:36 -0700351 ASSERT_EQ(0.0L, ctanhl(0.0L));
352
353 long double complex z;
354
355 // If z is NaN+0i, the result is NaN+0i.
356 z = ctanhl(nanl("") + 0.0Li);
357 ASSERT_TRUE(isnan(creall(z)));
358 // TODO: this case is currently broken in the netbsd ctanhl.
359 // ASSERT_EQ(0.0L, cimagl(z));
360
361 // If z is NaN+yi, the result is NaN+NaNi.
362 z = ctanhl(nanl("") + 2.0Li);
363 ASSERT_TRUE(isnan(creall(z)));
364 ASSERT_TRUE(isnan(cimagl(z)));
365
366 // If z is NaN+NaNi, the result is NaN+NaNi.
367 z = ctanhl(nanl("") + nanl("") * I);
368 ASSERT_TRUE(isnan(creall(z)));
369 ASSERT_TRUE(isnan(cimagl(z)));
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800370}