| 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 T, class D> |
| 14 | // constexpr bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept; // constexpr since C++23 |
| 15 | // template <class T, class D> |
| 16 | // bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept; // removed in C++20 |
| 17 | // template <class T, class D> |
| 18 | // bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept; // removed in C++20 |
| 19 | // template <class T, class D> |
| 20 | // bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept; // removed in C++20 |
| 21 | // template <class T, class D> |
| 22 | // constexpr bool operator<(const unique_ptr<T, D>& x, nullptr_t); // constexpr since C++23 |
| 23 | // template <class T, class D> |
| 24 | // constexpr bool operator<(nullptr_t, const unique_ptr<T, D>& y); // constexpr since C++23 |
| 25 | // template <class T, class D> |
| 26 | // constexpr bool operator<=(const unique_ptr<T, D>& x, nullptr_t); // constexpr since C++23 |
| 27 | // template <class T, class D> |
| 28 | // constexpr bool operator<=(nullptr_t, const unique_ptr<T, D>& y); // constexpr since C++23 |
| 29 | // template <class T, class D> |
| 30 | // constexpr bool operator>(const unique_ptr<T, D>& x, nullptr_t); // constexpr since C++23 |
| 31 | // template <class T, class D> |
| 32 | // constexpr bool operator>(nullptr_t, const unique_ptr<T, D>& y); // constexpr since C++23 |
| 33 | // template <class T, class D> |
| 34 | // constexpr bool operator>=(const unique_ptr<T, D>& x, nullptr_t); // constexpr since C++23 |
| 35 | // template <class T, class D> |
| 36 | // constexpr bool operator>=(nullptr_t, const unique_ptr<T, D>& y); // constexpr since C++23 |
| 37 | // template<class T, class D> |
| 38 | // requires three_way_comparable<typename unique_ptr<T, D>::pointer> |
| 39 | // constexpr compare_three_way_result_t<typename unique_ptr<T, D>::pointer> |
| 40 | // operator<=>(const unique_ptr<T, D>& x, nullptr_t); // C++20 |
| 41 | |
| 42 | #include <cassert> |
| 43 | #include <cstddef> |
| 44 | #include <memory> |
| 45 | |
| 46 | #include "test_macros.h" |
| 47 | #include "test_comparisons.h" |
| 48 | |
| 49 | TEST_CONSTEXPR_CXX23 bool test() { |
| 50 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 51 | AssertEqualityAreNoexcept<std::unique_ptr<int>, nullptr_t>(); |
| 52 | AssertEqualityAreNoexcept<nullptr_t, std::unique_ptr<int> >(); |
| 53 | AssertComparisonsReturnBool<std::unique_ptr<int>, nullptr_t>(); |
| 54 | AssertComparisonsReturnBool<nullptr_t, std::unique_ptr<int> >(); |
| 55 | #if TEST_STD_VER >= 20 |
| 56 | AssertOrderReturn<std::strong_ordering, std::unique_ptr<int>, nullptr_t>(); |
| 57 | AssertOrderReturn<std::strong_ordering, nullptr_t, std::unique_ptr<int>>(); |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | const std::unique_ptr<int> p1(new int(1)); |
| 62 | assert(!(p1 == nullptr)); |
| 63 | assert(!(nullptr == p1)); |
| 64 | // A pointer to allocated storage and a nullptr can't be compared at compile-time |
| 65 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 66 | assert(!(p1 < nullptr)); |
| 67 | assert((nullptr < p1)); |
| 68 | assert(!(p1 <= nullptr)); |
| 69 | assert((nullptr <= p1)); |
| 70 | assert((p1 > nullptr)); |
| 71 | assert(!(nullptr > p1)); |
| 72 | assert((p1 >= nullptr)); |
| 73 | assert(!(nullptr >= p1)); |
| 74 | #if TEST_STD_VER >= 20 |
| 75 | assert((p1 <=> nullptr) == std::strong_ordering::greater); |
| 76 | assert((nullptr <=> p1) == std::strong_ordering::less); |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | const std::unique_ptr<int> p2; |
| 81 | assert((p2 == nullptr)); |
| 82 | assert((nullptr == p2)); |
| 83 | assert(!(p2 < nullptr)); |
| 84 | assert(!(nullptr < p2)); |
| 85 | assert((p2 <= nullptr)); |
| 86 | assert((nullptr <= p2)); |
| 87 | assert(!(p2 > nullptr)); |
| 88 | assert(!(nullptr > p2)); |
| 89 | assert((p2 >= nullptr)); |
| 90 | assert((nullptr >= p2)); |
| 91 | #if TEST_STD_VER >= 20 |
| 92 | assert((p2 <=> nullptr) == std::strong_ordering::equivalent); |
| 93 | assert((nullptr <=> p2) == std::strong_ordering::equivalent); |
| 94 | #endif |
| 95 | |
| 96 | const std::unique_ptr<int[]> p3(new int[1]); |
| 97 | assert(!(p3 == nullptr)); |
| 98 | assert(!(nullptr == p3)); |
| 99 | // A pointer to allocated storage and a nullptr can't be compared at compile-time |
| 100 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 101 | assert(!(p3 < nullptr)); |
| 102 | assert((nullptr < p3)); |
| 103 | assert(!(p3 <= nullptr)); |
| 104 | assert((nullptr <= p3)); |
| 105 | assert((p3 > nullptr)); |
| 106 | assert(!(nullptr > p3)); |
| 107 | assert((p3 >= nullptr)); |
| 108 | assert(!(nullptr >= p3)); |
| 109 | #if TEST_STD_VER >= 20 |
| 110 | assert((nullptr <=> p3) == std::strong_ordering::less); |
| 111 | assert((p3 <=> nullptr) == std::strong_ordering::greater); |
| 112 | #endif |
| 113 | } |
| 114 | |
| 115 | const std::unique_ptr<int[]> p4; |
| 116 | assert((p4 == nullptr)); |
| 117 | assert((nullptr == p4)); |
| 118 | assert(!(p4 < nullptr)); |
| 119 | assert(!(nullptr < p4)); |
| 120 | assert((p4 <= nullptr)); |
| 121 | assert((nullptr <= p4)); |
| 122 | assert(!(p4 > nullptr)); |
| 123 | assert(!(nullptr > p4)); |
| 124 | assert((p4 >= nullptr)); |
| 125 | assert((nullptr >= p4)); |
| 126 | #if TEST_STD_VER >= 20 |
| 127 | assert((p4 <=> nullptr) == std::strong_ordering::equivalent); |
| 128 | assert((nullptr <=> p4) == std::strong_ordering::equivalent); |
| 129 | #endif |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | int main(int, char**) { |
| 135 | test(); |
| 136 | #if TEST_STD_VER >= 23 |
| 137 | static_assert(test()); |
| 138 | #endif |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |