| 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 sub_sat(T x, T y) noexcept; // freestanding |
| 15 | |
| 16 | #include <concepts> |
| 17 | #include <numeric> |
| 18 | |
| 19 | #include "test_macros.h" |
| 20 | |
| 21 | template <typename T, typename U> |
| 22 | concept CanDo = requires(T x, U y) { |
| 23 | { std::sub_sat(x, y) } -> std::same_as<T>; |
| 24 | }; |
| 25 | |
| 26 | template <typename T, typename U> |
| 27 | constexpr void test_constraint_success() { |
| 28 | static_assert(CanDo<T, T>); |
| 29 | static_assert(!CanDo<U, T>); |
| 30 | static_assert(!CanDo<T, U>); |
| 31 | } |
| 32 | |
| 33 | template <typename T> |
| 34 | constexpr void test_constraint_fail() { |
| 35 | using I = int; |
| 36 | static_assert(!CanDo<T, T>); |
| 37 | static_assert(!CanDo<I, T>); |
| 38 | static_assert(!CanDo<T, I>); |
| 39 | } |
| 40 | |
| 41 | constexpr void test() { |
| 42 | // Contraint success - Signed |
| 43 | using SI = long long int; |
| 44 | test_constraint_success<signed char, SI>(); |
| 45 | test_constraint_success<short int, SI>(); |
| 46 | test_constraint_success<signed char, SI>(); |
| 47 | test_constraint_success<short int, SI>(); |
| 48 | test_constraint_success<int, SI>(); |
| 49 | test_constraint_success<long int, SI>(); |
| 50 | test_constraint_success<long long int, int>(); |
| 51 | #ifndef TEST_HAS_NO_INT128 |
| 52 | test_constraint_success<__int128_t, SI>(); |
| 53 | #endif |
| 54 | // Contraint success - Unsigned |
| 55 | using UI = unsigned long long int; |
| 56 | test_constraint_success<unsigned char, UI>(); |
| 57 | test_constraint_success<unsigned short int, UI>(); |
| 58 | test_constraint_success<unsigned int, UI>(); |
| 59 | test_constraint_success<unsigned long int, UI>(); |
| 60 | test_constraint_success<unsigned long long int, unsigned int>(); |
| 61 | #ifndef TEST_HAS_NO_INT128 |
| 62 | test_constraint_success<__uint128_t, UI>(); |
| 63 | #endif |
| 64 | |
| 65 | // Contraint failure |
| 66 | test_constraint_fail<bool>(); |
| 67 | test_constraint_fail<char>(); |
| 68 | #ifndef TEST_HAS_NO_INT128 |
| 69 | test_constraint_fail<wchar_t>(); |
| 70 | #endif |
| 71 | test_constraint_fail<char8_t>(); |
| 72 | test_constraint_fail<char16_t>(); |
| 73 | test_constraint_fail<char32_t>(); |
| 74 | test_constraint_fail<float>(); |
| 75 | test_constraint_fail<double>(); |
| 76 | test_constraint_fail<long double>(); |
| 77 | } |
| 78 | |