summaryrefslogtreecommitdiff
path: root/libcxx/test/std/language.support/cmp/cmp.alg/partial_order.pass.cpp
blob: bd313889b1a4e3ee9b0b6d2f94339a6de40caca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

// <compare>

// template<class T> constexpr partial_ordering partial_order(const T& a, const T& b);

#include <compare>

#include <cassert>
#include <cmath>
#include <iterator> // std::size
#include <limits>
#include <type_traits>
#include <utility>

#include "test_macros.h"

template<class T, class U>
constexpr auto has_partial_order(T&& t, U&& u)
    -> decltype(std::partial_order(static_cast<T&&>(t), static_cast<U&&>(u)), true)
{
    return true;
}

constexpr bool has_partial_order(...) {
    return false;
}

namespace N11 {
    struct A {};
    struct B {};
    std::strong_ordering partial_order(const A&, const A&) { return std::strong_ordering::less; }
    std::strong_ordering partial_order(const A&, const B&);
}

void test_1_1()
{
    // If the decayed types of E and F differ, partial_order(E, F) is ill-formed.

    static_assert( has_partial_order(1, 2));
    static_assert(!has_partial_order(1, (short)2));
    static_assert(!has_partial_order(1, 2.0));
    static_assert(!has_partial_order(1.0f, 2.0));

    static_assert( has_partial_order((int*)nullptr, (int*)nullptr));
    static_assert(!has_partial_order((int*)nullptr, (const int*)nullptr));
    static_assert(!has_partial_order((const int*)nullptr, (int*)nullptr));
    static_assert( has_partial_order((const int*)nullptr, (const int*)nullptr));

    N11::A a;
    N11::B b;
    static_assert( has_partial_order(a, a));
    static_assert(!has_partial_order(a, b));
}

namespace N12 {
    struct A {};
    std::strong_ordering partial_order(A&, A&&) { return std::strong_ordering::less; }
    std::weak_ordering partial_order(A&&, A&&) { return std::weak_ordering::equivalent; }
    std::strong_ordering partial_order(const A&, const A&);

    struct B {
        friend int partial_order(B, B);
    };

    struct PartialOrder {
        explicit operator std::partial_ordering() const { return std::partial_ordering::less; }
    };
    struct C {
        bool touched = false;
        friend PartialOrder partial_order(C& lhs, C&) { lhs.touched = true; return PartialOrder(); }
    };
}

void test_1_2()
{
    // Otherwise, partial_ordering(partial_order(E, F))
    // if it is a well-formed expression with overload resolution performed
    // in a context that does not include a declaration of std::partial_order.

    // Test that partial_order does not const-qualify the forwarded arguments.
    N12::A a;
    assert(std::partial_order(a, std::move(a)) == std::partial_ordering::less);
    assert(std::partial_order(std::move(a), std::move(a)) == std::partial_ordering::equivalent);

    // The type of partial_order(e,f) must be explicitly convertible to partial_ordering.
    N12::B b;
    static_assert(!has_partial_order(b, b));

    N12::C c1, c2;
    ASSERT_SAME_TYPE(decltype(std::partial_order(c1, c2)), std::partial_ordering);
    assert(std::partial_order(c1, c2) == std::partial_ordering::less);
    assert(c1.touched);
    assert(!c2.touched);
}

namespace N13 {
    // Compare to N12::A.
    struct A {};
    bool operator==(const A&, const A&);
    constexpr std::partial_ordering operator<=>(A&, A&&) { return std::partial_ordering::less; }
    constexpr std::partial_ordering operator<=>(A&&, A&&) { return std::partial_ordering::equivalent; }
    std::partial_ordering operator<=>(const A&, const A&);
    static_assert(std::three_way_comparable<A>);

    struct B {
        std::partial_ordering operator<=>(const B&) const;  // lacks operator==
    };
    static_assert(!std::three_way_comparable<B>);

    struct C {
        bool *touched;
        bool operator==(const C&) const;
        constexpr std::partial_ordering operator<=>(const C& rhs) const {
            *rhs.touched = true;
            return std::partial_ordering::equivalent;
        }
    };
    static_assert(std::three_way_comparable<C>);
}

constexpr bool test_1_3()
{
    // Otherwise, partial_ordering(compare_three_way()(E, F)) if it is a well-formed expression.

    // Test neither partial_order nor compare_three_way const-qualify the forwarded arguments.
    N13::A a;
    assert(std::partial_order(a, std::move(a)) == std::partial_ordering::less);
    assert(std::partial_order(std::move(a), std::move(a)) == std::partial_ordering::equivalent);

    N13::B b;
    static_assert(!has_partial_order(b, b));

    // Test that the arguments are passed to <=> in the correct order.
    bool c1_touched = false;
    bool c2_touched = false;
    N13::C c1 = {&c1_touched};
    N13::C c2 = {&c2_touched};
    assert(std::partial_order(c1, c2) == std::partial_ordering::equivalent);
    assert(!c1_touched);
    assert(c2_touched);

    // For partial_order, this bullet point takes care of floating-point types;
    // they receive their natural partial order.
    {
        using F = float;
        F nan = std::numeric_limits<F>::quiet_NaN();
        assert(std::partial_order(F(1), F(2)) == std::partial_ordering::less);
        assert(std::partial_order(F(0), -F(0)) == std::partial_ordering::equivalent);
#ifndef TEST_COMPILER_GCC  // GCC can't compare NaN to non-NaN in a constant-expression
        assert(std::partial_order(nan, F(1)) == std::partial_ordering::unordered);
#endif
        assert(std::partial_order(nan, nan) == std::partial_ordering::unordered);
    }
    {
        using F = double;
        F nan = std::numeric_limits<F>::quiet_NaN();
        assert(std::partial_order(F(1), F(2)) == std::partial_ordering::less);
        assert(std::partial_order(F(0), -F(0)) == std::partial_ordering::equivalent);
#ifndef TEST_COMPILER_GCC
        assert(std::partial_order(nan, F(1)) == std::partial_ordering::unordered);
#endif
        assert(std::partial_order(nan, nan) == std::partial_ordering::unordered);
    }
    {
        using F = long double;
        F nan = std::numeric_limits<F>::quiet_NaN();
        assert(std::partial_order(F(1), F(2)) == std::partial_ordering::less);
        assert(std::partial_order(F(0), -F(0)) == std::partial_ordering::equivalent);
#ifndef TEST_COMPILER_GCC
        assert(std::partial_order(nan, F(1)) == std::partial_ordering::unordered);
#endif
        assert(std::partial_order(nan, nan) == std::partial_ordering::unordered);
    }

    return true;
}

namespace N14 {
    struct A {};
    constexpr std::strong_ordering weak_order(A&, A&&) { return std::strong_ordering::less; }
    constexpr std::strong_ordering weak_order(A&&, A&&) { return std::strong_ordering::equal; }
    std::strong_ordering weak_order(const A&, const A&);

    struct B {
        friend std::partial_ordering weak_order(B, B);
    };

    struct StrongOrder {
        operator std::strong_ordering() const { return std::strong_ordering::less; }
    };
    struct C {
        friend StrongOrder weak_order(C& lhs, C&);
    };

    struct WeakOrder {
        constexpr explicit operator std::weak_ordering() const { return std::weak_ordering::less; }
        operator std::partial_ordering() const = delete;
    };
    struct D {
        bool touched = false;
        friend constexpr WeakOrder weak_order(D& lhs, D&) { lhs.touched = true; return WeakOrder(); }
    };
}

constexpr bool test_1_4()
{
    // Otherwise, partial_ordering(weak_order(E, F)) [that is, std::weak_order]
    // if it is a well-formed expression.

    // Test that partial_order and weak_order do not const-qualify the forwarded arguments.
    N14::A a;
    assert(std::partial_order(a, std::move(a)) == std::partial_ordering::less);
    assert(std::partial_order(std::move(a), std::move(a)) == std::partial_ordering::equivalent);

    // The type of ADL weak_order(e,f) must be explicitly convertible to weak_ordering
    // (not just to partial_ordering), or else std::weak_order(e,f) won't exist.
    N14::B b;
    static_assert(!has_partial_order(b, b));

    // The type of ADL weak_order(e,f) must be explicitly convertible to weak_ordering
    // (not just to strong_ordering), or else std::weak_order(e,f) won't exist.
    N14::C c;
    static_assert(!has_partial_order(c, c));

    N14::D d1, d2;
    ASSERT_SAME_TYPE(decltype(std::partial_order(d1, d2)), std::partial_ordering);
    assert(std::partial_order(d1, d2) == std::partial_ordering::less);
    assert(d1.touched);
    assert(!d2.touched);

    return true;
}

int main(int, char**)
{
    test_1_1();
    test_1_2();
    test_1_3();
    test_1_4();

    static_assert(test_1_3());
    static_assert(test_1_4());

    return 0;
}