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