| 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 |
// sqrt(const complex<T>& x); |
| 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 std::complex<T>& c, std::complex<T> x) |
| 24 |
{ |
| 25 |
std::complex<T> a = sqrt(c); |
| 26 |
is_about(real(a), real(x)); |
| 27 |
assert(std::abs(imag(c)) < 1.e-6); |
| 28 |
} |
| 29 |
|
| 30 |
template <class T> |
| 31 |
void |
| 32 |
test() |
| 33 |
{ |
| 34 |
test(std::complex<T>(64, 0), std::complex<T>(8, 0)); |
| 35 |
} |
| 36 |
|
| 37 |
void test_edges() |
| 38 |
{ |
| 39 |
const unsigned N = sizeof(testcases) / sizeof(testcases[0]); |
| 40 |
for (unsigned i = 0; i < N; ++i) |
| 41 |
{ |
| 42 |
std::complex<double> r = sqrt(testcases[i]); |
| 43 |
if (testcases[i].real() == 0 && testcases[i].imag() == 0) |
| 44 |
{ |
| 45 |
assert(!std::signbit(r.real())); |
| 46 |
assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); |
| 47 |
} |
| 48 |
else if (std::isinf(testcases[i].imag())) |
| 49 |
{ |
| 50 |
assert(std::isinf(r.real())); |
| 51 |
assert(r.real() > 0); |
| 52 |
assert(std::isinf(r.imag())); |
| 53 |
assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); |
| 54 |
} |
| 55 |
else if (std::isfinite(testcases[i].real()) && std::isnan(testcases[i].imag())) |
| 56 |
{ |
| 57 |
assert(std::isnan(r.real())); |
| 58 |
assert(std::isnan(r.imag())); |
| 59 |
} |
| 60 |
else if (std::isinf(testcases[i].real()) && testcases[i].real() < 0 && std::isfinite(testcases[i].imag())) |
| 61 |
{ |
| 62 |
assert(r.real() == 0); |
| 63 |
assert(!std::signbit(r.real())); |
| 64 |
assert(std::isinf(r.imag())); |
| 65 |
assert(std::signbit(testcases[i].imag()) == std::signbit(r.imag())); |
| 66 |
} |
| 67 |
else if (std::isinf(testcases[i].real()) && testcases[i].real() > 0 && std::isfinite(testcases[i].imag())) |
| 68 |
{ |
| 69 |
assert(std::isinf(r.real())); |
| 70 |
assert(r.real() > 0); |
| 71 |
assert(r.imag() == 0); |
| 72 |
assert(std::signbit(testcases[i].imag()) == std::signbit(r.imag())); |
| 73 |
} |
| 74 |
else if (std::isinf(testcases[i].real()) && testcases[i].real() < 0 && std::isnan(testcases[i].imag())) |
| 75 |
{ |
| 76 |
assert(std::isnan(r.real())); |
| 77 |
assert(std::isinf(r.imag())); |
| 78 |
} |
| 79 |
else if (std::isinf(testcases[i].real()) && testcases[i].real() > 0 && std::isnan(testcases[i].imag())) |
| 80 |
{ |
| 81 |
assert(std::isinf(r.real())); |
| 82 |
assert(r.real() > 0); |
| 83 |
assert(std::isnan(r.imag())); |
| 84 |
} |
| 85 |
else if (std::isnan(testcases[i].real()) && (std::isfinite(testcases[i].imag()) || std::isnan(testcases[i].imag()))) |
| 86 |
{ |
| 87 |
assert(std::isnan(r.real())); |
| 88 |
assert(std::isnan(r.imag())); |
| 89 |
} |
| 90 |
else if (std::signbit(testcases[i].imag())) |
| 91 |
{ |
| 92 |
assert(!std::signbit(r.real())); |
| 93 |
assert(std::signbit(r.imag())); |
| 94 |
} |
| 95 |
else |
| 96 |
{ |
| 97 |
assert(!std::signbit(r.real())); |
| 98 |
assert(!std::signbit(r.imag())); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
int main(int, char**) |
| 104 |
{ |
| 105 |
test<float>(); |
| 106 |
test<double>(); |
| 107 |
test<long double>(); |
| 108 |
test_edges(); |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |