Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 9 | // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 10 | |
| 11 | // template <class T> |
| 12 | // constexpr T ceil2(T x) noexcept; |
| 13 | |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 14 | // Returns: The minimal value y such that ispow2(y) is true and y >= x; |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 15 | // if y is not representable as a value of type T, the result is an unspecified value. |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 16 | // Remarks: This function shall not participate in overload resolution unless |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 17 | // T is an unsigned integer type |
| 18 | |
| 19 | #include <bit> |
| 20 | #include <cstdint> |
| 21 | #include <type_traits> |
| 22 | #include <cassert> |
| 23 | |
| 24 | #include "test_macros.h" |
| 25 | |
| 26 | class A{}; |
| 27 | enum E1 : unsigned char { rEd }; |
| 28 | enum class E2 : unsigned char { red }; |
| 29 | |
| 30 | template <typename T> |
| 31 | constexpr bool constexpr_test() |
| 32 | { |
| 33 | return std::ceil2(T(0)) == T(1) |
| 34 | && std::ceil2(T(1)) == T(1) |
| 35 | && std::ceil2(T(2)) == T(2) |
| 36 | && std::ceil2(T(3)) == T(4) |
| 37 | && std::ceil2(T(4)) == T(4) |
| 38 | && std::ceil2(T(5)) == T(8) |
| 39 | && std::ceil2(T(6)) == T(8) |
| 40 | && std::ceil2(T(7)) == T(8) |
| 41 | && std::ceil2(T(8)) == T(8) |
| 42 | && std::ceil2(T(9)) == T(16) |
| 43 | ; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | template <typename T> |
| 48 | void runtime_test() |
| 49 | { |
| 50 | ASSERT_SAME_TYPE(T, decltype(std::ceil2(T(0)))); |
| 51 | LIBCPP_ASSERT_NOEXCEPT( std::ceil2(T(0))); |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 52 | |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 53 | assert( std::ceil2(T(60)) == T( 64)); |
| 54 | assert( std::ceil2(T(61)) == T( 64)); |
| 55 | assert( std::ceil2(T(62)) == T( 64)); |
| 56 | assert( std::ceil2(T(63)) == T( 64)); |
| 57 | assert( std::ceil2(T(64)) == T( 64)); |
| 58 | assert( std::ceil2(T(65)) == T(128)); |
| 59 | assert( std::ceil2(T(66)) == T(128)); |
| 60 | assert( std::ceil2(T(67)) == T(128)); |
| 61 | assert( std::ceil2(T(68)) == T(128)); |
| 62 | assert( std::ceil2(T(69)) == T(128)); |
| 63 | } |
| 64 | |
| 65 | int main() |
| 66 | { |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 67 | |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 68 | { |
| 69 | auto lambda = [](auto x) -> decltype(std::ceil2(x)) {}; |
| 70 | using L = decltype(lambda); |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 71 | |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 72 | static_assert( std::is_invocable_v<L, unsigned char>, ""); |
| 73 | static_assert( std::is_invocable_v<L, unsigned int>, ""); |
| 74 | static_assert( std::is_invocable_v<L, unsigned long>, ""); |
| 75 | static_assert( std::is_invocable_v<L, unsigned long long>, ""); |
| 76 | |
| 77 | static_assert( std::is_invocable_v<L, uint8_t>, ""); |
| 78 | static_assert( std::is_invocable_v<L, uint16_t>, ""); |
| 79 | static_assert( std::is_invocable_v<L, uint32_t>, ""); |
| 80 | static_assert( std::is_invocable_v<L, uint64_t>, ""); |
| 81 | static_assert( std::is_invocable_v<L, size_t>, ""); |
| 82 | |
| 83 | static_assert( std::is_invocable_v<L, uintmax_t>, ""); |
| 84 | static_assert( std::is_invocable_v<L, uintptr_t>, ""); |
| 85 | |
| 86 | |
| 87 | static_assert(!std::is_invocable_v<L, int>, ""); |
| 88 | static_assert(!std::is_invocable_v<L, signed int>, ""); |
| 89 | static_assert(!std::is_invocable_v<L, long>, ""); |
| 90 | static_assert(!std::is_invocable_v<L, long long>, ""); |
| 91 | |
| 92 | static_assert(!std::is_invocable_v<L, int8_t>, ""); |
| 93 | static_assert(!std::is_invocable_v<L, int16_t>, ""); |
| 94 | static_assert(!std::is_invocable_v<L, int32_t>, ""); |
| 95 | static_assert(!std::is_invocable_v<L, int64_t>, ""); |
| 96 | static_assert(!std::is_invocable_v<L, ptrdiff_t>, ""); |
| 97 | |
| 98 | static_assert(!std::is_invocable_v<L, bool>, ""); |
| 99 | static_assert(!std::is_invocable_v<L, signed char>, ""); |
| 100 | static_assert(!std::is_invocable_v<L, char16_t>, ""); |
| 101 | static_assert(!std::is_invocable_v<L, char32_t>, ""); |
| 102 | |
| 103 | #ifndef _LIBCPP_HAS_NO_INT128 |
| 104 | static_assert( std::is_invocable_v<L, __uint128_t>, ""); |
| 105 | static_assert(!std::is_invocable_v<L, __int128_t>, ""); |
| 106 | #endif |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 107 | |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 108 | static_assert(!std::is_invocable_v<L, A>, ""); |
| 109 | static_assert(!std::is_invocable_v<L, E1>, ""); |
| 110 | static_assert(!std::is_invocable_v<L, E2>, ""); |
| 111 | } |
| 112 | |
| 113 | static_assert(constexpr_test<unsigned char>(), ""); |
| 114 | static_assert(constexpr_test<unsigned short>(), ""); |
| 115 | static_assert(constexpr_test<unsigned>(), ""); |
| 116 | static_assert(constexpr_test<unsigned long>(), ""); |
| 117 | static_assert(constexpr_test<unsigned long long>(), ""); |
| 118 | |
| 119 | static_assert(constexpr_test<uint8_t>(), ""); |
| 120 | static_assert(constexpr_test<uint16_t>(), ""); |
| 121 | static_assert(constexpr_test<uint32_t>(), ""); |
| 122 | static_assert(constexpr_test<uint64_t>(), ""); |
| 123 | static_assert(constexpr_test<size_t>(), ""); |
| 124 | static_assert(constexpr_test<uintmax_t>(), ""); |
| 125 | static_assert(constexpr_test<uintptr_t>(), ""); |
| 126 | |
| 127 | #ifndef _LIBCPP_HAS_NO_INT128 |
| 128 | static_assert(constexpr_test<__uint128_t>(), ""); |
| 129 | #endif |
| 130 | |
| 131 | runtime_test<unsigned char>(); |
| 132 | runtime_test<unsigned>(); |
| 133 | runtime_test<unsigned short>(); |
| 134 | runtime_test<unsigned long>(); |
| 135 | runtime_test<unsigned long long>(); |
| 136 | |
| 137 | runtime_test<uint8_t>(); |
| 138 | runtime_test<uint16_t>(); |
| 139 | runtime_test<uint32_t>(); |
| 140 | runtime_test<uint64_t>(); |
| 141 | runtime_test<size_t>(); |
| 142 | runtime_test<uintmax_t>(); |
| 143 | runtime_test<uintptr_t>(); |
| 144 | |
| 145 | #ifndef _LIBCPP_HAS_NO_INT128 |
| 146 | runtime_test<__uint128_t>(); |
| 147 | #endif |
| 148 | } |