| 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 | // <memory> |
| 10 | |
| 11 | // unique_ptr |
| 12 | |
| 13 | // template <class T1, class D1, class T2, class D2> |
| 14 | // bool |
| 15 | // operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 16 | |
| 17 | // template <class T1, class D1, class T2, class D2> |
| 18 | // bool |
| 19 | // operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 20 | |
| 21 | // template <class T1, class D1, class T2, class D2> |
| 22 | // bool |
| 23 | // operator< (const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 24 | |
| 25 | // template <class T1, class D1, class T2, class D2> |
| 26 | // bool |
| 27 | // operator> (const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 28 | |
| 29 | // template <class T1, class D1, class T2, class D2> |
| 30 | // bool |
| 31 | // operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 32 | |
| 33 | // template <class T1, class D1, class T2, class D2> |
| 34 | // bool |
| 35 | // operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 36 | |
| 37 | // template<class T1, class D1, class T2, class D2> |
| 38 | // requires three_way_comparable_with<typename unique_ptr<T1, D1>::pointer, |
| 39 | // typename unique_ptr<T2, D2>::pointer> |
| 40 | // compare_three_way_result_t<typename unique_ptr<T1, D1>::pointer, |
| 41 | // typename unique_ptr<T2, D2>::pointer> |
| 42 | // operator<=>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); |
| 43 | |
| 44 | #include <memory> |
| 45 | #include <cassert> |
| 46 | |
| 47 | #include "test_macros.h" |
| 48 | #include "deleter_types.h" |
| 49 | #include "test_comparisons.h" |
| 50 | #include "unique_ptr_test_helper.h" |
| 51 | |
| 52 | TEST_CONSTEXPR_CXX23 bool test() { |
| 53 | AssertComparisonsReturnBool<std::unique_ptr<int> >(); |
| 54 | #if TEST_STD_VER > 17 |
| 55 | AssertOrderReturn<std::strong_ordering, std::unique_ptr<int>>(); |
| 56 | #endif |
| 57 | |
| 58 | // Pointers of same type |
| 59 | { |
| 60 | A* ptr1 = new A; |
| 61 | A* ptr2 = new A; |
| 62 | const std::unique_ptr<A, Deleter<A> > p1(ptr1); |
| 63 | const std::unique_ptr<A, Deleter<A> > p2(ptr2); |
| 64 | |
| 65 | assert(!(p1 == p2)); |
| 66 | assert(p1 != p2); |
| 67 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 68 | assert((p1 < p2) == (ptr1 < ptr2)); |
| 69 | assert((p1 <= p2) == (ptr1 <= ptr2)); |
| 70 | assert((p1 > p2) == (ptr1 > ptr2)); |
| 71 | assert((p1 >= p2) == (ptr1 >= ptr2)); |
| 72 | #if TEST_STD_VER > 17 |
| 73 | assert((p1 <=> p2) != std::strong_ordering::equal); |
| 74 | assert((p1 <=> p2) == (ptr1 <=> ptr2)); |
| 75 | #endif |
| 76 | } |
| 77 | } |
| 78 | // Pointers of different type |
| 79 | { |
| 80 | A* ptr1 = new A; |
| 81 | B* ptr2 = new B; |
| 82 | const std::unique_ptr<A, Deleter<A> > p1(ptr1); |
| 83 | const std::unique_ptr<B, Deleter<B> > p2(ptr2); |
| 84 | assert(!(p1 == p2)); |
| 85 | assert(p1 != p2); |
| 86 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 87 | assert((p1 < p2) == (ptr1 < ptr2)); |
| 88 | assert((p1 <= p2) == (ptr1 <= ptr2)); |
| 89 | assert((p1 > p2) == (ptr1 > ptr2)); |
| 90 | assert((p1 >= p2) == (ptr1 >= ptr2)); |
| 91 | #if TEST_STD_VER > 17 |
| 92 | assert((p1 <=> p2) != std::strong_ordering::equal); |
| 93 | assert((p1 <=> p2) == (ptr1 <=> ptr2)); |
| 94 | #endif |
| 95 | } |
| 96 | } |
| 97 | // Pointers of same array type |
| 98 | { |
| 99 | A* ptr1 = new A[3]; |
| 100 | A* ptr2 = new A[3]; |
| 101 | const std::unique_ptr<A[], Deleter<A[]> > p1(ptr1); |
| 102 | const std::unique_ptr<A[], Deleter<A[]> > p2(ptr2); |
| 103 | assert(!(p1 == p2)); |
| 104 | assert(p1 != p2); |
| 105 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 106 | assert((p1 < p2) == (ptr1 < ptr2)); |
| 107 | assert((p1 <= p2) == (ptr1 <= ptr2)); |
| 108 | assert((p1 > p2) == (ptr1 > ptr2)); |
| 109 | assert((p1 >= p2) == (ptr1 >= ptr2)); |
| 110 | #if TEST_STD_VER > 17 |
| 111 | assert((p1 <=> p2) != std::strong_ordering::equal); |
| 112 | assert((p1 <=> p2) == (ptr1 <=> ptr2)); |
| 113 | #endif |
| 114 | } |
| 115 | } |
| 116 | // Pointers of different array types |
| 117 | { |
| 118 | A* ptr1 = new A[3]; |
| 119 | B* ptr2 = new B[3]; |
| 120 | const std::unique_ptr<A[], Deleter<A[]> > p1(ptr1); |
| 121 | const std::unique_ptr<B[], Deleter<B[]> > p2(ptr2); |
| 122 | assert(!(p1 == p2)); |
| 123 | assert(p1 != p2); |
| 124 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 125 | assert((p1 < p2) == (ptr1 < ptr2)); |
| 126 | assert((p1 <= p2) == (ptr1 <= ptr2)); |
| 127 | assert((p1 > p2) == (ptr1 > ptr2)); |
| 128 | assert((p1 >= p2) == (ptr1 >= ptr2)); |
| 129 | #if TEST_STD_VER > 17 |
| 130 | assert((p1 <=> p2) != std::strong_ordering::equal); |
| 131 | assert((p1 <=> p2) == (ptr1 <=> ptr2)); |
| 132 | #endif |
| 133 | } |
| 134 | } |
| 135 | // Default-constructed pointers of same type |
| 136 | { |
| 137 | const std::unique_ptr<A, Deleter<A> > p1; |
| 138 | const std::unique_ptr<A, Deleter<A> > p2; |
| 139 | assert(p1 == p2); |
| 140 | #if TEST_STD_VER > 17 |
| 141 | if (!TEST_IS_CONSTANT_EVALUATED) |
| 142 | assert((p1 <=> p2) == std::strong_ordering::equal); |
| 143 | #endif |
| 144 | } |
| 145 | // Default-constructed pointers of different type |
| 146 | { |
| 147 | const std::unique_ptr<A, Deleter<A> > p1; |
| 148 | const std::unique_ptr<B, Deleter<B> > p2; |
| 149 | assert(p1 == p2); |
| 150 | #if TEST_STD_VER > 17 |
| 151 | if (!TEST_IS_CONSTANT_EVALUATED) |
| 152 | assert((p1 <=> p2) == std::strong_ordering::equal); |
| 153 | #endif |
| 154 | } |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | int main(int, char**) { |
| 160 | test(); |
| 161 | #if TEST_STD_VER >= 23 |
| 162 | static_assert(test()); |
| 163 | #endif |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |