| 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_greater_equal(T t, U u) noexcept; // C++20 |
| 14 | |
| 15 | #include <utility> |
| 16 | #include <cassert> |
| 17 | #include <limits> |
| 18 | #include <numeric> |
| 19 | #include <tuple> |
| 20 | #include <type_traits> |
| 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_greater_equal1() { |
| 38 | constexpr Tuple<T> tup; |
| 39 | assert(!std::cmp_greater_equal(T(0), T(1))); |
| 40 | assert(!std::cmp_greater_equal(T(1), T(2))); |
| 41 | assert(!std::cmp_greater_equal(tup.min, tup.max)); |
| 42 | assert(!std::cmp_greater_equal(tup.min, tup.mid)); |
| 43 | assert(!std::cmp_greater_equal(tup.mid, tup.max)); |
| 44 | assert(std::cmp_greater_equal(T(1), T(0))); |
| 45 | assert(std::cmp_greater_equal(T(10), T(5))); |
| 46 | assert(std::cmp_greater_equal(tup.max, tup.min)); |
| 47 | assert(std::cmp_greater_equal(tup.mid, tup.min)); |
| 48 | assert(std::cmp_greater_equal(tup.mid, tup.mid)); |
| 49 | assert(std::cmp_greater_equal(tup.min, tup.min)); |
| 50 | assert(std::cmp_greater_equal(tup.max, tup.max)); |
| 51 | assert(std::cmp_greater_equal(tup.max, 1)); |
| 52 | assert(std::cmp_greater_equal(1, tup.min)); |
| 53 | assert(std::cmp_greater_equal(T(-1), T(-1))); |
| 54 | assert(std::cmp_greater_equal(-2, tup.min) == std::is_signed_v<T>); |
| 55 | assert(std::cmp_greater_equal(tup.min, -2) == std::is_unsigned_v<T>); |
| 56 | assert(!std::cmp_greater_equal(-2, tup.max)); |
| 57 | assert(std::cmp_greater_equal(tup.max, -2)); |
| 58 | } |
| 59 | |
| 60 | template <typename T, typename U> |
| 61 | constexpr void test_cmp_greater_equal2() { |
| 62 | assert(!std::cmp_greater_equal(T(0), U(1))); |
| 63 | assert(std::cmp_greater_equal(T(1), U(0))); |
| 64 | assert(std::cmp_greater_equal(T(0), U(0))); |
| 65 | assert(std::cmp_greater_equal(T(1), U(1))); |
| 66 | } |
| 67 | |
| 68 | template <class... Ts> |
| 69 | constexpr void test1(const std::tuple<Ts...>&) { |
| 70 | (test_cmp_greater_equal1<Ts>() , ...); |
| 71 | } |
| 72 | |
| 73 | template <class T, class... Us> |
| 74 | constexpr void test2_impl(const std::tuple<Us...>&) { |
| 75 | (test_cmp_greater_equal2<T, Us>() , ...); |
| 76 | } |
| 77 | |
| 78 | template <class... Ts, class UTuple> |
| 79 | constexpr void test2(const std::tuple<Ts...>&, const UTuple& utuple) { |
| 80 | (test2_impl<Ts>(utuple) , ...); |
| 81 | } |
| 82 | |
| 83 | constexpr bool test() { |
| 84 | std::tuple< |
| 85 | #ifndef TEST_HAS_NO_INT128 |
| 86 | __int128_t, __uint128_t, |
| 87 | #endif |
| 88 | unsigned long long, long long, unsigned long, long, unsigned int, int, |
| 89 | unsigned short, short, unsigned char, signed char> types; |
| 90 | test1(types); |
| 91 | test2(types, utuple: types); |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | int main(int, char**) { |
| 96 | ASSERT_NOEXCEPT(std::cmp_greater_equal(1, 0)); |
| 97 | test(); |
| 98 | static_assert(test()); |
| 99 | return 0; |
| 100 | } |
| 101 | |