1bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//===----------------------------------------------------------------------===//
2bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//
3f5256e16dfc425c1d466f6308d4026d529ce9e0bHoward Hinnant//                     The LLVM Compiler Infrastructure
4bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//
5b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// This file is dual licensed under the MIT and the University of Illinois Open
6b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// Source Licenses. See LICENSE.TXT for details.
7bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//
8bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//===----------------------------------------------------------------------===//
9bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
10bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant// <complex>
11bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
12bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant// template<class T>
13bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//   complex<T>
14bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//   polar(const T& rho, const T& theta = 0);
15bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
16bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include <complex>
17bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include <cassert>
18bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
19bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include "../cases.h"
20bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
21bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttemplate <class T>
22bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnantvoid
23bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttest(const T& rho, std::complex<T> x)
24bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant{
25bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    assert(std::polar(rho) == x);
26bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant}
27bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
28bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttemplate <class T>
29bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnantvoid
30bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttest(const T& rho, const T& theta, std::complex<T> x)
31bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant{
32bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    assert(std::polar(rho, theta) == x);
33bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant}
34bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
35bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttemplate <class T>
36bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnantvoid
37bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttest()
38bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant{
39bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(T(0), std::complex<T>(0, 0));
40bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(T(1), std::complex<T>(1, 0));
41bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(T(100), std::complex<T>(100, 0));
42bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(T(0), T(0), std::complex<T>(0, 0));
43bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(T(1), T(0), std::complex<T>(1, 0));
44bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(T(100), T(0), std::complex<T>(100, 0));
45bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant}
46bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
47bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnantvoid test_edges()
48bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant{
49bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    const unsigned N = sizeof(x) / sizeof(x[0]);
50bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    for (unsigned i = 0; i < N; ++i)
51bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    {
52bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant        double r = real(x[i]);
53bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant        double theta = imag(x[i]);
54bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant        std::complex<double> z = std::polar(r, theta);
55bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant        switch (classify(r))
56bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant        {
57bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant        case zero:
58bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            if (std::signbit(r) || classify(theta) == inf || classify(theta) == NaN)
59bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            {
60bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                int c = classify(z);
61bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                assert(c == NaN || c == non_zero_nan);
62bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            }
63bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            else
64bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            {
65bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                assert(z == std::complex<double>());
66bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            }
67bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            break;
68bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant        case non_zero:
69bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            if (std::signbit(r) || classify(theta) == inf || classify(theta) == NaN)
70bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            {
71bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                int c = classify(z);
72bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                assert(c == NaN || c == non_zero_nan);
73bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            }
74bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            else
75bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            {
76bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                is_about(std::abs(z), r);
77bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            }
78bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            break;
79bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant        case inf:
80bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            if (r < 0)
81bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            {
82bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                int c = classify(z);
83bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                assert(c == NaN || c == non_zero_nan);
84bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            }
85bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            else
86bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            {
87bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                assert(classify(z) == inf);
88bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                if (classify(theta) != NaN && classify(theta) != inf)
89bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                {
90bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                    assert(classify(real(z)) != NaN);
91bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                    assert(classify(imag(z)) != NaN);
92bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                }
93bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            }
94bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            break;
95bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant        case NaN:
96bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant        case non_zero_nan:
97bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            {
98bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                int c = classify(z);
99bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant                assert(c == NaN || c == non_zero_nan);
100bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            }
101bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant            break;
102bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant        }
103bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    }
104bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant}
105bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
106bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnantint main()
107bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant{
108bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test<float>();
109bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test<double>();
110bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test<long double>();
111bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test_edges();
112bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant}
113