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, c++20, c++23
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 #include "helper_concepts.h"
27 #include "helper_types.h"
28 
29 // Test SFINAE.
30 
31 static_assert(HasSpaceshipOperatorWithInt<std::reference_wrapper<StrongOrder>>);
32 static_assert(HasSpaceshipOperatorWithInt<std::reference_wrapper<WeakOrder>>);
33 static_assert(HasSpaceshipOperatorWithInt<std::reference_wrapper<PartialOrder>>);
34 
35 static_assert(!HasSpaceshipOperatorWithInt<std::reference_wrapper<NonComparable>>);
36 
37 // Test comparisons.
38 
39 template <typename T, typename Order>
test()40 constexpr void test() {
41   T t{47};
42 
43   T bigger{94};
44   T smaller{82};
45 
46   T unordered{std::numeric_limits<int>::min()};
47 
48   // Identical contents
49   {
50     std::reference_wrapper<T> rw1{t};
51     assert(testOrder(rw1, t, Order::equivalent));
52   }
53   // Less
54   {
55     std::reference_wrapper<T> rw1{smaller};
56     assert(testOrder(rw1, bigger, Order::less));
57   }
58   // Greater
59   {
60     std::reference_wrapper<T> rw1{bigger};
61     assert(testOrder(rw1, smaller, Order::greater));
62   }
63   // Unordered
64   if constexpr (std::same_as<T, PartialOrder>) {
65     std::reference_wrapper<T> rw1{bigger};
66     assert(testOrder(rw1, unordered, Order::unordered));
67   }
68 }
69 
test()70 constexpr bool test() {
71   test<int, std::strong_ordering>();
72   test<StrongOrder, std::strong_ordering>();
73   test<int, std::weak_ordering>();
74   test<WeakOrder, std::weak_ordering>();
75   test<int, std::partial_ordering>();
76   test<PartialOrder, std::partial_ordering>();
77 
78   // `LessAndEqComp` does not have `operator<=>`. Ordering is synthesized based on `operator<`
79   test<LessAndEqComp, std::weak_ordering>();
80 
81   return true;
82 }
83 
main(int,char **)84 int main(int, char**) {
85   test();
86   static_assert(test());
87 
88   return 0;
89 }
90