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