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_equal(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_equal1() { |
38 | constexpr Tuple<T> tup; |
39 | assert(std::cmp_equal(T(0), T(0))); |
40 | assert(std::cmp_equal(T(10), T(10))); |
41 | assert(std::cmp_equal(tup.min, tup.min)); |
42 | assert(std::cmp_equal(tup.max, tup.max)); |
43 | assert(!std::cmp_equal(T(0), T(1))); |
44 | assert(!std::cmp_equal(T(1), T(0))); |
45 | assert(!std::cmp_equal(T(5), T(10))); |
46 | assert(!std::cmp_equal(T(10), T(5))); |
47 | assert(!std::cmp_equal(tup.min, tup.max)); |
48 | assert(!std::cmp_equal(tup.max, tup.min)); |
49 | assert(!std::cmp_equal(1, tup.max)); |
50 | assert(!std::cmp_equal(tup.max, 1)); |
51 | assert(!std::cmp_equal(1, tup.min)); |
52 | assert(!std::cmp_equal(tup.min, 1)); |
53 | assert(std::cmp_equal(T(-5), T(-5))); |
54 | assert(!std::cmp_equal(-2, tup.min)); |
55 | assert(!std::cmp_equal(tup.min, -2)); |
56 | assert(!std::cmp_equal(-2, tup.max)); |
57 | assert(!std::cmp_equal(tup.max, -2)); |
58 | } |
59 | |
60 | template <typename T, typename U> |
61 | constexpr void test_cmp_equal2() { |
62 | constexpr Tuple<T> ttup; |
63 | constexpr Tuple<U> utup; |
64 | assert(std::cmp_equal(T(0), U(0))); |
65 | assert(std::cmp_equal(T(10), U(10))); |
66 | assert(!std::cmp_equal(T(0), U(1))); |
67 | assert(!std::cmp_equal(T(1), U(0))); |
68 | assert(!std::cmp_equal(T(5), U(10))); |
69 | assert(!std::cmp_equal(T(10), U(5))); |
70 | assert(!std::cmp_equal(ttup.min, utup.max)); |
71 | assert(!std::cmp_equal(utup.min, ttup.max)); |
72 | } |
73 | |
74 | template <class... Ts> |
75 | constexpr void test1(const std::tuple<Ts...>&) { |
76 | (test_cmp_equal1<Ts>() , ...); |
77 | } |
78 | |
79 | template <class T, class... Us> |
80 | constexpr void test2_impl(const std::tuple<Us...>&) { |
81 | (test_cmp_equal2<T, Us>() , ...); |
82 | } |
83 | |
84 | template <class... Ts, class UTuple> |
85 | constexpr void test2(const std::tuple<Ts...>&, const UTuple& utuple) { |
86 | (test2_impl<Ts>(utuple) , ...); |
87 | } |
88 | |
89 | constexpr bool test() { |
90 | std::tuple< |
91 | #ifndef TEST_HAS_NO_INT128 |
92 | __int128_t, __uint128_t, |
93 | #endif |
94 | unsigned long long, long long, unsigned long, long, unsigned int, int, |
95 | unsigned short, short, unsigned char, signed char> types; |
96 | test1(types); |
97 | test2(types, utuple: types); |
98 | return true; |
99 | } |
100 | |
101 | int main(int, char**) { |
102 | ASSERT_NOEXCEPT(std::cmp_equal(0, 0)); |
103 | test(); |
104 | static_assert(test()); |
105 | return 0; |
106 | } |
107 | |