| 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 | // <complex> |
| 10 | |
| 11 | // template<class T> |
| 12 | // complex<T> |
| 13 | // acosh(const complex<T>& x); |
| 14 | |
| 15 | #include <complex> |
| 16 | #include <cassert> |
| 17 | |
| 18 | #include "test_macros.h" |
| 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(acosh(c) == x); |
| 26 | } |
| 27 | |
| 28 | template <class T> |
| 29 | void |
| 30 | test() |
| 31 | { |
| 32 | test(std::complex<T>(INFINITY, 1), std::complex<T>(INFINITY, 0)); |
| 33 | } |
| 34 | |
| 35 | void test_edges() |
| 36 | { |
| 37 | const double pi = std::atan2(y: +0., x: -0.); |
| 38 | const unsigned N = sizeof(testcases) / sizeof(testcases[0]); |
| 39 | for (unsigned i = 0; i < N; ++i) |
| 40 | { |
| 41 | std::complex<double> r = acosh(testcases[i]); |
| 42 | if (testcases[i].real() == 0 && testcases[i].imag() == 0) |
| 43 | { |
| 44 | assert(!std::signbit(r.real())); |
| 45 | if (std::signbit(testcases[i].imag())) |
| 46 | is_about(x: r.imag(), y: -pi/2); |
| 47 | else |
| 48 | is_about(x: r.imag(), y: pi/2); |
| 49 | } |
| 50 | else if (testcases[i].real() == 1 && testcases[i].imag() == 0) |
| 51 | { |
| 52 | assert(r.real() == 0); |
| 53 | assert(!std::signbit(r.real())); |
| 54 | assert(r.imag() == 0); |
| 55 | assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); |
| 56 | } |
| 57 | else if (testcases[i].real() == -1 && testcases[i].imag() == 0) |
| 58 | { |
| 59 | assert(r.real() == 0); |
| 60 | assert(!std::signbit(r.real())); |
| 61 | if (std::signbit(testcases[i].imag())) |
| 62 | is_about(x: r.imag(), y: -pi); |
| 63 | else |
| 64 | is_about(x: r.imag(), y: pi); |
| 65 | } |
| 66 | else if (std::isfinite(testcases[i].real()) && std::isinf(testcases[i].imag())) |
| 67 | { |
| 68 | assert(std::isinf(r.real())); |
| 69 | assert(r.real() > 0); |
| 70 | if (std::signbit(testcases[i].imag())) |
| 71 | is_about(x: r.imag(), y: -pi/2); |
| 72 | else |
| 73 | is_about(x: r.imag(), y: pi/2); |
| 74 | } |
| 75 | else if (std::isfinite(testcases[i].real()) && std::isnan(testcases[i].imag())) |
| 76 | { |
| 77 | assert(std::isnan(r.real())); |
| 78 | assert(std::isnan(r.imag())); |
| 79 | } |
| 80 | else if (std::isinf(testcases[i].real()) && testcases[i].real() < 0 && std::isfinite(testcases[i].imag())) |
| 81 | { |
| 82 | assert(std::isinf(r.real())); |
| 83 | assert(r.real() > 0); |
| 84 | if (std::signbit(testcases[i].imag())) |
| 85 | is_about(x: r.imag(), y: -pi); |
| 86 | else |
| 87 | is_about(x: r.imag(), y: pi); |
| 88 | } |
| 89 | else if (std::isinf(testcases[i].real()) && testcases[i].real() > 0 && std::isfinite(testcases[i].imag())) |
| 90 | { |
| 91 | assert(std::isinf(r.real())); |
| 92 | assert(r.real() > 0); |
| 93 | assert(r.imag() == 0); |
| 94 | assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); |
| 95 | } |
| 96 | else if (std::isinf(testcases[i].real()) && testcases[i].real() < 0 && std::isinf(testcases[i].imag())) |
| 97 | { |
| 98 | assert(std::isinf(r.real())); |
| 99 | assert(r.real() > 0); |
| 100 | if (std::signbit(testcases[i].imag())) |
| 101 | is_about(x: r.imag(), y: -0.75 * pi); |
| 102 | else |
| 103 | is_about(x: r.imag(), y: 0.75 * pi); |
| 104 | } |
| 105 | else if (std::isinf(testcases[i].real()) && testcases[i].real() > 0 && std::isinf(testcases[i].imag())) |
| 106 | { |
| 107 | assert(std::isinf(r.real())); |
| 108 | assert(r.real() > 0); |
| 109 | if (std::signbit(testcases[i].imag())) |
| 110 | is_about(x: r.imag(), y: -0.25 * pi); |
| 111 | else |
| 112 | is_about(x: r.imag(), y: 0.25 * pi); |
| 113 | } |
| 114 | else if (std::isinf(testcases[i].real()) && std::isnan(testcases[i].imag())) |
| 115 | { |
| 116 | assert(std::isinf(r.real())); |
| 117 | assert(r.real() > 0); |
| 118 | assert(std::isnan(r.imag())); |
| 119 | } |
| 120 | else if (std::isnan(testcases[i].real()) && std::isfinite(testcases[i].imag())) |
| 121 | { |
| 122 | assert(std::isnan(r.real())); |
| 123 | assert(std::isnan(r.imag())); |
| 124 | } |
| 125 | else if (std::isnan(testcases[i].real()) && std::isinf(testcases[i].imag())) |
| 126 | { |
| 127 | assert(std::isinf(r.real())); |
| 128 | assert(r.real() > 0); |
| 129 | assert(std::isnan(r.imag())); |
| 130 | } |
| 131 | else if (std::isnan(testcases[i].real()) && std::isnan(testcases[i].imag())) |
| 132 | { |
| 133 | assert(std::isnan(r.real())); |
| 134 | assert(std::isnan(r.imag())); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | assert(!std::signbit(r.real())); |
| 139 | assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | int main(int, char**) |
| 145 | { |
| 146 | test<float>(); |
| 147 | test<double>(); |
| 148 | test<long double>(); |
| 149 | test_edges(); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |