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 //   proj(const complex<T>& x);
15 
16 #include <complex>
17 #include <cassert>
18 
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(x) / sizeof(x[0]);
41     for (unsigned i = 0; i < N; ++i)
42     {
43         std::complex<double> r = proj(x[i]);
44         switch (classify(x[i]))
45         {
46         case zero:
47         case non_zero:
48             assert(r == x[i]);
49             assert(std::signbit(real(r)) == std::signbit(real(x[i])));
50             assert(std::signbit(imag(r)) == std::signbit(imag(x[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(x[i])));
56             break;
57         case NaN:
58         case non_zero_nan:
59             assert(classify(r) == classify(x[i]));
60             break;
61         }
62     }
63 }
64 
main()65 int main()
66 {
67     test<float>();
68     test<double>();
69     test<long double>();
70     test_edges();
71 }
72