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
10
11// <compare>
12
13// template<class T> constexpr weak_ordering compare_weak_order_fallback(const T& a, const T& b);
14
15#include <compare>
16
17#include <cassert>
18#include <cmath>
19#include <iterator> // std::size
20#include <limits>
21#include <type_traits>
22#include <utility>
23
24#include "test_macros.h"
25
26template<class T, class U>
27constexpr auto has_weak_order(T&& t, U&& u)
28 -> decltype(std::compare_weak_order_fallback(static_cast<T&&>(t), static_cast<U&&>(u)), true)
29{
30 return true;
31}
32
33constexpr bool has_weak_order(...) {
34 return false;
35}
36
37namespace N11 {
38 struct A {};
39 struct B {};
40 std::strong_ordering weak_order(const A&, const A&) { return std::strong_ordering::less; }
41 std::strong_ordering weak_order(const A&, const B&);
42}
43
44void test_1_1()
45{
46 // If the decayed types of E and F differ, weak_order(E, F) is ill-formed.
47
48 static_assert( has_weak_order(1, 2));
49 static_assert(!has_weak_order(1, (short)2));
50 static_assert(!has_weak_order(1, 2.0));
51 static_assert(!has_weak_order(1.0f, 2.0));
52
53 static_assert( has_weak_order((int*)nullptr, (int*)nullptr));
54 static_assert(!has_weak_order((int*)nullptr, (const int*)nullptr));
55 static_assert(!has_weak_order((const int*)nullptr, (int*)nullptr));
56 static_assert( has_weak_order((const int*)nullptr, (const int*)nullptr));
57
58 N11::A a;
59 N11::B b;
60 static_assert( has_weak_order(a, a));
61 static_assert(!has_weak_order(a, b));
62}
63
64namespace N12 {
65 struct A {};
66 std::strong_ordering weak_order(A&, A&&) { return std::strong_ordering::less; }
67 std::strong_ordering weak_order(A&&, A&&) { return std::strong_ordering::equal; }
68 std::strong_ordering weak_order(const A&, const A&);
69
70 struct B {
71 friend std::partial_ordering weak_order(B&, B&);
72 };
73
74 struct WeakOrder {
75 explicit operator std::weak_ordering() const { return std::weak_ordering::less; }
76 };
77 struct C {
78 bool touched = false;
79 friend WeakOrder weak_order(C& lhs, C&) { lhs.touched = true; return WeakOrder(); }
80 };
81}
82
83void test_1_2()
84{
85 // Otherwise, weak_ordering(weak_order(E, F))
86 // if it is a well-formed expression with overload resolution performed
87 // in a context that does not include a declaration of std::weak_order.
88
89 // Test that weak_order does not const-qualify the forwarded arguments.
90 N12::A a;
91 assert(std::compare_weak_order_fallback(a, std::move(a)) == std::weak_ordering::less);
92 assert(std::compare_weak_order_fallback(std::move(a), std::move(a)) == std::weak_ordering::equivalent);
93
94 // The type of weak_order(e,f) must be explicitly convertible to weak_ordering.
95 N12::B b;
96 static_assert(!has_weak_order(b, b));
97
98 N12::C c1, c2;
99 ASSERT_SAME_TYPE(decltype(std::compare_weak_order_fallback(c1, c2)), std::weak_ordering);
100 assert(std::compare_weak_order_fallback(c1, c2) == std::weak_ordering::less);
101 assert(c1.touched);
102 assert(!c2.touched);
103}
104
105template<class F>
106constexpr bool test_1_3()
107{
108 // Otherwise, if the decayed type T of E is a floating-point type,
109 // yields a value of type weak_ordering that is consistent with
110 // the ordering observed by T's comparison operators and strong_order,
111 // and if numeric_limits<T>::is_iec559 is true, is additionally consistent with
112 // the following equivalence classes...
113
114 // std::numeric_limits<F>::is_iec559 is usually true.
115 // It is false for F=long double on AIX; but this test is still expected
116 // to pass (e.g. std::weak_order(+0, -0) == weak_ordering::equivalent,
117 // even on AIX).
118
119 ASSERT_SAME_TYPE(decltype(std::compare_weak_order_fallback(F(0), F(0))), std::weak_ordering);
120
121 F v[] = {
122 -std::numeric_limits<F>::infinity(),
123 std::numeric_limits<F>::lowest(), // largest (finite) negative number
124 F(-1.0), F(-0.1),
125 -std::numeric_limits<F>::min(), // smallest (normal) negative number
126 F(-0.0), // negative zero
127 F(0.0),
128 std::numeric_limits<F>::min(), // smallest (normal) positive number
129 F(0.1), F(1.0), F(2.0), F(3.14),
130 std::numeric_limits<F>::max(), // largest (finite) positive number
131 std::numeric_limits<F>::infinity(),
132 };
133
134 static_assert(std::size(v) == 14);
135
136 // Sanity-check that array 'v' is indeed in the right order.
137 for (int i=0; i < 14; ++i) {
138 for (int j=0; j < 14; ++j) {
139 auto naturalOrder = (v[i] <=> v[j]);
140 if (v[i] == 0 && v[j] == 0) {
141 assert(naturalOrder == std::partial_ordering::equivalent);
142 } else {
143 assert(naturalOrder == std::partial_ordering::unordered || naturalOrder == (i <=> j));
144 }
145 }
146 }
147
148 assert(std::compare_weak_order_fallback(v[0], v[0]) == std::weak_ordering::equivalent);
149 assert(std::compare_weak_order_fallback(v[0], v[1]) == std::weak_ordering::less);
150 assert(std::compare_weak_order_fallback(v[0], v[2]) == std::weak_ordering::less);
151 assert(std::compare_weak_order_fallback(v[0], v[3]) == std::weak_ordering::less);
152 assert(std::compare_weak_order_fallback(v[0], v[4]) == std::weak_ordering::less);
153 assert(std::compare_weak_order_fallback(v[0], v[5]) == std::weak_ordering::less);
154 assert(std::compare_weak_order_fallback(v[0], v[6]) == std::weak_ordering::less);
155 assert(std::compare_weak_order_fallback(v[0], v[7]) == std::weak_ordering::less);
156 assert(std::compare_weak_order_fallback(v[0], v[8]) == std::weak_ordering::less);
157 assert(std::compare_weak_order_fallback(v[0], v[9]) == std::weak_ordering::less);
158 assert(std::compare_weak_order_fallback(v[0], v[10]) == std::weak_ordering::less);
159 assert(std::compare_weak_order_fallback(v[0], v[11]) == std::weak_ordering::less);
160 assert(std::compare_weak_order_fallback(v[0], v[12]) == std::weak_ordering::less);
161 assert(std::compare_weak_order_fallback(v[0], v[13]) == std::weak_ordering::less);
162 assert(std::compare_weak_order_fallback(v[1], v[0]) == std::weak_ordering::greater);
163 assert(std::compare_weak_order_fallback(v[1], v[1]) == std::weak_ordering::equivalent);
164 assert(std::compare_weak_order_fallback(v[1], v[2]) == std::weak_ordering::less);
165 assert(std::compare_weak_order_fallback(v[1], v[3]) == std::weak_ordering::less);
166 assert(std::compare_weak_order_fallback(v[1], v[4]) == std::weak_ordering::less);
167 assert(std::compare_weak_order_fallback(v[1], v[5]) == std::weak_ordering::less);
168 assert(std::compare_weak_order_fallback(v[1], v[6]) == std::weak_ordering::less);
169 assert(std::compare_weak_order_fallback(v[1], v[7]) == std::weak_ordering::less);
170 assert(std::compare_weak_order_fallback(v[1], v[8]) == std::weak_ordering::less);
171 assert(std::compare_weak_order_fallback(v[1], v[9]) == std::weak_ordering::less);
172 assert(std::compare_weak_order_fallback(v[1], v[10]) == std::weak_ordering::less);
173 assert(std::compare_weak_order_fallback(v[1], v[11]) == std::weak_ordering::less);
174 assert(std::compare_weak_order_fallback(v[1], v[12]) == std::weak_ordering::less);
175 assert(std::compare_weak_order_fallback(v[1], v[13]) == std::weak_ordering::less);
176 assert(std::compare_weak_order_fallback(v[2], v[0]) == std::weak_ordering::greater);
177 assert(std::compare_weak_order_fallback(v[2], v[1]) == std::weak_ordering::greater);
178 assert(std::compare_weak_order_fallback(v[2], v[2]) == std::weak_ordering::equivalent);
179 assert(std::compare_weak_order_fallback(v[2], v[3]) == std::weak_ordering::less);
180 assert(std::compare_weak_order_fallback(v[2], v[4]) == std::weak_ordering::less);
181 assert(std::compare_weak_order_fallback(v[2], v[5]) == std::weak_ordering::less);
182 assert(std::compare_weak_order_fallback(v[2], v[6]) == std::weak_ordering::less);
183 assert(std::compare_weak_order_fallback(v[2], v[7]) == std::weak_ordering::less);
184 assert(std::compare_weak_order_fallback(v[2], v[8]) == std::weak_ordering::less);
185 assert(std::compare_weak_order_fallback(v[2], v[9]) == std::weak_ordering::less);
186 assert(std::compare_weak_order_fallback(v[2], v[10]) == std::weak_ordering::less);
187 assert(std::compare_weak_order_fallback(v[2], v[11]) == std::weak_ordering::less);
188 assert(std::compare_weak_order_fallback(v[2], v[12]) == std::weak_ordering::less);
189 assert(std::compare_weak_order_fallback(v[2], v[13]) == std::weak_ordering::less);
190 assert(std::compare_weak_order_fallback(v[3], v[0]) == std::weak_ordering::greater);
191 assert(std::compare_weak_order_fallback(v[3], v[1]) == std::weak_ordering::greater);
192 assert(std::compare_weak_order_fallback(v[3], v[2]) == std::weak_ordering::greater);
193 assert(std::compare_weak_order_fallback(v[3], v[3]) == std::weak_ordering::equivalent);
194 assert(std::compare_weak_order_fallback(v[3], v[4]) == std::weak_ordering::less);
195 assert(std::compare_weak_order_fallback(v[3], v[5]) == std::weak_ordering::less);
196 assert(std::compare_weak_order_fallback(v[3], v[6]) == std::weak_ordering::less);
197 assert(std::compare_weak_order_fallback(v[3], v[7]) == std::weak_ordering::less);
198 assert(std::compare_weak_order_fallback(v[3], v[8]) == std::weak_ordering::less);
199 assert(std::compare_weak_order_fallback(v[3], v[9]) == std::weak_ordering::less);
200 assert(std::compare_weak_order_fallback(v[3], v[10]) == std::weak_ordering::less);
201 assert(std::compare_weak_order_fallback(v[3], v[11]) == std::weak_ordering::less);
202 assert(std::compare_weak_order_fallback(v[3], v[12]) == std::weak_ordering::less);
203 assert(std::compare_weak_order_fallback(v[3], v[13]) == std::weak_ordering::less);
204 assert(std::compare_weak_order_fallback(v[4], v[0]) == std::weak_ordering::greater);
205 assert(std::compare_weak_order_fallback(v[4], v[1]) == std::weak_ordering::greater);
206 assert(std::compare_weak_order_fallback(v[4], v[2]) == std::weak_ordering::greater);
207 assert(std::compare_weak_order_fallback(v[4], v[3]) == std::weak_ordering::greater);
208 assert(std::compare_weak_order_fallback(v[4], v[4]) == std::weak_ordering::equivalent);
209 assert(std::compare_weak_order_fallback(v[4], v[5]) == std::weak_ordering::less);
210 assert(std::compare_weak_order_fallback(v[4], v[6]) == std::weak_ordering::less);
211 assert(std::compare_weak_order_fallback(v[4], v[7]) == std::weak_ordering::less);
212 assert(std::compare_weak_order_fallback(v[4], v[8]) == std::weak_ordering::less);
213 assert(std::compare_weak_order_fallback(v[4], v[9]) == std::weak_ordering::less);
214 assert(std::compare_weak_order_fallback(v[4], v[10]) == std::weak_ordering::less);
215 assert(std::compare_weak_order_fallback(v[4], v[11]) == std::weak_ordering::less);
216 assert(std::compare_weak_order_fallback(v[4], v[12]) == std::weak_ordering::less);
217 assert(std::compare_weak_order_fallback(v[4], v[13]) == std::weak_ordering::less);
218 assert(std::compare_weak_order_fallback(v[5], v[0]) == std::weak_ordering::greater);
219 assert(std::compare_weak_order_fallback(v[5], v[1]) == std::weak_ordering::greater);
220 assert(std::compare_weak_order_fallback(v[5], v[2]) == std::weak_ordering::greater);
221 assert(std::compare_weak_order_fallback(v[5], v[3]) == std::weak_ordering::greater);
222 assert(std::compare_weak_order_fallback(v[5], v[4]) == std::weak_ordering::greater);
223 assert(std::compare_weak_order_fallback(v[5], v[5]) == std::weak_ordering::equivalent);
224 assert(std::compare_weak_order_fallback(v[5], v[6]) == std::weak_ordering::equivalent);
225 assert(std::compare_weak_order_fallback(v[5], v[7]) == std::weak_ordering::less);
226 assert(std::compare_weak_order_fallback(v[5], v[8]) == std::weak_ordering::less);
227 assert(std::compare_weak_order_fallback(v[5], v[9]) == std::weak_ordering::less);
228 assert(std::compare_weak_order_fallback(v[5], v[10]) == std::weak_ordering::less);
229 assert(std::compare_weak_order_fallback(v[5], v[11]) == std::weak_ordering::less);
230 assert(std::compare_weak_order_fallback(v[5], v[12]) == std::weak_ordering::less);
231 assert(std::compare_weak_order_fallback(v[5], v[13]) == std::weak_ordering::less);
232 assert(std::compare_weak_order_fallback(v[6], v[0]) == std::weak_ordering::greater);
233 assert(std::compare_weak_order_fallback(v[6], v[1]) == std::weak_ordering::greater);
234 assert(std::compare_weak_order_fallback(v[6], v[2]) == std::weak_ordering::greater);
235 assert(std::compare_weak_order_fallback(v[6], v[3]) == std::weak_ordering::greater);
236 assert(std::compare_weak_order_fallback(v[6], v[4]) == std::weak_ordering::greater);
237 assert(std::compare_weak_order_fallback(v[6], v[5]) == std::weak_ordering::equivalent);
238 assert(std::compare_weak_order_fallback(v[6], v[6]) == std::weak_ordering::equivalent);
239 assert(std::compare_weak_order_fallback(v[6], v[7]) == std::weak_ordering::less);
240 assert(std::compare_weak_order_fallback(v[6], v[8]) == std::weak_ordering::less);
241 assert(std::compare_weak_order_fallback(v[6], v[9]) == std::weak_ordering::less);
242 assert(std::compare_weak_order_fallback(v[6], v[10]) == std::weak_ordering::less);
243 assert(std::compare_weak_order_fallback(v[6], v[11]) == std::weak_ordering::less);
244 assert(std::compare_weak_order_fallback(v[6], v[12]) == std::weak_ordering::less);
245 assert(std::compare_weak_order_fallback(v[6], v[13]) == std::weak_ordering::less);
246 assert(std::compare_weak_order_fallback(v[7], v[0]) == std::weak_ordering::greater);
247 assert(std::compare_weak_order_fallback(v[7], v[1]) == std::weak_ordering::greater);
248 assert(std::compare_weak_order_fallback(v[7], v[2]) == std::weak_ordering::greater);
249 assert(std::compare_weak_order_fallback(v[7], v[3]) == std::weak_ordering::greater);
250 assert(std::compare_weak_order_fallback(v[7], v[4]) == std::weak_ordering::greater);
251 assert(std::compare_weak_order_fallback(v[7], v[5]) == std::weak_ordering::greater);
252 assert(std::compare_weak_order_fallback(v[7], v[6]) == std::weak_ordering::greater);
253 assert(std::compare_weak_order_fallback(v[7], v[7]) == std::weak_ordering::equivalent);
254 assert(std::compare_weak_order_fallback(v[7], v[8]) == std::weak_ordering::less);
255 assert(std::compare_weak_order_fallback(v[7], v[9]) == std::weak_ordering::less);
256 assert(std::compare_weak_order_fallback(v[7], v[10]) == std::weak_ordering::less);
257 assert(std::compare_weak_order_fallback(v[7], v[11]) == std::weak_ordering::less);
258 assert(std::compare_weak_order_fallback(v[7], v[12]) == std::weak_ordering::less);
259 assert(std::compare_weak_order_fallback(v[7], v[13]) == std::weak_ordering::less);
260 assert(std::compare_weak_order_fallback(v[8], v[0]) == std::weak_ordering::greater);
261 assert(std::compare_weak_order_fallback(v[8], v[1]) == std::weak_ordering::greater);
262 assert(std::compare_weak_order_fallback(v[8], v[2]) == std::weak_ordering::greater);
263 assert(std::compare_weak_order_fallback(v[8], v[3]) == std::weak_ordering::greater);
264 assert(std::compare_weak_order_fallback(v[8], v[4]) == std::weak_ordering::greater);
265 assert(std::compare_weak_order_fallback(v[8], v[5]) == std::weak_ordering::greater);
266 assert(std::compare_weak_order_fallback(v[8], v[6]) == std::weak_ordering::greater);
267 assert(std::compare_weak_order_fallback(v[8], v[7]) == std::weak_ordering::greater);
268 assert(std::compare_weak_order_fallback(v[8], v[8]) == std::weak_ordering::equivalent);
269 assert(std::compare_weak_order_fallback(v[8], v[9]) == std::weak_ordering::less);
270 assert(std::compare_weak_order_fallback(v[8], v[10]) == std::weak_ordering::less);
271 assert(std::compare_weak_order_fallback(v[8], v[11]) == std::weak_ordering::less);
272 assert(std::compare_weak_order_fallback(v[8], v[12]) == std::weak_ordering::less);
273 assert(std::compare_weak_order_fallback(v[8], v[13]) == std::weak_ordering::less);
274 assert(std::compare_weak_order_fallback(v[9], v[0]) == std::weak_ordering::greater);
275 assert(std::compare_weak_order_fallback(v[9], v[1]) == std::weak_ordering::greater);
276 assert(std::compare_weak_order_fallback(v[9], v[2]) == std::weak_ordering::greater);
277 assert(std::compare_weak_order_fallback(v[9], v[3]) == std::weak_ordering::greater);
278 assert(std::compare_weak_order_fallback(v[9], v[4]) == std::weak_ordering::greater);
279 assert(std::compare_weak_order_fallback(v[9], v[5]) == std::weak_ordering::greater);
280 assert(std::compare_weak_order_fallback(v[9], v[6]) == std::weak_ordering::greater);
281 assert(std::compare_weak_order_fallback(v[9], v[7]) == std::weak_ordering::greater);
282 assert(std::compare_weak_order_fallback(v[9], v[8]) == std::weak_ordering::greater);
283 assert(std::compare_weak_order_fallback(v[9], v[9]) == std::weak_ordering::equivalent);
284 assert(std::compare_weak_order_fallback(v[9], v[10]) == std::weak_ordering::less);
285 assert(std::compare_weak_order_fallback(v[9], v[11]) == std::weak_ordering::less);
286 assert(std::compare_weak_order_fallback(v[9], v[12]) == std::weak_ordering::less);
287 assert(std::compare_weak_order_fallback(v[9], v[13]) == std::weak_ordering::less);
288 assert(std::compare_weak_order_fallback(v[10], v[0]) == std::weak_ordering::greater);
289 assert(std::compare_weak_order_fallback(v[10], v[1]) == std::weak_ordering::greater);
290 assert(std::compare_weak_order_fallback(v[10], v[2]) == std::weak_ordering::greater);
291 assert(std::compare_weak_order_fallback(v[10], v[3]) == std::weak_ordering::greater);
292 assert(std::compare_weak_order_fallback(v[10], v[4]) == std::weak_ordering::greater);
293 assert(std::compare_weak_order_fallback(v[10], v[5]) == std::weak_ordering::greater);
294 assert(std::compare_weak_order_fallback(v[10], v[6]) == std::weak_ordering::greater);
295 assert(std::compare_weak_order_fallback(v[10], v[7]) == std::weak_ordering::greater);
296 assert(std::compare_weak_order_fallback(v[10], v[8]) == std::weak_ordering::greater);
297 assert(std::compare_weak_order_fallback(v[10], v[9]) == std::weak_ordering::greater);
298 assert(std::compare_weak_order_fallback(v[10], v[10]) == std::weak_ordering::equivalent);
299 assert(std::compare_weak_order_fallback(v[10], v[11]) == std::weak_ordering::less);
300 assert(std::compare_weak_order_fallback(v[10], v[12]) == std::weak_ordering::less);
301 assert(std::compare_weak_order_fallback(v[10], v[13]) == std::weak_ordering::less);
302 assert(std::compare_weak_order_fallback(v[11], v[0]) == std::weak_ordering::greater);
303 assert(std::compare_weak_order_fallback(v[11], v[1]) == std::weak_ordering::greater);
304 assert(std::compare_weak_order_fallback(v[11], v[2]) == std::weak_ordering::greater);
305 assert(std::compare_weak_order_fallback(v[11], v[3]) == std::weak_ordering::greater);
306 assert(std::compare_weak_order_fallback(v[11], v[4]) == std::weak_ordering::greater);
307 assert(std::compare_weak_order_fallback(v[11], v[5]) == std::weak_ordering::greater);
308 assert(std::compare_weak_order_fallback(v[11], v[6]) == std::weak_ordering::greater);
309 assert(std::compare_weak_order_fallback(v[11], v[7]) == std::weak_ordering::greater);
310 assert(std::compare_weak_order_fallback(v[11], v[8]) == std::weak_ordering::greater);
311 assert(std::compare_weak_order_fallback(v[11], v[9]) == std::weak_ordering::greater);
312 assert(std::compare_weak_order_fallback(v[11], v[10]) == std::weak_ordering::greater);
313 assert(std::compare_weak_order_fallback(v[11], v[11]) == std::weak_ordering::equivalent);
314 assert(std::compare_weak_order_fallback(v[11], v[12]) == std::weak_ordering::less);
315 assert(std::compare_weak_order_fallback(v[11], v[13]) == std::weak_ordering::less);
316 assert(std::compare_weak_order_fallback(v[12], v[0]) == std::weak_ordering::greater);
317 assert(std::compare_weak_order_fallback(v[12], v[1]) == std::weak_ordering::greater);
318 assert(std::compare_weak_order_fallback(v[12], v[2]) == std::weak_ordering::greater);
319 assert(std::compare_weak_order_fallback(v[12], v[3]) == std::weak_ordering::greater);
320 assert(std::compare_weak_order_fallback(v[12], v[4]) == std::weak_ordering::greater);
321 assert(std::compare_weak_order_fallback(v[12], v[5]) == std::weak_ordering::greater);
322 assert(std::compare_weak_order_fallback(v[12], v[6]) == std::weak_ordering::greater);
323 assert(std::compare_weak_order_fallback(v[12], v[7]) == std::weak_ordering::greater);
324 assert(std::compare_weak_order_fallback(v[12], v[8]) == std::weak_ordering::greater);
325 assert(std::compare_weak_order_fallback(v[12], v[9]) == std::weak_ordering::greater);
326 assert(std::compare_weak_order_fallback(v[12], v[10]) == std::weak_ordering::greater);
327 assert(std::compare_weak_order_fallback(v[12], v[11]) == std::weak_ordering::greater);
328 assert(std::compare_weak_order_fallback(v[12], v[12]) == std::weak_ordering::equivalent);
329 assert(std::compare_weak_order_fallback(v[12], v[13]) == std::weak_ordering::less);
330 assert(std::compare_weak_order_fallback(v[13], v[0]) == std::weak_ordering::greater);
331 assert(std::compare_weak_order_fallback(v[13], v[1]) == std::weak_ordering::greater);
332 assert(std::compare_weak_order_fallback(v[13], v[2]) == std::weak_ordering::greater);
333 assert(std::compare_weak_order_fallback(v[13], v[3]) == std::weak_ordering::greater);
334 assert(std::compare_weak_order_fallback(v[13], v[4]) == std::weak_ordering::greater);
335 assert(std::compare_weak_order_fallback(v[13], v[5]) == std::weak_ordering::greater);
336 assert(std::compare_weak_order_fallback(v[13], v[6]) == std::weak_ordering::greater);
337 assert(std::compare_weak_order_fallback(v[13], v[7]) == std::weak_ordering::greater);
338 assert(std::compare_weak_order_fallback(v[13], v[8]) == std::weak_ordering::greater);
339 assert(std::compare_weak_order_fallback(v[13], v[9]) == std::weak_ordering::greater);
340 assert(std::compare_weak_order_fallback(v[13], v[10]) == std::weak_ordering::greater);
341 assert(std::compare_weak_order_fallback(v[13], v[11]) == std::weak_ordering::greater);
342 assert(std::compare_weak_order_fallback(v[13], v[12]) == std::weak_ordering::greater);
343 assert(std::compare_weak_order_fallback(v[13], v[13]) == std::weak_ordering::equivalent);
344
345
346 // There's no way to produce a specifically positive or negative NAN
347 // at compile-time, so the NAN-related tests must be runtime-only.
348
349 if (!std::is_constant_evaluated()) {
350 F nq = std::copysign(std::numeric_limits<F>::quiet_NaN(), F(-1));
351 F ns = std::copysign(std::numeric_limits<F>::signaling_NaN(), F(-1));
352 F ps = std::copysign(std::numeric_limits<F>::signaling_NaN(), F(+1));
353 F pq = std::copysign(std::numeric_limits<F>::quiet_NaN(), F(+1));
354
355 assert(std::compare_weak_order_fallback(nq, nq) == std::weak_ordering::equivalent);
356 assert(std::compare_weak_order_fallback(nq, ns) == std::weak_ordering::equivalent);
357 for (int i=0; i < 14; ++i) {
358 assert(std::compare_weak_order_fallback(nq, v[i]) == std::weak_ordering::less);
359 }
360 assert(std::compare_weak_order_fallback(nq, ps) == std::weak_ordering::less);
361 assert(std::compare_weak_order_fallback(nq, pq) == std::weak_ordering::less);
362
363 assert(std::compare_weak_order_fallback(ns, nq) == std::weak_ordering::equivalent);
364 assert(std::compare_weak_order_fallback(ns, ns) == std::weak_ordering::equivalent);
365 for (int i=0; i < 14; ++i) {
366 assert(std::compare_weak_order_fallback(ns, v[i]) == std::weak_ordering::less);
367 }
368 assert(std::compare_weak_order_fallback(ns, ps) == std::weak_ordering::less);
369 assert(std::compare_weak_order_fallback(ns, pq) == std::weak_ordering::less);
370
371 assert(std::compare_weak_order_fallback(ps, nq) == std::weak_ordering::greater);
372 assert(std::compare_weak_order_fallback(ps, ns) == std::weak_ordering::greater);
373 for (int i=0; i < 14; ++i) {
374 assert(std::compare_weak_order_fallback(ps, v[i]) == std::weak_ordering::greater);
375 }
376 assert(std::compare_weak_order_fallback(ps, ps) == std::weak_ordering::equivalent);
377 assert(std::compare_weak_order_fallback(ps, pq) == std::weak_ordering::equivalent);
378
379 assert(std::compare_weak_order_fallback(pq, nq) == std::weak_ordering::greater);
380 assert(std::compare_weak_order_fallback(pq, ns) == std::weak_ordering::greater);
381 for (int i=0; i < 14; ++i) {
382 assert(std::compare_weak_order_fallback(pq, v[i]) == std::weak_ordering::greater);
383 }
384 assert(std::compare_weak_order_fallback(pq, ps) == std::weak_ordering::equivalent);
385 assert(std::compare_weak_order_fallback(pq, pq) == std::weak_ordering::equivalent);
386 }
387
388 return true;
389}
390
391namespace N14 {
392 // Compare to N12::A.
393 struct A {};
394 bool operator==(const A&, const A&);
395 constexpr std::weak_ordering operator<=>(A&, A&&) { return std::weak_ordering::less; }
396 constexpr std::weak_ordering operator<=>(A&&, A&&) { return std::weak_ordering::equivalent; }
397 std::weak_ordering operator<=>(const A&, const A&);
398 static_assert(std::three_way_comparable<A>);
399
400 struct B {
401 std::weak_ordering operator<=>(const B&) const; // lacks operator==
402 };
403 static_assert(!std::three_way_comparable<B>);
404
405 struct C {
406 bool *touched;
407 bool operator==(const C&) const;
408 constexpr std::weak_ordering operator<=>(const C& rhs) const {
409 *rhs.touched = true;
410 return std::weak_ordering::equivalent;
411 }
412 };
413 static_assert(std::three_way_comparable<C>);
414}
415
416constexpr bool test_1_4()
417{
418 // Otherwise, weak_ordering(compare_three_way()(E, F)) if it is a well-formed expression.
419
420 // Test neither weak_order nor compare_three_way const-qualify the forwarded arguments.
421 N14::A a;
422 assert(std::compare_weak_order_fallback(a, std::move(a)) == std::weak_ordering::less);
423 assert(std::compare_weak_order_fallback(std::move(a), std::move(a)) == std::weak_ordering::equivalent);
424
425 N14::B b;
426 static_assert(!has_weak_order(b, b));
427
428 // Test that the arguments are passed to <=> in the correct order.
429 bool c1_touched = false;
430 bool c2_touched = false;
431 N14::C c1 = {.touched: &c1_touched};
432 N14::C c2 = {.touched: &c2_touched};
433 assert(std::compare_weak_order_fallback(c1, c2) == std::weak_ordering::equivalent);
434 assert(!c1_touched);
435 assert(c2_touched);
436
437 return true;
438}
439
440namespace N15 {
441 struct A {};
442 constexpr std::strong_ordering strong_order(A&, A&&) { return std::strong_ordering::less; }
443 constexpr std::strong_ordering strong_order(A&&, A&&) { return std::strong_ordering::equal; }
444 std::strong_ordering strong_order(const A&, const A&);
445
446 struct B {
447 friend std::weak_ordering strong_order(B&, B&);
448 };
449
450 struct WeakOrder {
451 operator std::weak_ordering() const { return std::weak_ordering::less; }
452 };
453 struct C {
454 friend WeakOrder strong_order(C& lhs, C&);
455 };
456
457 struct StrongOrder {
458 constexpr explicit operator std::strong_ordering() const { return std::strong_ordering::less; }
459 operator std::weak_ordering() const = delete;
460 };
461 struct D {
462 bool touched = false;
463 friend constexpr StrongOrder strong_order(D& lhs, D&) { lhs.touched = true; return StrongOrder(); }
464 };
465}
466
467constexpr bool test_1_5()
468{
469 // Otherwise, weak_ordering(strong_order(E, F)) [that is, std::strong_order]
470 // if it is a well-formed expression.
471
472 // Test that weak_order and strong_order do not const-qualify the forwarded arguments.
473 N15::A a;
474 assert(std::compare_weak_order_fallback(a, std::move(a)) == std::weak_ordering::less);
475 assert(std::compare_weak_order_fallback(std::move(a), std::move(a)) == std::weak_ordering::equivalent);
476
477 // The type of ADL strong_order(e,f) must be explicitly convertible to strong_ordering
478 // (not just to weak_ordering), or else std::strong_order(e,f) won't exist.
479 N15::B b;
480 static_assert(!has_weak_order(b, b));
481
482 // The type of ADL strong_order(e,f) must be explicitly convertible to strong_ordering
483 // (not just to weak_ordering), or else std::strong_order(e,f) won't exist.
484 N15::C c;
485 static_assert(!has_weak_order(c, c));
486
487 N15::D d1, d2;
488 ASSERT_SAME_TYPE(decltype(std::compare_weak_order_fallback(d1, d2)), std::weak_ordering);
489 assert(std::compare_weak_order_fallback(d1, d2) == std::weak_ordering::less);
490 assert(d1.touched);
491 assert(!d2.touched);
492
493 return true;
494}
495
496namespace N2 {
497 struct Stats {
498 int eq = 0;
499 int lt = 0;
500 };
501 struct A {
502 Stats *stats_;
503 double value_;
504 constexpr explicit A(Stats *stats, double value) : stats_(stats), value_(value) {}
505 friend constexpr bool operator==(A a, A b) { a.stats_->eq += 1; return a.value_ == b.value_; }
506 friend constexpr bool operator<(A a, A b) { a.stats_->lt += 1; return a.value_ < b.value_; }
507 };
508 struct NoEquality {
509 friend bool operator<(NoEquality, NoEquality);
510 };
511 struct VC1 {
512 // Deliberately asymmetric `const` qualifiers here.
513 friend bool operator==(const VC1&, VC1&);
514 friend bool operator<(const VC1&, VC1&);
515 };
516 struct VC2 {
517 // Deliberately asymmetric `const` qualifiers here.
518 friend bool operator==(const VC2&, VC2&);
519 friend bool operator==(VC2&, const VC2&) = delete;
520 friend bool operator<(const VC2&, VC2&);
521 friend bool operator<(VC2&, const VC2&);
522 };
523}
524
525constexpr bool test_2()
526{
527 {
528 N2::Stats stats;
529 assert(std::compare_weak_order_fallback(N2::A(&stats, 1), N2::A(nullptr, 1)) == std::weak_ordering::equivalent);
530 assert(stats.eq == 1 && stats.lt == 0);
531 stats = {};
532 assert(std::compare_weak_order_fallback(N2::A(&stats, 1), N2::A(nullptr, 2)) == std::weak_ordering::less);
533 assert(stats.eq == 1 && stats.lt == 1);
534 stats = {};
535 assert(std::compare_weak_order_fallback(N2::A(&stats, 2), N2::A(nullptr, 1)) == std::weak_ordering::greater);
536 assert(stats.eq == 1 && stats.lt == 1);
537 }
538 {
539 N2::NoEquality ne;
540 assert(!has_weak_order(ne, ne));
541 }
542 {
543 // LWG3465: (cvc < vc) is well-formed, (vc < cvc) is not. That's fine, for weak ordering.
544 N2::VC1 vc;
545 const N2::VC1 cvc;
546 assert( has_weak_order(cvc, vc));
547 assert(!has_weak_order(vc, cvc));
548 }
549 {
550 // LWG3465: (cvc == vc) is well-formed, (vc == cvc) is not. That's fine.
551 N2::VC2 vc;
552 const N2::VC2 cvc;
553 assert( has_weak_order(cvc, vc));
554 assert(!has_weak_order(vc, cvc));
555 }
556 return true;
557}
558
559int main(int, char**)
560{
561 test_1_1();
562 test_1_2();
563 test_1_3<float>();
564 test_1_3<double>();
565 test_1_3<long double>();
566 test_1_4();
567 test_1_5();
568 test_2();
569
570 static_assert(test_1_3<float>());
571 static_assert(test_1_3<double>());
572 static_assert(test_1_3<long double>());
573 static_assert(test_1_4());
574 static_assert(test_1_5());
575 static_assert(test_2());
576
577 return 0;
578}
579

source code of libcxx/test/std/language.support/cmp/cmp.alg/compare_weak_order_fallback.pass.cpp