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