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, reference_wrapper); // 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(std::three_way_comparable<std::reference_wrapper<StrongOrder>>);
32 static_assert(std::three_way_comparable<std::reference_wrapper<WeakOrder>>);
33 static_assert(std::three_way_comparable<std::reference_wrapper<PartialOrder>>);
34
35 static_assert(!std::three_way_comparable<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 std::reference_wrapper<T> rw2{t};
52 assert(testOrder(rw1, rw2, Order::equivalent));
53 }
54 // Less
55 {
56 std::reference_wrapper<T> rw1{smaller};
57 std::reference_wrapper<T> rw2{bigger};
58 assert(testOrder(rw1, rw2, Order::less));
59 }
60 // Greater
61 {
62 std::reference_wrapper<T> rw1{bigger};
63 std::reference_wrapper<T> rw2{smaller};
64 assert(testOrder(rw1, rw2, Order::greater));
65 }
66 // Unordered
67 if constexpr (std::same_as<T, PartialOrder>) {
68 std::reference_wrapper<T> rw1{bigger};
69 std::reference_wrapper<T> rw2{unordered};
70 assert(testOrder(rw1, rw2, Order::unordered));
71 }
72 }
73
test()74 constexpr bool test() {
75 test<int, std::strong_ordering>();
76 test<StrongOrder, std::strong_ordering>();
77 test<int, std::weak_ordering>();
78 test<WeakOrder, std::weak_ordering>();
79 test<int, std::partial_ordering>();
80 test<PartialOrder, std::partial_ordering>();
81
82 // `LessAndEqComp` does not have `operator<=>`. Ordering is synthesized based on `operator<`
83 test<LessAndEqComp, std::weak_ordering>();
84
85 return true;
86 }
87
main(int,char **)88 int main(int, char**) {
89 test();
90 static_assert(test());
91
92 return 0;
93 }
94