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 //   polar(const T& rho, const T& theta = 0);
15 
16 #include <complex>
17 #include <cassert>
18 
19 #include "../cases.h"
20 
21 template <class T>
22 void
test(const T & rho,std::complex<T> x)23 test(const T& rho, std::complex<T> x)
24 {
25     assert(std::polar(rho) == x);
26 }
27 
28 template <class T>
29 void
test(const T & rho,const T & theta,std::complex<T> x)30 test(const T& rho, const T& theta, std::complex<T> x)
31 {
32     assert(std::polar(rho, theta) == x);
33 }
34 
35 template <class T>
36 void
test()37 test()
38 {
39     test(T(0), std::complex<T>(0, 0));
40     test(T(1), std::complex<T>(1, 0));
41     test(T(100), std::complex<T>(100, 0));
42     test(T(0), T(0), std::complex<T>(0, 0));
43     test(T(1), T(0), std::complex<T>(1, 0));
44     test(T(100), T(0), std::complex<T>(100, 0));
45 }
46 
test_edges()47 void test_edges()
48 {
49     const unsigned N = sizeof(x) / sizeof(x[0]);
50     for (unsigned i = 0; i < N; ++i)
51     {
52         double r = real(x[i]);
53         double theta = imag(x[i]);
54         std::complex<double> z = std::polar(r, theta);
55         switch (classify(r))
56         {
57         case zero:
58             if (std::signbit(r) || classify(theta) == inf || classify(theta) == NaN)
59             {
60                 int c = classify(z);
61                 assert(c == NaN || c == non_zero_nan);
62             }
63             else
64             {
65                 assert(z == std::complex<double>());
66             }
67             break;
68         case non_zero:
69             if (std::signbit(r) || classify(theta) == inf || classify(theta) == NaN)
70             {
71                 int c = classify(z);
72                 assert(c == NaN || c == non_zero_nan);
73             }
74             else
75             {
76                 is_about(std::abs(z), r);
77             }
78             break;
79         case inf:
80             if (r < 0)
81             {
82                 int c = classify(z);
83                 assert(c == NaN || c == non_zero_nan);
84             }
85             else
86             {
87                 assert(classify(z) == inf);
88                 if (classify(theta) != NaN && classify(theta) != inf)
89                 {
90                     assert(classify(real(z)) != NaN);
91                     assert(classify(imag(z)) != NaN);
92                 }
93             }
94             break;
95         case NaN:
96         case non_zero_nan:
97             {
98                 int c = classify(z);
99                 assert(c == NaN || c == non_zero_nan);
100             }
101             break;
102         }
103     }
104 }
105 
main()106 int main()
107 {
108     test<float>();
109     test<double>();
110     test<long double>();
111     test_edges();
112 }
113