80 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 R, class T>14// constexpr R saturate_cast(T x) noexcept; // freestanding15 16#include <concepts>17#include <numeric>18 19#include "test_macros.h"20 21template <typename R, typename T>22concept CanDo = requires(T x) {23 { std::saturate_cast<R>(x) } -> std::same_as<R>;24};25 26template <typename R, typename T>27constexpr void test_constraint_success() {28 static_assert(CanDo<R, T>);29 static_assert(CanDo<T, T>);30 static_assert(CanDo<T, R>);31}32 33template <typename T>34constexpr void test_constraint_fail() {35 using I = int;36 using R = T;37 static_assert(!CanDo<R, T>);38 static_assert(!CanDo<T, R>);39 static_assert(!CanDo<I, T>);40 static_assert(!CanDo<T, I>);41}42 43constexpr void test() {44 // Contraint success - Signed45 using SI = long long int;46 test_constraint_success<SI, signed char>();47 test_constraint_success<SI, short int>();48 test_constraint_success<SI, signed char>();49 test_constraint_success<SI, short int>();50 test_constraint_success<SI, int>();51 test_constraint_success<SI, long int>();52 test_constraint_success<int, long long int>();53#ifndef TEST_HAS_NO_INT12854 test_constraint_success<__int128_t, SI>();55#endif56 // Contraint success - Unsigned57 using UI = unsigned long long int;58 test_constraint_success<UI, unsigned char>();59 test_constraint_success<UI, unsigned short int>();60 test_constraint_success<UI, unsigned int>();61 test_constraint_success<UI, unsigned long int>();62 test_constraint_success<unsigned int, unsigned long long int>();63#ifndef TEST_HAS_NO_INT12864 test_constraint_success<UI, __uint128_t>();65#endif66 67 // Contraint failure68 test_constraint_fail<bool>();69 test_constraint_fail<char>();70#ifndef TEST_HAS_NO_WIDE_CHARACTERS71 test_constraint_fail<wchar_t>();72#endif73 test_constraint_fail<char8_t>();74 test_constraint_fail<char16_t>();75 test_constraint_fail<char32_t>();76 test_constraint_fail<float>();77 test_constraint_fail<double>();78 test_constraint_fail<long double>();79}80