|
| 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 | +// bool signbit(floating-point-type x); // constexpr since C++23 |
| 10 | + |
| 11 | +// We don't control the implementation on windows |
| 12 | +// UNSUPPORTED: windows |
| 13 | + |
| 14 | +#include <cassert> |
| 15 | +#include <cmath> |
| 16 | +#include <limits> |
| 17 | + |
| 18 | +#include "test_macros.h" |
| 19 | +#include "type_algorithms.h" |
| 20 | + |
| 21 | +struct TestFloat { |
| 22 | + template <class T> |
| 23 | + static TEST_CONSTEXPR_CXX23 bool test() { |
| 24 | + assert(!std::signbit(T(0))); |
| 25 | + assert(std::signbit(-T(0))); |
| 26 | + assert(std::signbit(std::numeric_limits<T>::lowest())); |
| 27 | + assert(!std::signbit(std::numeric_limits<T>::min())); |
| 28 | + assert(!std::signbit(std::numeric_limits<T>::denorm_min())); |
| 29 | + assert(!std::signbit(std::numeric_limits<T>::max())); |
| 30 | + assert(!std::signbit(std::numeric_limits<T>::infinity())); |
| 31 | + assert(std::signbit(-std::numeric_limits<T>::infinity())); |
| 32 | + assert(!std::signbit(std::numeric_limits<T>::quiet_NaN())); |
| 33 | + assert(!std::signbit(std::numeric_limits<T>::signaling_NaN())); |
| 34 | + |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + template <class T> |
| 39 | + TEST_CONSTEXPR_CXX23 void operator()() { |
| 40 | + test<T>(); |
| 41 | +#if TEST_STD_VER >= 23 |
| 42 | + static_assert(test<T>()); |
| 43 | +#endif |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +struct TestUnsignedIntAndChar { |
| 48 | + template <class T> |
| 49 | + static TEST_CONSTEXPR_CXX23 bool test() { |
| 50 | + assert(!std::signbit(std::numeric_limits<T>::max())); |
| 51 | + assert(!std::signbit(std::numeric_limits<T>::lowest())); |
| 52 | + assert(!std::signbit(T(0))); |
| 53 | + |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + template <class T> |
| 58 | + TEST_CONSTEXPR_CXX23 void operator()() { |
| 59 | + test<T>(); |
| 60 | +#if TEST_STD_VER >= 23 |
| 61 | + static_assert(test<T>()); |
| 62 | +#endif |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +struct TestSignedIntAndChar { |
| 67 | + template <class T> |
| 68 | + static TEST_CONSTEXPR_CXX23 bool test() { |
| 69 | + assert(!std::signbit(std::numeric_limits<T>::max())); |
| 70 | + assert(std::signbit(std::numeric_limits<T>::lowest())); |
| 71 | + assert(!std::signbit(T(0))); |
| 72 | + |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + template <class T> |
| 77 | + TEST_CONSTEXPR_CXX23 void operator()() { |
| 78 | + test<T>(); |
| 79 | +#if TEST_STD_VER >= 23 |
| 80 | + static_assert(test<T>()); |
| 81 | +#endif |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +template <typename T> |
| 86 | +struct ConvertibleTo { |
| 87 | + operator T() const { return T(); } |
| 88 | +}; |
| 89 | + |
| 90 | +int main(int, char**) { |
| 91 | + types::for_each(types::floating_point_types(), TestFloat()); |
| 92 | + types::for_each(types::concatenate_t<types::unsigned_integer_types, types::type_list<char8_t, char16_t, char32_t>>(), TestUnsignedIntAndChar()); |
| 93 | + types::for_each(types::concatenate_t<types::signed_integer_types, types::type_list<char>>(), TestSignedIntAndChar()); |
| 94 | + |
| 95 | + // Make sure we can call `std::signbit` with convertible types |
| 96 | + { |
| 97 | + assert(!std::signbit(ConvertibleTo<float>())); |
| 98 | + assert(!std::signbit(ConvertibleTo<double>())); |
| 99 | + assert(!std::signbit(ConvertibleTo<long double>())); |
| 100 | + } |
| 101 | + |
| 102 | + return 0; |
| 103 | +} |
0 commit comments