Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
Josh Gao | ff504e6 | 2016-04-29 11:52:39 -0700 | [diff] [blame] | 19 | // This file is compiled against both glibc and bionic, and our complex.h |
| 20 | // depends on bionic-specific macros, so hack around that. |
| 21 | #include <sys/cdefs.h> |
| 22 | #if !defined(__INTRODUCED_IN) |
| 23 | #define __INTRODUCED_IN(x) |
Josh Gao | 5a3d5ca | 2016-04-29 12:15:18 -0700 | [diff] [blame] | 24 | #define __INTRODUCED_IN_32(x) |
| 25 | #define __INTRODUCED_IN_64(x) |
Josh Gao | 34c599a | 2016-04-29 13:45:25 -0700 | [diff] [blame] | 26 | #define __INTRODUCED_IN_FUTURE |
Josh Gao | ff504e6 | 2016-04-29 11:52:39 -0700 | [diff] [blame] | 27 | #endif |
| 28 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 29 | // libc++ actively gets in the way of including <complex.h> from C++, so we |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 30 | // have to be naughty. |
Dan Albert | b2ca031 | 2017-08-04 15:37:39 -0700 | [diff] [blame^] | 31 | #include "../libc/include/complex.h" |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 32 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 33 | // (libc++ also seems to have really bad implementations of its own that ignore |
| 34 | // the intricacies of floating point math.) |
| 35 | // http://llvm.org/bugs/show_bug.cgi?id=21504 |
| 36 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 37 | #include <math.h> // For M_PI_2/M_PI_2l. |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 38 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 39 | // Note that gtest doesn't support complex numbers, so the output from |
| 40 | // assertion failures is misleading/useless (at best you'll only see the real |
| 41 | // part). |
| 42 | // TODO: find out why gtest doesn't use these; until then they're only useful |
| 43 | // for manual printf^Woperator<< debugging. |
| 44 | #include <iostream> |
| 45 | std::ostream& operator<<(std::ostream& os, const double _Complex c) { |
| 46 | os << "(" << creal(c) << "," << cimag(c) << "i)"; |
| 47 | return os; |
| 48 | } |
| 49 | std::ostream& operator<<(std::ostream& os, const float _Complex c) { |
| 50 | os << "(" << crealf(c) << "," << cimagf(c) << "i)"; |
| 51 | return os; |
| 52 | } |
| 53 | std::ostream& operator<<(std::ostream& os, const long double _Complex c) { |
| 54 | os << "(" << creall(c) << "," << cimagl(c) << "i)"; |
| 55 | return os; |
| 56 | } |
| 57 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 58 | TEST(complex, cabs) { |
| 59 | ASSERT_EQ(0.0, cabs(0)); |
| 60 | } |
| 61 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 62 | TEST(complex, cabsf) { |
| 63 | ASSERT_EQ(0.0, cabsf(0)); |
| 64 | } |
| 65 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 66 | TEST(complex, cabsl) { |
| 67 | ASSERT_EQ(0.0, cabsl(0)); |
| 68 | } |
| 69 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 70 | TEST(complex, cacos) { |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 71 | ASSERT_EQ(M_PI_2, cacos(0.0)); |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 74 | TEST(complex, cacosf) { |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 75 | ASSERT_EQ(static_cast<float>(M_PI_2), cacosf(0.0)); |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 78 | TEST(complex, cacosl) { |
| 79 | ASSERT_EQ(M_PI_2l, cacosl(0.0)); |
| 80 | } |
| 81 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 82 | TEST(complex, cacosh) { |
| 83 | ASSERT_EQ(0.0, cacosh(1.0)); |
| 84 | } |
| 85 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 86 | TEST(complex, cacoshl) { |
| 87 | ASSERT_EQ(0.0, cacoshl(1.0)); |
| 88 | } |
| 89 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 90 | TEST(complex, cacoshf) { |
| 91 | ASSERT_EQ(0.0, cacoshf(1.0)); |
| 92 | } |
| 93 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 94 | TEST(complex, carg) { |
| 95 | ASSERT_EQ(0.0, carg(0)); |
| 96 | } |
| 97 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 98 | TEST(complex, cargf) { |
| 99 | ASSERT_EQ(0.0, cargf(0)); |
| 100 | } |
| 101 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 102 | TEST(complex, cargl) { |
| 103 | ASSERT_EQ(0.0, cargl(0)); |
| 104 | } |
| 105 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 106 | TEST(complex, casin) { |
| 107 | ASSERT_EQ(0.0, casin(0)); |
| 108 | } |
| 109 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 110 | TEST(complex, casinf) { |
| 111 | ASSERT_EQ(0.0, casinf(0)); |
| 112 | } |
| 113 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 114 | TEST(complex, casinl) { |
| 115 | ASSERT_EQ(0.0, casinl(0)); |
| 116 | } |
| 117 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 118 | TEST(complex, casinh) { |
| 119 | ASSERT_EQ(0.0, casinh(0)); |
| 120 | } |
| 121 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 122 | TEST(complex, casinhf) { |
| 123 | ASSERT_EQ(0.0, casinhf(0)); |
| 124 | } |
| 125 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 126 | TEST(complex, casinhl) { |
| 127 | ASSERT_EQ(0.0, casinhl(0)); |
| 128 | } |
| 129 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 130 | TEST(complex, catan) { |
| 131 | ASSERT_EQ(0.0, catan(0)); |
| 132 | } |
| 133 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 134 | TEST(complex, catanf) { |
| 135 | ASSERT_EQ(0.0, catanf(0)); |
| 136 | } |
| 137 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 138 | TEST(complex, catanl) { |
| 139 | ASSERT_EQ(0.0, catanl(0)); |
| 140 | } |
| 141 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 142 | TEST(complex, catanh) { |
| 143 | ASSERT_EQ(0.0, catanh(0)); |
| 144 | } |
| 145 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 146 | TEST(complex, catanhf) { |
| 147 | ASSERT_EQ(0.0, catanhf(0)); |
| 148 | } |
| 149 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 150 | TEST(complex, catanhl) { |
| 151 | ASSERT_EQ(0.0, catanhl(0)); |
| 152 | } |
| 153 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 154 | TEST(complex, ccos) { |
| 155 | ASSERT_EQ(1.0, ccos(0)); |
| 156 | } |
| 157 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 158 | TEST(complex, ccosf) { |
| 159 | ASSERT_EQ(1.0, ccosf(0)); |
| 160 | } |
| 161 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 162 | TEST(complex, ccosl) { |
| 163 | ASSERT_EQ(1.0, ccosl(0)); |
| 164 | } |
| 165 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 166 | TEST(complex, ccosh) { |
| 167 | ASSERT_EQ(1.0, ccosh(0)); |
| 168 | } |
| 169 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 170 | TEST(complex, ccoshf) { |
| 171 | ASSERT_EQ(1.0, ccoshf(0)); |
| 172 | } |
| 173 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 174 | TEST(complex, ccoshl) { |
| 175 | ASSERT_EQ(1.0, ccoshl(0)); |
| 176 | } |
| 177 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 178 | TEST(complex, cexp) { |
| 179 | ASSERT_EQ(1.0, cexp(0)); |
| 180 | } |
| 181 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 182 | TEST(complex, cexpf) { |
| 183 | ASSERT_EQ(1.0, cexpf(0)); |
| 184 | } |
| 185 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 186 | TEST(complex, cexpl) { |
| 187 | ASSERT_EQ(1.0, cexpl(0)); |
| 188 | } |
| 189 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 190 | TEST(complex, cimag) { |
| 191 | ASSERT_EQ(0.0, cimag(0)); |
| 192 | } |
| 193 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 194 | TEST(complex, cimagf) { |
| 195 | ASSERT_EQ(0.0f, cimagf(0)); |
| 196 | } |
| 197 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 198 | TEST(complex, cimagl) { |
| 199 | ASSERT_EQ(0.0, cimagl(0)); |
| 200 | } |
| 201 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 202 | TEST(complex, clog) { |
| 203 | ASSERT_EQ(0.0, clog(1.0)); |
| 204 | } |
| 205 | |
| 206 | TEST(complex, clogf) { |
| 207 | ASSERT_EQ(0.0f, clogf(1.0f)); |
| 208 | } |
| 209 | |
| 210 | TEST(complex, clogl) { |
| 211 | ASSERT_EQ(0.0L, clogl(1.0L)); |
| 212 | } |
| 213 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 214 | TEST(complex, conj) { |
| 215 | ASSERT_EQ(0.0, conj(0)); |
| 216 | } |
| 217 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 218 | TEST(complex, conjf) { |
| 219 | ASSERT_EQ(0.0f, conjf(0)); |
| 220 | } |
| 221 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 222 | TEST(complex, conjl) { |
| 223 | ASSERT_EQ(0.0, conjl(0)); |
| 224 | } |
| 225 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 226 | TEST(complex, cpow) { |
| 227 | ASSERT_EQ(8.0, cpow(2.0, 3.0)); |
| 228 | } |
| 229 | |
| 230 | TEST(complex, cpowf) { |
| 231 | ASSERT_EQ(8.0f, cpowf(2.0f, 3.0f)); |
| 232 | } |
| 233 | |
| 234 | TEST(complex, cpowl) { |
| 235 | ASSERT_EQ(8.0L, cpowl(2.0L, 3.0L)); |
| 236 | } |
| 237 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 238 | TEST(complex, cproj) { |
| 239 | ASSERT_EQ(0.0, cproj(0)); |
| 240 | } |
| 241 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 242 | TEST(complex, cprojf) { |
| 243 | ASSERT_EQ(0.0f, cprojf(0)); |
| 244 | } |
| 245 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 246 | TEST(complex, cprojl) { |
| 247 | ASSERT_EQ(0.0, cprojl(0)); |
| 248 | } |
| 249 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 250 | TEST(complex, creal) { |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 251 | ASSERT_EQ(2.0, creal(2.0 + 3.0I)); |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 254 | TEST(complex, crealf) { |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 255 | ASSERT_EQ(2.0f, crealf(2.0f + 3.0fI)); |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 258 | TEST(complex, creall) { |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 259 | ASSERT_EQ(2.0, creall(2.0L + 3.0LI)); |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 262 | TEST(complex, csin) { |
| 263 | ASSERT_EQ(0.0, csin(0)); |
| 264 | } |
| 265 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 266 | TEST(complex, csinf) { |
| 267 | ASSERT_EQ(0.0, csinf(0)); |
| 268 | } |
| 269 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 270 | TEST(complex, csinl) { |
| 271 | ASSERT_EQ(0.0, csinl(0)); |
| 272 | } |
| 273 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 274 | TEST(complex, csinh) { |
| 275 | ASSERT_EQ(0.0, csinh(0)); |
| 276 | } |
| 277 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 278 | TEST(complex, csinhf) { |
| 279 | ASSERT_EQ(0.0, csinhf(0)); |
| 280 | } |
| 281 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 282 | TEST(complex, csinhl) { |
| 283 | ASSERT_EQ(0.0, csinhl(0)); |
| 284 | } |
| 285 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 286 | TEST(complex, csqrt) { |
| 287 | ASSERT_EQ(0.0, csqrt(0)); |
| 288 | } |
| 289 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 290 | TEST(complex, csqrtf) { |
Elliott Hughes | e9719f3 | 2016-09-26 09:35:04 -0700 | [diff] [blame] | 291 | ASSERT_EQ(0.0f, csqrtf(0)); |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 294 | TEST(complex, csqrtl) { |
| 295 | ASSERT_EQ(0.0, csqrtl(0)); |
| 296 | } |
| 297 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 298 | TEST(complex, ctan) { |
| 299 | ASSERT_EQ(0.0, ctan(0)); |
| 300 | } |
| 301 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 302 | TEST(complex, ctanf) { |
| 303 | ASSERT_EQ(0.0, ctanf(0)); |
| 304 | } |
| 305 | |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 306 | TEST(complex, ctanl) { |
| 307 | ASSERT_EQ(0.0, ctanl(0)); |
| 308 | } |
| 309 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 310 | TEST(complex, ctanh) { |
| 311 | ASSERT_EQ(0.0, ctanh(0)); |
| 312 | } |
| 313 | |
Elliott Hughes | b8ee16f | 2014-11-06 11:16:55 -0800 | [diff] [blame] | 314 | TEST(complex, ctanhf) { |
| 315 | ASSERT_EQ(0.0, ctanhf(0)); |
| 316 | } |
Elliott Hughes | 9ee6adb | 2016-03-11 14:49:13 -0800 | [diff] [blame] | 317 | |
| 318 | TEST(complex, ctanhl) { |
| 319 | ASSERT_EQ(0.0, ctanhl(0)); |
| 320 | } |