| 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 "test_macros.h" |
| 19 | #include "../cases.h" |
| 20 | |
| 21 | template <class T> |
| 22 | void |
| 23 | test(const T& rho, std::complex<T> x) |
| 24 | { |
| 25 | assert(std::polar(rho) == x); |
| 26 | } |
| 27 | |
| 28 | template <class T> |
| 29 | void |
| 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 |
| 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 | |
| 47 | void test_edges() |
| 48 | { |
| 49 | const unsigned N = sizeof(testcases) / sizeof(testcases[0]); |
| 50 | for (unsigned i = 0; i < N; ++i) |
| 51 | { |
| 52 | double r = real(testcases[i]); |
| 53 | double theta = imag(testcases[i]); |
| 54 | std::complex<double> z = std::polar(rho: r, theta: theta); |
| 55 | switch (classify(x: r)) |
| 56 | { |
| 57 | case zero: |
| 58 | if (std::signbit(x: r) || classify(x: theta) == inf || classify(x: 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(x: r) || classify(x: theta) == inf || classify(x: theta) == NaN) |
| 70 | { |
| 71 | int c = classify(z); |
| 72 | assert(c == NaN || c == non_zero_nan); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | is_about(x: std::abs(z: z), y: 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(x: theta) != NaN && classify(x: 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 | |
| 106 | int main(int, char**) |
| 107 | { |
| 108 | test<float>(); |
| 109 | test<double>(); |
| 110 | test<long double>(); |
| 111 | test_edges(); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |