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