109 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 div_sat(T x, T y) noexcept; // freestanding15 16#include <concepts>17#include <numeric>18#include <type_traits>19 20#include "test_macros.h"21 22// Constraints23 24template <typename T, typename U>25concept CanDo = requires(T x, U y) {26 { std::div_sat(x, y) } -> std::same_as<T>;27};28 29template <typename T, typename U>30constexpr void test_constraint_success() {31 static_assert(CanDo<T, T>);32 static_assert(!CanDo<U, T>);33 static_assert(!CanDo<T, U>);34}35 36template <typename T>37constexpr 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 44constexpr void test() {45 // Contraint success - Signed46 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_INT12855 test_constraint_success<__int128_t, SI>();56#endif57 // Contraint success - Unsigned58 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_INT12865 test_constraint_success<__uint128_t, UI>();66#endif67 68 // Contraint failure69 test_constraint_fail<bool>();70 test_constraint_fail<char>();71#ifndef TEST_HAS_NO_INT12872 test_constraint_fail<wchar_t>();73#endif74 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 84template <auto N>85using QuotT = std::integral_constant<decltype(N), std::div_sat(N, N)>;86 87template <auto N>88QuotT<N> div_by_zero();89 90template <auto N>91concept CanDivByZero = requires { div_by_zero<N>(); };92 93static_assert(!CanDivByZero<static_cast<signed char>(0)>);94static_assert(!CanDivByZero<static_cast<short int>(0)>);95static_assert(!CanDivByZero<0>);96static_assert(!CanDivByZero<0L>);97static_assert(!CanDivByZero<0LL>);98#ifndef TEST_HAS_NO_INT12899static_assert(!CanDivByZero<static_cast<__int128_t>(0)>);100#endif101static_assert(!CanDivByZero<static_cast<unsigned char>(0)>);102static_assert(!CanDivByZero<static_cast<unsigned short int>(0)>);103static_assert(!CanDivByZero<0U>);104static_assert(!CanDivByZero<0UL>);105static_assert(!CanDivByZero<0ULL>);106#ifndef TEST_HAS_NO_INT128107static_assert(!CanDivByZero<static_cast<__uint128_t>(0)>);108#endif109