115 lines · cpp
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-exception6//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#287014 15#include <complex>16#include <cassert>17 18#include "test_macros.h"19#include "../cases.h"20 21template <class T>22void23test(const T& rho, std::complex<T> x)24{25 assert(std::polar(rho) == x);26}27 28template <class T>29void30test(const T& rho, const T& theta, std::complex<T> x)31{32 assert(std::polar(rho, theta) == x);33}34 35template <class T>36void37test()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 47void 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(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 else64 {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 else75 {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 else86 {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 106int main(int, char**)107{108 test<float>();109 test<double>();110 test<long double>();111 test_edges();112 113 return 0;114}115