1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // XFAIL: LIBCXX-AIX-FIXME 10 11 // <complex> 12 13 // template<class T> 14 // complex<T> 15 // log(const complex<T>& x); 16 17 #include <complex> 18 #include <cassert> 19 20 #include "test_macros.h" 21 #include "../cases.h" 22 23 template <class T> 24 void 25 test(const std::complex<T>& c, std::complex<T> x) 26 { 27 assert(log(c) == x); 28 } 29 30 template <class T> 31 void 32 test() 33 { 34 test(std::complex<T>(0, 0), std::complex<T>(-INFINITY, 0)); 35 } 36 37 void test_edges() 38 { 39 const double pi = std::atan2(+0., -0.); 40 const unsigned N = sizeof(testcases) / sizeof(testcases[0]); 41 for (unsigned i = 0; i < N; ++i) 42 { 43 std::complex<double> r = log(testcases[i]); 44 if (testcases[i].real() == 0 && testcases[i].imag() == 0) 45 { 46 if (std::signbit(testcases[i].real())) 47 { 48 assert(std::isinf(r.real())); 49 assert(r.real() < 0); 50 if (std::signbit(testcases[i].imag())) 51 is_about(r.imag(), -pi); 52 else 53 is_about(r.imag(), pi); 54 } 55 else 56 { 57 assert(std::isinf(r.real())); 58 assert(r.real() < 0); 59 assert(r.imag() == 0); 60 assert(std::signbit(testcases[i].imag()) == std::signbit(r.imag())); 61 } 62 } 63 else if (std::isfinite(testcases[i].real()) && std::isinf(testcases[i].imag())) 64 { 65 assert(std::isinf(r.real())); 66 assert(r.real() > 0); 67 if (testcases[i].imag() > 0) 68 is_about(r.imag(), pi/2); 69 else 70 is_about(r.imag(), -pi/2); 71 } 72 else if (std::isfinite(testcases[i].real()) && std::isnan(testcases[i].imag())) 73 { 74 assert(std::isnan(r.real())); 75 assert(std::isnan(r.imag())); 76 } 77 else if (std::isinf(testcases[i].real()) && testcases[i].real() < 0 && std::isfinite(testcases[i].imag())) 78 { 79 assert(std::isinf(r.real()) && r.real() > 0); 80 if (r.imag() > 0) 81 is_about(r.imag(), pi); 82 else 83 is_about(r.imag(), -pi); 84 } 85 else if (std::isinf(testcases[i].real()) && testcases[i].real() > 0 && std::isfinite(testcases[i].imag())) 86 { 87 assert(std::isinf(r.real()) && r.real() > 0); 88 assert(r.imag() == 0); 89 assert(std::signbit(testcases[i].imag()) == std::signbit(r.imag())); 90 } 91 else if (testcases[i].real() == 1 && testcases[i].imag() == 0) 92 { 93 assert(r.real() == 0); 94 assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); 95 } 96 else if (testcases[i].real() == 0 && testcases[i].imag() == 1) 97 { 98 assert(r.real() == 0); 99 is_about(r.imag(), pi/2); 100 } 101 else if (testcases[i].real() == -1 && testcases[i].imag() == 0) 102 { 103 assert(r.real() == 0); 104 if (std::signbit(testcases[i].imag())) 105 is_about(r.imag(), -pi); 106 else 107 is_about(r.imag(), pi); 108 } 109 else if (testcases[i].real() == 0 && testcases[i].imag() == -1) 110 { 111 assert(r.real() == 0); 112 is_about(r.imag(), -pi/2); 113 } 114 else if (std::isfinite(testcases[i].real()) && std::isfinite(testcases[i].imag()) && abs(testcases[i]) < 1) 115 { 116 assert( std::signbit(r.real())); 117 assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); 118 } 119 else if (std::isfinite(testcases[i].real()) && std::isfinite(testcases[i].imag()) && abs(testcases[i]) > 1) 120 { 121 assert(!std::signbit(r.real())); 122 assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); 123 } 124 } 125 } 126 127 int main(int, char**) 128 { 129 test<float>(); 130 test<double>(); 131 test<long double>(); 132 test_edges(); 133 134 return 0; 135 } 136