| 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 | // tanh(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(tanh(c) == x); |
| 26 | } |
| 27 | |
| 28 | template <class T> |
| 29 | void |
| 30 | test() |
| 31 | { |
| 32 | test(std::complex<T>(0, 0), std::complex<T>(0, 0)); |
| 33 | } |
| 34 | |
| 35 | void test_edges() |
| 36 | { |
| 37 | const unsigned N = sizeof(testcases) / sizeof(testcases[0]); |
| 38 | for (unsigned i = 0; i < N; ++i) |
| 39 | { |
| 40 | std::complex<double> r = tanh(testcases[i]); |
| 41 | if (testcases[i].real() == 0 && testcases[i].imag() == 0) |
| 42 | { |
| 43 | assert(r.real() == 0); |
| 44 | assert(std::signbit(r.real()) == std::signbit(testcases[i].real())); |
| 45 | assert(r.imag() == 0); |
| 46 | assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); |
| 47 | } |
| 48 | else if (std::isfinite(testcases[i].real()) && std::isinf(testcases[i].imag())) |
| 49 | { |
| 50 | assert(std::isnan(r.real())); |
| 51 | assert(std::isnan(r.imag())); |
| 52 | } |
| 53 | else if (std::isfinite(testcases[i].real()) && std::isnan(testcases[i].imag())) |
| 54 | { |
| 55 | assert(std::isnan(r.real())); |
| 56 | assert(std::isnan(r.imag())); |
| 57 | } |
| 58 | else if (std::isinf(testcases[i].real()) && std::isfinite(testcases[i].imag())) |
| 59 | { |
| 60 | assert(r.real() == (testcases[i].real() > 0 ? 1 : -1)); |
| 61 | assert(r.imag() == 0); |
| 62 | assert(std::signbit(r.imag()) == std::signbit(sin(2 * testcases[i].imag()))); |
| 63 | } |
| 64 | else if (std::isinf(testcases[i].real()) && std::isinf(testcases[i].imag())) |
| 65 | { |
| 66 | assert(r.real() == (testcases[i].real() > 0 ? 1 : -1)); |
| 67 | assert(r.imag() == 0); |
| 68 | } |
| 69 | else if (std::isinf(testcases[i].real()) && std::isnan(testcases[i].imag())) |
| 70 | { |
| 71 | assert(r.real() == (testcases[i].real() > 0 ? 1 : -1)); |
| 72 | assert(r.imag() == 0); |
| 73 | } |
| 74 | else if (std::isnan(testcases[i].real()) && testcases[i].imag() == 0) |
| 75 | { |
| 76 | assert(std::isnan(r.real())); |
| 77 | assert(r.imag() == 0); |
| 78 | assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); |
| 79 | } |
| 80 | else if (std::isnan(testcases[i].real()) && std::isfinite(testcases[i].imag())) |
| 81 | { |
| 82 | assert(std::isnan(r.real())); |
| 83 | assert(std::isnan(r.imag())); |
| 84 | } |
| 85 | else if (std::isnan(testcases[i].real()) && std::isnan(testcases[i].imag())) |
| 86 | { |
| 87 | assert(std::isnan(r.real())); |
| 88 | assert(std::isnan(r.imag())); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | int main(int, char**) |
| 94 | { |
| 95 | test<float>(); |
| 96 | test<double>(); |
| 97 | test<long double>(); |
| 98 | test_edges(); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |