blob: d27861a7c88ce60ee63733b49a18d5a159a81fdd [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)
Josh Gao5a3d5ca2016-04-29 12:15:18 -070022#define __INTRODUCED_IN_32(x)
23#define __INTRODUCED_IN_64(x)
Josh Gaoff504e62016-04-29 11:52:39 -070024#endif
25
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080026// libc++ actively gets in the way of including <complex.h> from C++, so we
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080027// have to be naughty.
Dan Albertb2ca0312017-08-04 15:37:39 -070028#include "../libc/include/complex.h"
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080029
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080030// (libc++ also seems to have really bad implementations of its own that ignore
31// the intricacies of floating point math.)
32// http://llvm.org/bugs/show_bug.cgi?id=21504
33
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080034#include <math.h> // For M_PI_2/M_PI_2l.
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080035
Yi Kong43ccab82018-01-05 16:22:10 -080036// Prettify gtest Complex printing.
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080037#include <iostream>
Yi Kong43ccab82018-01-05 16:22:10 -080038namespace testing {
39namespace internal {
40inline void PrintTo(const double _Complex& c, std::ostream* os) {
41 *os << "(" << creal(c) << "," << cimag(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080042}
Yi Kong43ccab82018-01-05 16:22:10 -080043inline void PrintTo(const float _Complex& c, std::ostream* os) {
44 *os << "(" << crealf(c) << "," << cimagf(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080045}
Yi Kong43ccab82018-01-05 16:22:10 -080046inline void PrintTo(const long double _Complex& c, std::ostream* os) {
47 *os << "(" << creall(c) << "," << cimagl(c) << "i)";
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080048}
Yi Kong43ccab82018-01-05 16:22:10 -080049}
50}
51
52// Macro 'I' defined in complex.h conflicts with gtest.h.
53#pragma push_macro("I")
54#undef I
55#include <gtest/gtest.h>
56#pragma pop_macro("I")
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080057
Elliott Hughesab2d3e12023-06-07 17:16:34 +000058TEST(complex_h, cabs) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080059 ASSERT_EQ(0.0, cabs(0));
60}
61
Elliott Hughesab2d3e12023-06-07 17:16:34 +000062TEST(complex_h, cabsf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080063 ASSERT_EQ(0.0, cabsf(0));
64}
65
Elliott Hughesab2d3e12023-06-07 17:16:34 +000066TEST(complex_h, cabsl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080067 ASSERT_EQ(0.0, cabsl(0));
68}
69
Elliott Hughesab2d3e12023-06-07 17:16:34 +000070TEST(complex_h, cacos) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080071 ASSERT_EQ(M_PI_2, cacos(0.0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080072}
73
Elliott Hughesab2d3e12023-06-07 17:16:34 +000074TEST(complex_h, cacosf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080075 ASSERT_EQ(static_cast<float>(M_PI_2), cacosf(0.0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080076}
77
Elliott Hughesab2d3e12023-06-07 17:16:34 +000078TEST(complex_h, cacosl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080079 ASSERT_EQ(M_PI_2l, cacosl(0.0));
80}
81
Elliott Hughesab2d3e12023-06-07 17:16:34 +000082TEST(complex_h, cacosh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080083 ASSERT_EQ(0.0, cacosh(1.0));
84}
85
Elliott Hughesab2d3e12023-06-07 17:16:34 +000086TEST(complex_h, cacoshl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -080087 ASSERT_EQ(0.0, cacoshl(1.0));
88}
89
Elliott Hughesab2d3e12023-06-07 17:16:34 +000090TEST(complex_h, cacoshf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080091 ASSERT_EQ(0.0, cacoshf(1.0));
92}
93
Elliott Hughesab2d3e12023-06-07 17:16:34 +000094TEST(complex_h, carg) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080095 ASSERT_EQ(0.0, carg(0));
96}
97
Elliott Hughesab2d3e12023-06-07 17:16:34 +000098TEST(complex_h, cargf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -080099 ASSERT_EQ(0.0, cargf(0));
100}
101
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000102TEST(complex_h, cargl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800103 ASSERT_EQ(0.0, cargl(0));
104}
105
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000106TEST(complex_h, casin) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800107 ASSERT_EQ(0.0, casin(0));
108}
109
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000110TEST(complex_h, casinf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800111 ASSERT_EQ(0.0, casinf(0));
112}
113
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000114TEST(complex_h, casinl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800115 ASSERT_EQ(0.0, casinl(0));
116}
117
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000118TEST(complex_h, casinh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800119 ASSERT_EQ(0.0, casinh(0));
120}
121
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000122TEST(complex_h, casinhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800123 ASSERT_EQ(0.0, casinhf(0));
124}
125
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000126TEST(complex_h, casinhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800127 ASSERT_EQ(0.0, casinhl(0));
128}
129
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000130TEST(complex_h, catan) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800131 ASSERT_EQ(0.0, catan(0));
132}
133
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000134TEST(complex_h, catanf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800135 ASSERT_EQ(0.0, catanf(0));
136}
137
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000138TEST(complex_h, catanl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800139 ASSERT_EQ(0.0, catanl(0));
140}
141
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000142TEST(complex_h, catanh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800143 ASSERT_EQ(0.0, catanh(0));
144}
145
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000146TEST(complex_h, catanhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800147 ASSERT_EQ(0.0, catanhf(0));
148}
149
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000150TEST(complex_h, catanhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800151 ASSERT_EQ(0.0, catanhl(0));
152}
153
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000154TEST(complex_h, ccos) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800155 ASSERT_EQ(1.0, ccos(0));
156}
157
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000158TEST(complex_h, ccosf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800159 ASSERT_EQ(1.0, ccosf(0));
160}
161
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000162TEST(complex_h, ccosl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800163 ASSERT_EQ(1.0, ccosl(0));
164}
165
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000166TEST(complex_h, ccosh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800167 ASSERT_EQ(1.0, ccosh(0));
168}
169
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000170TEST(complex_h, ccoshf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800171 ASSERT_EQ(1.0, ccoshf(0));
172}
173
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000174TEST(complex_h, ccoshl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800175 ASSERT_EQ(1.0, ccoshl(0));
176}
177
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000178TEST(complex_h, cexp) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800179 ASSERT_EQ(1.0, cexp(0));
180}
181
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000182TEST(complex_h, cexpf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800183 ASSERT_EQ(1.0, cexpf(0));
184}
185
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000186TEST(complex_h, cexpl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800187 ASSERT_EQ(1.0, cexpl(0));
188}
189
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000190TEST(complex_h, cimag) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800191 ASSERT_EQ(0.0, cimag(0));
192}
193
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000194TEST(complex_h, cimagf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800195 ASSERT_EQ(0.0f, cimagf(0));
196}
197
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000198TEST(complex_h, cimagl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800199 ASSERT_EQ(0.0, cimagl(0));
200}
201
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000202TEST(complex_h, clog) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800203 ASSERT_EQ(0.0, clog(1.0));
204}
205
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000206TEST(complex_h, clogf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800207 ASSERT_EQ(0.0f, clogf(1.0f));
208}
209
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000210TEST(complex_h, clogl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800211 ASSERT_EQ(0.0L, clogl(1.0L));
212}
213
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000214TEST(complex_h, conj) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800215 ASSERT_EQ(0.0, conj(0));
216}
217
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000218TEST(complex_h, conjf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800219 ASSERT_EQ(0.0f, conjf(0));
220}
221
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000222TEST(complex_h, conjl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800223 ASSERT_EQ(0.0, conjl(0));
224}
225
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000226TEST(complex_h, cpow) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800227 ASSERT_EQ(8.0, cpow(2.0, 3.0));
228}
229
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000230TEST(complex_h, cpowf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800231 ASSERT_EQ(8.0f, cpowf(2.0f, 3.0f));
232}
233
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000234TEST(complex_h, cpowl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800235 ASSERT_EQ(8.0L, cpowl(2.0L, 3.0L));
236}
237
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000238TEST(complex_h, cproj) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800239 ASSERT_EQ(0.0, cproj(0));
240}
241
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000242TEST(complex_h, cprojf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800243 ASSERT_EQ(0.0f, cprojf(0));
244}
245
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000246TEST(complex_h, cprojl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800247 ASSERT_EQ(0.0, cprojl(0));
248}
249
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000250TEST(complex_h, creal) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800251 ASSERT_EQ(2.0, creal(2.0 + 3.0I));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800252}
253
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000254TEST(complex_h, crealf) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800255 ASSERT_EQ(2.0f, crealf(2.0f + 3.0fI));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800256}
257
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000258TEST(complex_h, creall) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800259 ASSERT_EQ(2.0, creall(2.0L + 3.0LI));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800260}
261
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000262TEST(complex_h, csin) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800263 ASSERT_EQ(0.0, csin(0));
264}
265
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000266TEST(complex_h, csinf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800267 ASSERT_EQ(0.0, csinf(0));
268}
269
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000270TEST(complex_h, csinl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800271 ASSERT_EQ(0.0, csinl(0));
272}
273
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000274TEST(complex_h, csinh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800275 ASSERT_EQ(0.0, csinh(0));
276}
277
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000278TEST(complex_h, csinhf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800279 ASSERT_EQ(0.0, csinhf(0));
280}
281
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000282TEST(complex_h, csinhl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800283 ASSERT_EQ(0.0, csinhl(0));
284}
285
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000286TEST(complex_h, csqrt) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800287 ASSERT_EQ(0.0, csqrt(0));
288}
289
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000290TEST(complex_h, csqrtf) {
Elliott Hughese9719f32016-09-26 09:35:04 -0700291 ASSERT_EQ(0.0f, csqrtf(0));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800292}
293
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000294TEST(complex_h, csqrtl) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800295 ASSERT_EQ(0.0, csqrtl(0));
296}
297
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000298TEST(complex_h, ctan) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800299 ASSERT_EQ(0.0, ctan(0));
300}
301
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000302TEST(complex_h, ctanf) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800303 ASSERT_EQ(0.0, ctanf(0));
304}
305
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000306TEST(complex_h, ctanl) {
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800307 ASSERT_EQ(0.0, ctanl(0));
308}
309
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000310TEST(complex_h, ctanh) {
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800311 ASSERT_EQ(0.0, ctanh(0));
Elliott Hughes505ecd62018-07-23 14:25:36 -0700312
313 double complex z;
314
315 // If z is NaN+0i, the result is NaN+0i.
316 z = ctanh(nan("") + 0i);
317 ASSERT_TRUE(isnan(creal(z)));
318 ASSERT_EQ(0.0, cimag(z));
319
320 // If z is NaN+yi, the result is NaN+NaNi.
321 z = ctanh(nan("") + 2.0i);
322 ASSERT_TRUE(isnan(creal(z)));
323 ASSERT_TRUE(isnan(cimag(z)));
324
325 // If z is NaN+NaNi, the result is NaN+NaNi.
326 z = ctanh(nan("") + nan("") * I);
327 ASSERT_TRUE(isnan(creal(z)));
328 ASSERT_TRUE(isnan(cimag(z)));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800329}
330
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000331TEST(complex_h, ctanhf) {
Elliott Hughes505ecd62018-07-23 14:25:36 -0700332 ASSERT_EQ(0.0f, ctanhf(0.0f));
333
334 float complex z;
335
336 // If z is NaN+0i, the result is NaN+0i.
337 z = ctanhf(nanf("") + 0.0fi);
338 ASSERT_TRUE(isnan(crealf(z)));
339 ASSERT_EQ(0.0f, cimagf(z));
340
341 // If z is NaN+yi, the result is NaN+NaNi.
342 z = ctanhf(nanf("") + 2.0fi);
343 ASSERT_TRUE(isnan(crealf(z)));
344 ASSERT_TRUE(isnan(cimagf(z)));
345
346 // If z is NaN+NaNi, the result is NaN+NaNi.
347 z = ctanhf(nanf("") + nanf("") * I);
348 ASSERT_TRUE(isnan(crealf(z)));
349 ASSERT_TRUE(isnan(cimagf(z)));
Elliott Hughesb8ee16f2014-11-06 11:16:55 -0800350}
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800351
Elliott Hughesab2d3e12023-06-07 17:16:34 +0000352TEST(complex_h, ctanhl) {
Elliott Hughes505ecd62018-07-23 14:25:36 -0700353 ASSERT_EQ(0.0L, ctanhl(0.0L));
354
355 long double complex z;
356
357 // If z is NaN+0i, the result is NaN+0i.
358 z = ctanhl(nanl("") + 0.0Li);
359 ASSERT_TRUE(isnan(creall(z)));
360 // TODO: this case is currently broken in the netbsd ctanhl.
361 // ASSERT_EQ(0.0L, cimagl(z));
362
363 // If z is NaN+yi, the result is NaN+NaNi.
364 z = ctanhl(nanl("") + 2.0Li);
365 ASSERT_TRUE(isnan(creall(z)));
366 ASSERT_TRUE(isnan(cimagl(z)));
367
368 // If z is NaN+NaNi, the result is NaN+NaNi.
369 z = ctanhl(nanl("") + nanl("") * I);
370 ASSERT_TRUE(isnan(creall(z)));
371 ASSERT_TRUE(isnan(cimagl(z)));
Elliott Hughes9ee6adb2016-03-11 14:49:13 -0800372}