Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / numerics / complex.number / complex.transcendentals / sqrt.pass.cpp
blob12fd9a2c0440a15c56a93dba01811398ba39556d
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 //===----------------------------------------------------------------------===//
9 // <complex>
11 // template<class T>
12 // complex<T>
13 // sqrt(const complex<T>& x);
15 #include <complex>
16 #include <cassert>
18 #include "test_macros.h"
19 #include "../cases.h"
21 template <class T>
22 void
23 test(const std::complex<T>& c, std::complex<T> x)
25 std::complex<T> a = sqrt(c);
26 is_about(real(a), real(x));
27 assert(std::abs(imag(c)) < 1.e-6);
30 template <class T>
31 void
32 test()
34 test(std::complex<T>(64, 0), std::complex<T>(8, 0));
37 void test_edges()
39 const unsigned N = sizeof(testcases) / sizeof(testcases[0]);
40 for (unsigned i = 0; i < N; ++i)
42 std::complex<double> r = sqrt(testcases[i]);
43 if (testcases[i].real() == 0 && testcases[i].imag() == 0)
45 assert(!std::signbit(r.real()));
46 assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag()));
48 else if (std::isinf(testcases[i].imag()))
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()));
55 else if (std::isfinite(testcases[i].real()) && std::isnan(testcases[i].imag()))
57 assert(std::isnan(r.real()));
58 assert(std::isnan(r.imag()));
60 else if (std::isinf(testcases[i].real()) && testcases[i].real() < 0 && std::isfinite(testcases[i].imag()))
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()));
67 else if (std::isinf(testcases[i].real()) && testcases[i].real() > 0 && std::isfinite(testcases[i].imag()))
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()));
74 else if (std::isinf(testcases[i].real()) && testcases[i].real() < 0 && std::isnan(testcases[i].imag()))
76 assert(std::isnan(r.real()));
77 assert(std::isinf(r.imag()));
79 else if (std::isinf(testcases[i].real()) && testcases[i].real() > 0 && std::isnan(testcases[i].imag()))
81 assert(std::isinf(r.real()));
82 assert(r.real() > 0);
83 assert(std::isnan(r.imag()));
85 else if (std::isnan(testcases[i].real()) && (std::isfinite(testcases[i].imag()) || std::isnan(testcases[i].imag())))
87 assert(std::isnan(r.real()));
88 assert(std::isnan(r.imag()));
90 else if (std::signbit(testcases[i].imag()))
92 assert(!std::signbit(r.real()));
93 assert(std::signbit(r.imag()));
95 else
97 assert(!std::signbit(r.real()));
98 assert(!std::signbit(r.imag()));
103 int main(int, char**)
105 test<float>();
106 test<double>();
107 test<long double>();
108 test_edges();
110 return 0;