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 // UNSUPPORTED: c++03, c++11, c++14, c++17 10 // UNSUPPORTED: libcpp-no-concepts 11 12 // <utility> 13 14 // template<class T, class U> 15 // constexpr bool cmp_less(T t, U u) noexcept; // C++20 16 17 #include <utility> 18 #include <limits> 19 #include <numeric> 20 #include <tuple> 21 #include <cassert> 22 23 #include "test_macros.h" 24 25 template <typename T> 26 struct Tuple { 27 T min; 28 T max; 29 T mid; 30 constexpr Tuple() { 31 min = std::numeric_limits<T>::min(); 32 max = std::numeric_limits<T>::max(); 33 mid = std::midpoint(min, max); 34 } 35 }; 36 37 template <typename T> 38 constexpr void test_cmp_less1() { 39 constexpr Tuple<T> tup; 40 assert(std::cmp_less(T(0), T(1))); 41 assert(std::cmp_less(T(1), T(2))); 42 assert(std::cmp_less(tup.min, tup.max)); 43 assert(std::cmp_less(tup.min, tup.mid)); 44 assert(std::cmp_less(tup.mid, tup.max)); 45 assert(!std::cmp_less(T(1), T(0))); 46 assert(!std::cmp_less(T(10), T(5))); 47 assert(!std::cmp_less(tup.max, tup.min)); 48 assert(!std::cmp_less(tup.mid, tup.min)); 49 assert(!std::cmp_less(tup.mid, tup.mid)); 50 assert(!std::cmp_less(tup.min, tup.min)); 51 assert(!std::cmp_less(tup.max, tup.max)); 52 assert(!std::cmp_less(tup.max, 1)); 53 assert(!std::cmp_less(1, tup.min)); 54 assert(!std::cmp_less(T(-1), T(-1))); 55 assert(!std::cmp_less(-2, tup.min) == std::is_signed_v<T>); 56 assert(std::cmp_less(tup.min, -2) == std::is_signed_v<T>); 57 assert(std::cmp_less(-2, tup.max)); 58 assert(!std::cmp_less(tup.max, -2)); 59 } 60 61 template <typename T, typename U> 62 constexpr void test_cmp_less2() { 63 assert(std::cmp_less(T(0), U(1))); 64 assert(!std::cmp_less(T(1), U(0))); 65 } 66 67 template <class... Ts> 68 constexpr void test1(const std::tuple<Ts...>&) { 69 (test_cmp_less1<Ts>() , ...); 70 } 71 72 template <class T, class... Us> 73 constexpr void test2_impl(const std::tuple<Us...>&) { 74 (test_cmp_less2<T, Us>() , ...); 75 } 76 77 template <class... Ts, class UTuple> 78 constexpr void test2(const std::tuple<Ts...>&, const UTuple& utuple) { 79 (test2_impl<Ts>(utuple) , ...); 80 } 81 82 constexpr bool test() { 83 std::tuple< 84 #ifndef _LIBCPP_HAS_NO_INT128 85 __int128_t, __uint128_t, 86 #endif 87 unsigned long long, long long, unsigned long, long, unsigned int, int, 88 unsigned short, short, unsigned char, signed char> types; 89 test1(types); 90 test2(types, types); 91 return true; 92 } 93 94 int main(int, char**) { 95 ASSERT_NOEXCEPT(std::cmp_less(0, 1)); 96 test(); 97 static_assert(test()); 98 return 0; 99 } 100