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 //   proj(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
test(const std::complex<T> & z,std::complex<T> x)23 test(const std::complex<T>& z, std::complex<T> x)
24 {
25     assert(proj(z) == x);
26 }
27 
28 template <class T>
29 void
test()30 test()
31 {
32     test(std::complex<T>(1, 2), std::complex<T>(1, 2));
33     test(std::complex<T>(-1, 2), std::complex<T>(-1, 2));
34     test(std::complex<T>(1, -2), std::complex<T>(1, -2));
35     test(std::complex<T>(-1, -2), std::complex<T>(-1, -2));
36 }
37 
test_edges()38 void test_edges()
39 {
40     const unsigned N = sizeof(testcases) / sizeof(testcases[0]);
41     for (unsigned i = 0; i < N; ++i)
42     {
43         std::complex<double> r = proj(testcases[i]);
44         switch (classify(testcases[i]))
45         {
46         case zero:
47         case non_zero:
48             assert(r == testcases[i]);
49             assert(std::signbit(real(r)) == std::signbit(real(testcases[i])));
50             assert(std::signbit(imag(r)) == std::signbit(imag(testcases[i])));
51             break;
52         case inf:
53             assert(std::isinf(real(r)) && real(r) > 0);
54             assert(imag(r) == 0);
55             assert(std::signbit(imag(r)) == std::signbit(imag(testcases[i])));
56             break;
57         case NaN:
58         case non_zero_nan:
59             assert(classify(r) == classify(testcases[i]));
60             break;
61         }
62     }
63 }
64 
main(int,char **)65 int main(int, char**)
66 {
67     test<float>();
68     test<double>();
69     test<long double>();
70     test_edges();
71 
72   return 0;
73 }
74