78 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// REQUIRES: std-at-least-c++2610 11// <numeric>12 13// template<class T>14// constexpr T add_sat(T x, T y) noexcept; // freestanding15 16#include <concepts>17#include <numeric>18 19#include "test_macros.h"20 21template <typename T, typename U>22concept CanDo = requires(T x, U y) {23 { std::add_sat(x, y) } -> std::same_as<T>;24};25 26template <typename T, typename U>27constexpr void test_constraint_success() {28 static_assert(CanDo<T, T>);29 static_assert(!CanDo<U, T>);30 static_assert(!CanDo<T, U>);31}32 33template <typename T>34constexpr 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 41constexpr void test() {42 // Contraint success - Signed43 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_INT12852 test_constraint_success<__int128_t, SI>();53#endif54 // Contraint success - Unsigned55 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_INT12862 test_constraint_success<__uint128_t, UI>();63#endif64 65 // Contraint failure66 test_constraint_fail<bool>();67 test_constraint_fail<char>();68#ifndef TEST_HAS_NO_WIDE_CHARACTERS69 test_constraint_fail<wchar_t>();70#endif71 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