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