| 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<Arithmetic T> |
| 12 |
// T |
| 13 |
// norm(T x); // constexpr in C++20 |
| 14 |
|
| 15 |
#include <complex> |
| 16 |
#include <type_traits> |
| 17 |
#include <cassert> |
| 18 |
|
| 19 |
#include "test_macros.h" |
| 20 |
#include "../cases.h" |
| 21 |
|
| 22 |
template <class T> |
| 23 |
TEST_CONSTEXPR_CXX20 |
| 24 |
void |
| 25 |
test(T x, typename std::enable_if<std::is_integral<T>::value>::type* = 0) |
| 26 |
{ |
| 27 |
static_assert((std::is_same<decltype(std::norm(x)), double>::value), ""); |
| 28 |
assert(std::norm(x) == norm(std::complex<double>(static_cast<double>(x), 0))); |
| 29 |
} |
| 30 |
|
| 31 |
template <class T> |
| 32 |
TEST_CONSTEXPR_CXX20 |
| 33 |
void |
| 34 |
test(T x, typename std::enable_if<!std::is_integral<T>::value>::type* = 0) |
| 35 |
{ |
| 36 |
static_assert((std::is_same<decltype(std::norm(x)), T>::value), ""); |
| 37 |
assert(std::norm(x) == norm(std::complex<T>(x, 0))); |
| 38 |
} |
| 39 |
|
| 40 |
template <class T> |
| 41 |
TEST_CONSTEXPR_CXX20 |
| 42 |
bool |
| 43 |
test() |
| 44 |
{ |
| 45 |
test<T>(0); |
| 46 |
test<T>(1); |
| 47 |
test<T>(10); |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
int main(int, char**) |
| 52 |
{ |
| 53 |
test<float>(); |
| 54 |
test<double>(); |
| 55 |
test<long double>(); |
| 56 |
test<int>(); |
| 57 |
test<unsigned>(); |
| 58 |
test<long long>(); |
| 59 |
|
| 60 |
#if TEST_STD_VER >= 20 |
| 61 |
static_assert(test<float>()); |
| 62 |
static_assert(test<double>()); |
| 63 |
static_assert(test<long double>()); |
| 64 |
static_assert(test<int>()); |
| 65 |
static_assert(test<unsigned>()); |
| 66 |
static_assert(test<long long>()); |
| 67 |
#endif |
| 68 |
|
| 69 |
return 0; |
| 70 |
} |