| 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 | // REQUIRES: std-at-least-c++26 |
| 10 | |
| 11 | // <functional> |
| 12 | |
| 13 | // class reference_wrapper |
| 14 | |
| 15 | // [refwrap.comparisons], comparisons |
| 16 | |
| 17 | // friend constexpr auto operator<=>(reference_wrapper, const T&); // Since C++26 |
| 18 | |
| 19 | #include <cassert> |
| 20 | #include <concepts> |
| 21 | #include <functional> |
| 22 | |
| 23 | #include "test_comparisons.h" |
| 24 | #include "test_macros.h" |
| 25 | |
| 26 | // Test SFINAE. |
| 27 | |
| 28 | static_assert(HasOperatorSpaceship<std::reference_wrapper<StrongOrder>, int>); |
| 29 | static_assert(HasOperatorSpaceship<std::reference_wrapper<WeakOrder>, int>); |
| 30 | static_assert(HasOperatorSpaceship<std::reference_wrapper<PartialOrder>, int>); |
| 31 | |
| 32 | static_assert(!HasOperatorSpaceship<std::reference_wrapper<NonComparable>, int>); |
| 33 | |
| 34 | // Test comparisons. |
| 35 | |
| 36 | template <typename T, typename Order> |
| 37 | constexpr void test() { |
| 38 | T t{47}; |
| 39 | |
| 40 | T bigger{94}; |
| 41 | T smaller{82}; |
| 42 | |
| 43 | T unordered{std::numeric_limits<int>::min()}; |
| 44 | |
| 45 | // Identical contents |
| 46 | { |
| 47 | std::reference_wrapper<T> rw1{t}; |
| 48 | assert(testOrder(rw1, t, Order::equivalent)); |
| 49 | } |
| 50 | // Less |
| 51 | { |
| 52 | std::reference_wrapper<T> rw1{smaller}; |
| 53 | assert(testOrder(rw1, bigger, Order::less)); |
| 54 | } |
| 55 | // Greater |
| 56 | { |
| 57 | std::reference_wrapper<T> rw1{bigger}; |
| 58 | assert(testOrder(rw1, smaller, Order::greater)); |
| 59 | } |
| 60 | // Unordered |
| 61 | if constexpr (std::same_as<T, PartialOrder>) { |
| 62 | std::reference_wrapper<T> rw1{bigger}; |
| 63 | assert(testOrder(rw1, unordered, Order::unordered)); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | constexpr bool test() { |
| 68 | test<int, std::strong_ordering>(); |
| 69 | test<StrongOrder, std::strong_ordering>(); |
| 70 | test<int, std::weak_ordering>(); |
| 71 | test<WeakOrder, std::weak_ordering>(); |
| 72 | test<int, std::partial_ordering>(); |
| 73 | test<PartialOrder, std::partial_ordering>(); |
| 74 | |
| 75 | // `LessAndEqComp` does not have `operator<=>`. Ordering is synthesized based on `operator<` |
| 76 | test<LessAndEqComp, std::weak_ordering>(); |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | int main(int, char**) { |
| 82 | test(); |
| 83 | static_assert(test()); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |