1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <complex> 11 12 // template<class T> 13 // complex<T> 14 // acos(const complex<T>& x); 15 16 #include <complex> 17 #include <cassert> 18 19 #include "../cases.h" 20 21 template <class T> 22 void 23 test(const std::complex<T>& c, std::complex<T> x) 24 { 25 assert(acos(c) == x); 26 } 27 28 template <class T> 29 void 30 test() 31 { 32 test(std::complex<T>(INFINITY, 1), std::complex<T>(0, -INFINITY)); 33 } 34 35 void test_edges() 36 { 37 typedef std::complex<double> C; 38 const double pi = std::atan2(+0., -0.); 39 const unsigned N = sizeof(x) / sizeof(x[0]); 40 for (unsigned i = 0; i < N; ++i) 41 { 42 std::complex<double> r = acos(x[i]); 43 if (x[i].real() == 0 && x[i].imag() == 0) 44 { 45 is_about(r.real(), pi/2); 46 assert(r.imag() == 0); 47 assert(std::signbit(x[i].imag()) != std::signbit(r.imag())); 48 } 49 else if (x[i].real() == 0 && std::isnan(x[i].imag())) 50 { 51 is_about(r.real(), pi/2); 52 assert(std::isnan(r.imag())); 53 } 54 else if (std::isfinite(x[i].real()) && std::isinf(x[i].imag())) 55 { 56 is_about(r.real(), pi/2); 57 assert(std::isinf(r.imag())); 58 assert(std::signbit(x[i].imag()) != std::signbit(r.imag())); 59 } 60 else if (std::isfinite(x[i].real()) && x[i].real() != 0 && std::isnan(x[i].imag())) 61 { 62 assert(std::isnan(r.real())); 63 assert(std::isnan(r.imag())); 64 } 65 else if (std::isinf(x[i].real()) && x[i].real() < 0 && std::isfinite(x[i].imag())) 66 { 67 is_about(r.real(), pi); 68 assert(std::isinf(r.imag())); 69 assert(std::signbit(x[i].imag()) != std::signbit(r.imag())); 70 } 71 else if (std::isinf(x[i].real()) && x[i].real() > 0 && std::isfinite(x[i].imag())) 72 { 73 assert(r.real() == 0); 74 assert(!std::signbit(r.real())); 75 assert(std::isinf(r.imag())); 76 assert(std::signbit(x[i].imag()) != std::signbit(r.imag())); 77 } 78 else if (std::isinf(x[i].real()) && x[i].real() < 0 && std::isinf(x[i].imag())) 79 { 80 is_about(r.real(), 0.75 * pi); 81 assert(std::isinf(r.imag())); 82 assert(std::signbit(x[i].imag()) != std::signbit(r.imag())); 83 } 84 else if (std::isinf(x[i].real()) && x[i].real() > 0 && std::isinf(x[i].imag())) 85 { 86 is_about(r.real(), 0.25 * pi); 87 assert(std::isinf(r.imag())); 88 assert(std::signbit(x[i].imag()) != std::signbit(r.imag())); 89 } 90 else if (std::isinf(x[i].real()) && std::isnan(x[i].imag())) 91 { 92 assert(std::isnan(r.real())); 93 assert(std::isinf(r.imag())); 94 } 95 else if (std::isnan(x[i].real()) && std::isfinite(x[i].imag())) 96 { 97 assert(std::isnan(r.real())); 98 assert(std::isnan(r.imag())); 99 } 100 else if (std::isnan(x[i].real()) && std::isinf(x[i].imag())) 101 { 102 assert(std::isnan(r.real())); 103 assert(std::isinf(r.imag())); 104 assert(std::signbit(x[i].imag()) != std::signbit(r.imag())); 105 } 106 else if (std::isnan(x[i].real()) && std::isnan(x[i].imag())) 107 { 108 assert(std::isnan(r.real())); 109 assert(std::isnan(r.imag())); 110 } 111 else if (!std::signbit(x[i].real()) && !std::signbit(x[i].imag())) 112 { 113 assert(!std::signbit(r.real())); 114 assert( std::signbit(r.imag())); 115 } 116 else if (std::signbit(x[i].real()) && !std::signbit(x[i].imag())) 117 { 118 assert(!std::signbit(r.real())); 119 assert( std::signbit(r.imag())); 120 } 121 else if (std::signbit(x[i].real()) && std::signbit(x[i].imag())) 122 { 123 assert(!std::signbit(r.real())); 124 assert(!std::signbit(r.imag())); 125 } 126 else if (!std::signbit(x[i].real()) && std::signbit(x[i].imag())) 127 { 128 assert(!std::signbit(r.real())); 129 assert(!std::signbit(r.imag())); 130 } 131 } 132 } 133 134 int main() 135 { 136 test<float>(); 137 test<double>(); 138 test<long double>(); 139 test_edges(); 140 } 141