| 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 | // REQUIRES: std-at-least-c++26 |
| 10 | |
| 11 | // <numeric> |
| 12 | |
| 13 | // template<class T> |
| 14 | // constexpr T div_sat(T x, T y) noexcept; // freestanding |
| 15 | |
| 16 | #include <concepts> |
| 17 | #include <numeric> |
| 18 | #include <type_traits> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | // Constraints |
| 23 | |
| 24 | template <typename T, typename U> |
| 25 | concept CanDo = requires(T x, U y) { |
| 26 | { std::div_sat(x, y) } -> std::same_as<T>; |
| 27 | }; |
| 28 | |
| 29 | template <typename T, typename U> |
| 30 | constexpr void test_constraint_success() { |
| 31 | static_assert(CanDo<T, T>); |
| 32 | static_assert(!CanDo<U, T>); |
| 33 | static_assert(!CanDo<T, U>); |
| 34 | } |
| 35 | |
| 36 | template <typename T> |
| 37 | constexpr void test_constraint_fail() { |
| 38 | using I = int; |
| 39 | static_assert(!CanDo<T, T>); |
| 40 | static_assert(!CanDo<I, T>); |
| 41 | static_assert(!CanDo<T, I>); |
| 42 | } |
| 43 | |
| 44 | constexpr void test() { |
| 45 | // Contraint success - Signed |
| 46 | using SI = long long int; |
| 47 | test_constraint_success<signed char, SI>(); |
| 48 | test_constraint_success<short int, SI>(); |
| 49 | test_constraint_success<signed char, SI>(); |
| 50 | test_constraint_success<short int, SI>(); |
| 51 | test_constraint_success<int, SI>(); |
| 52 | test_constraint_success<long int, SI>(); |
| 53 | test_constraint_success<long long int, int>(); |
| 54 | #ifndef TEST_HAS_NO_INT128 |
| 55 | test_constraint_success<__int128_t, SI>(); |
| 56 | #endif |
| 57 | // Contraint success - Unsigned |
| 58 | using UI = unsigned long long int; |
| 59 | test_constraint_success<unsigned char, UI>(); |
| 60 | test_constraint_success<unsigned short int, UI>(); |
| 61 | test_constraint_success<unsigned int, UI>(); |
| 62 | test_constraint_success<unsigned long int, UI>(); |
| 63 | test_constraint_success<unsigned long long int, unsigned int>(); |
| 64 | #ifndef TEST_HAS_NO_INT128 |
| 65 | test_constraint_success<__uint128_t, UI>(); |
| 66 | #endif |
| 67 | |
| 68 | // Contraint failure |
| 69 | test_constraint_fail<bool>(); |
| 70 | test_constraint_fail<char>(); |
| 71 | #ifndef TEST_HAS_NO_INT128 |
| 72 | test_constraint_fail<wchar_t>(); |
| 73 | #endif |
| 74 | test_constraint_fail<char8_t>(); |
| 75 | test_constraint_fail<char16_t>(); |
| 76 | test_constraint_fail<char32_t>(); |
| 77 | test_constraint_fail<float>(); |
| 78 | test_constraint_fail<double>(); |
| 79 | test_constraint_fail<long double>(); |
| 80 | } |
| 81 | |
| 82 | // A function call expression that violates the precondition in the Preconditions: element is not a core constant expression (7.7 [expr.const]). |
| 83 | |
| 84 | template <auto N> |
| 85 | using QuotT = std::integral_constant<decltype(N), std::div_sat(N, N)>; |
| 86 | |
| 87 | template <auto N> |
| 88 | QuotT<N> div_by_zero(); |
| 89 | |
| 90 | template <auto N> |
| 91 | concept CanDivByZero = requires { div_by_zero<N>(); }; |
| 92 | |
| 93 | static_assert(!CanDivByZero<static_cast<signed char>(0)>); |
| 94 | static_assert(!CanDivByZero<static_cast<short int>(0)>); |
| 95 | static_assert(!CanDivByZero<0>); |
| 96 | static_assert(!CanDivByZero<0L>); |
| 97 | static_assert(!CanDivByZero<0LL>); |
| 98 | #ifndef TEST_HAS_NO_INT128 |
| 99 | static_assert(!CanDivByZero<static_cast<__int128_t>(0)>); |
| 100 | #endif |
| 101 | static_assert(!CanDivByZero<static_cast<unsigned char>(0)>); |
| 102 | static_assert(!CanDivByZero<static_cast<unsigned short int>(0)>); |
| 103 | static_assert(!CanDivByZero<0U>); |
| 104 | static_assert(!CanDivByZero<0UL>); |
| 105 | static_assert(!CanDivByZero<0ULL>); |
| 106 | #ifndef TEST_HAS_NO_INT128 |
| 107 | static_assert(!CanDivByZero<static_cast<__uint128_t>(0)>); |
| 108 | #endif |
| 109 | |