15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <complex>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // template<class T>
125a83710eSEric Fiselier //   T
135a83710eSEric Fiselier //   arg(const complex<T>& x);
145a83710eSEric Fiselier 
155a83710eSEric Fiselier #include <complex>
165a83710eSEric Fiselier #include <cassert>
175a83710eSEric Fiselier 
18*7fc6a556SMarshall Clow #include "test_macros.h"
195a83710eSEric Fiselier #include "../cases.h"
205a83710eSEric Fiselier 
215a83710eSEric Fiselier template <class T>
225a83710eSEric Fiselier void
test()235a83710eSEric Fiselier test()
245a83710eSEric Fiselier {
255a83710eSEric Fiselier     std::complex<T> z(1, 0);
265a83710eSEric Fiselier     assert(arg(z) == 0);
275a83710eSEric Fiselier }
285a83710eSEric Fiselier 
test_edges()295a83710eSEric Fiselier void test_edges()
305a83710eSEric Fiselier {
315a83710eSEric Fiselier     const double pi = std::atan2(+0., -0.);
32217cf69bSMarshall Clow     const unsigned N = sizeof(testcases) / sizeof(testcases[0]);
335a83710eSEric Fiselier     for (unsigned i = 0; i < N; ++i)
345a83710eSEric Fiselier     {
35217cf69bSMarshall Clow         double r = arg(testcases[i]);
36217cf69bSMarshall Clow         if (std::isnan(testcases[i].real()) || std::isnan(testcases[i].imag()))
375a83710eSEric Fiselier             assert(std::isnan(r));
385a83710eSEric Fiselier         else
395a83710eSEric Fiselier         {
40217cf69bSMarshall Clow             switch (classify(testcases[i]))
415a83710eSEric Fiselier             {
425a83710eSEric Fiselier             case zero:
43217cf69bSMarshall Clow                 if (std::signbit(testcases[i].real()))
445a83710eSEric Fiselier                 {
45217cf69bSMarshall Clow                     if (std::signbit(testcases[i].imag()))
465a83710eSEric Fiselier                         is_about(r, -pi);
475a83710eSEric Fiselier                     else
485a83710eSEric Fiselier                         is_about(r, pi);
495a83710eSEric Fiselier                 }
505a83710eSEric Fiselier                 else
515a83710eSEric Fiselier                 {
52217cf69bSMarshall Clow                     assert(std::signbit(testcases[i].imag()) == std::signbit(r));
535a83710eSEric Fiselier                 }
545a83710eSEric Fiselier                 break;
555a83710eSEric Fiselier             case non_zero:
56217cf69bSMarshall Clow                 if (testcases[i].real() == 0)
575a83710eSEric Fiselier                 {
58217cf69bSMarshall Clow                     if (testcases[i].imag() < 0)
595a83710eSEric Fiselier                         is_about(r, -pi/2);
605a83710eSEric Fiselier                     else
615a83710eSEric Fiselier                         is_about(r, pi/2);
625a83710eSEric Fiselier                 }
63217cf69bSMarshall Clow                 else if (testcases[i].imag() == 0)
645a83710eSEric Fiselier                 {
65217cf69bSMarshall Clow                     if (testcases[i].real() < 0)
665a83710eSEric Fiselier                     {
67217cf69bSMarshall Clow                         if (std::signbit(testcases[i].imag()))
685a83710eSEric Fiselier                             is_about(r, -pi);
695a83710eSEric Fiselier                         else
705a83710eSEric Fiselier                             is_about(r, pi);
715a83710eSEric Fiselier                     }
725a83710eSEric Fiselier                     else
735a83710eSEric Fiselier                     {
745a83710eSEric Fiselier                         assert(r == 0);
75217cf69bSMarshall Clow                         assert(std::signbit(testcases[i].imag()) == std::signbit(r));
765a83710eSEric Fiselier                     }
775a83710eSEric Fiselier                 }
78217cf69bSMarshall Clow                 else if (testcases[i].imag() > 0)
795a83710eSEric Fiselier                     assert(r > 0);
805a83710eSEric Fiselier                 else
815a83710eSEric Fiselier                     assert(r < 0);
825a83710eSEric Fiselier                 break;
835a83710eSEric Fiselier             case inf:
84217cf69bSMarshall Clow                 if (std::isinf(testcases[i].real()) && std::isinf(testcases[i].imag()))
855a83710eSEric Fiselier                 {
86217cf69bSMarshall Clow                     if (testcases[i].real() < 0)
875a83710eSEric Fiselier                     {
88217cf69bSMarshall Clow                         if (testcases[i].imag() > 0)
895a83710eSEric Fiselier                             is_about(r, 0.75 * pi);
905a83710eSEric Fiselier                         else
915a83710eSEric Fiselier                             is_about(r, -0.75 * pi);
925a83710eSEric Fiselier                     }
935a83710eSEric Fiselier                     else
945a83710eSEric Fiselier                     {
95217cf69bSMarshall Clow                         if (testcases[i].imag() > 0)
965a83710eSEric Fiselier                             is_about(r, 0.25 * pi);
975a83710eSEric Fiselier                         else
985a83710eSEric Fiselier                             is_about(r, -0.25 * pi);
995a83710eSEric Fiselier                     }
1005a83710eSEric Fiselier                 }
101217cf69bSMarshall Clow                 else if (std::isinf(testcases[i].real()))
1025a83710eSEric Fiselier                 {
103217cf69bSMarshall Clow                     if (testcases[i].real() < 0)
1045a83710eSEric Fiselier                     {
105217cf69bSMarshall Clow                         if (std::signbit(testcases[i].imag()))
1065a83710eSEric Fiselier                             is_about(r, -pi);
1075a83710eSEric Fiselier                         else
1085a83710eSEric Fiselier                             is_about(r, pi);
1095a83710eSEric Fiselier                     }
1105a83710eSEric Fiselier                     else
1115a83710eSEric Fiselier                     {
1125a83710eSEric Fiselier                         assert(r == 0);
113217cf69bSMarshall Clow                         assert(std::signbit(r) == std::signbit(testcases[i].imag()));
1145a83710eSEric Fiselier                     }
1155a83710eSEric Fiselier                 }
1165a83710eSEric Fiselier                 else
1175a83710eSEric Fiselier                 {
118217cf69bSMarshall Clow                     if (testcases[i].imag() < 0)
1195a83710eSEric Fiselier                         is_about(r, -pi/2);
1205a83710eSEric Fiselier                     else
1215a83710eSEric Fiselier                         is_about(r, pi/2);
1225a83710eSEric Fiselier                 }
1235a83710eSEric Fiselier                 break;
1245a83710eSEric Fiselier             }
1255a83710eSEric Fiselier         }
1265a83710eSEric Fiselier     }
1275a83710eSEric Fiselier }
1285a83710eSEric Fiselier 
main(int,char **)1292df59c50SJF Bastien int main(int, char**)
1305a83710eSEric Fiselier {
1315a83710eSEric Fiselier     test<float>();
1325a83710eSEric Fiselier     test<double>();
1335a83710eSEric Fiselier     test<long double>();
1345a83710eSEric Fiselier     test_edges();
1352df59c50SJF Bastien 
1362df59c50SJF Bastien   return 0;
1375a83710eSEric Fiselier }
138