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// T
13// abs(const complex<T>& x);
14
15#include <complex>
16#include <cassert>
17
18#include "test_macros.h"
19#include "../cases.h"
20
21template <class T>
22void
23test()
24{
25 std::complex<T> z(3, 4);
26 assert(abs(z) == 5);
27}
28
29void test_edges()
30{
31 const unsigned N = sizeof(testcases) / sizeof(testcases[0]);
32 for (unsigned i = 0; i < N; ++i)
33 {
34 double r = abs(testcases[i]);
35 switch (classify(testcases[i]))
36 {
37 case zero:
38 assert(r == 0);
39 assert(!std::signbit(r));
40 break;
41 case non_zero:
42 assert(std::isfinite(r) && r > 0);
43 break;
44 case inf:
45 assert(std::isinf(r) && r > 0);
46 break;
47 case NaN:
48 assert(std::isnan(r));
49 break;
50 case non_zero_nan:
51 assert(std::isnan(r));
52 break;
53 }
54 }
55}
56
57int main(int, char**)
58{
59 test<float>();
60 test<double>();
61 test<long double>();
62 test_edges();
63
64 return 0;
65}
66

source code of libcxx/test/std/numerics/complex.number/complex.value.ops/abs.pass.cpp