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, 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// Test SFINAE.
26
27static_assert(HasOperatorSpaceship<std::reference_wrapper<StrongOrder>>);
28static_assert(HasOperatorSpaceship<std::reference_wrapper<WeakOrder>>);
29static_assert(HasOperatorSpaceship<std::reference_wrapper<PartialOrder>>);
30
31static_assert(!HasOperatorSpaceship<std::reference_wrapper<NonComparable>>);
32
33// Test comparisons.
34
35template <typename T, typename Order>
36constexpr void test() {
37 T t{47};
38
39 T bigger{94};
40 T smaller{82};
41
42 T unordered{std::numeric_limits<int>::min()};
43
44 // Identical contents
45 {
46 std::reference_wrapper<T> rw1{t};
47 std::reference_wrapper<T> rw2{t};
48 assert(testOrder(rw1, rw2, Order::equivalent));
49 }
50 // Less
51 {
52 std::reference_wrapper<T> rw1{smaller};
53 std::reference_wrapper<T> rw2{bigger};
54 assert(testOrder(rw1, rw2, Order::less));
55 }
56 // Greater
57 {
58 std::reference_wrapper<T> rw1{bigger};
59 std::reference_wrapper<T> rw2{smaller};
60 assert(testOrder(rw1, rw2, Order::greater));
61 }
62 // Unordered
63 if constexpr (std::same_as<T, PartialOrder>) {
64 std::reference_wrapper<T> rw1{bigger};
65 std::reference_wrapper<T> rw2{unordered};
66 assert(testOrder(rw1, rw2, Order::unordered));
67 }
68}
69
70constexpr 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
84int main(int, char**) {
85 test();
86 static_assert(test());
87
88 return 0;
89}
90

source code of libcxx/test/std/utilities/function.objects/refwrap/refwrap.comparissons/compare.three_way.refwrap.refwrap.pass.cpp