]>
Commit | Line | Data |
---|---|---|
a945c346 | 1 | // Copyright (C) 2019-2024 Free Software Foundation, Inc. |
0ff15d21 JW |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
771752c4 | 18 | // { dg-do run { target c++20 } } |
0ff15d21 JW |
19 | |
20 | #include <compare> | |
21 | #include <limits> | |
22 | #include <testsuite_hooks.h> | |
23 | ||
24 | using std::partial_order; | |
25 | using std::partial_ordering; | |
26 | ||
27 | void | |
28 | test01() | |
29 | { | |
6c3ae88d | 30 | const int one = 1, two = 2; |
0ff15d21 | 31 | |
6c3ae88d JW |
32 | static_assert( partial_order(one, two) == partial_ordering::less ); |
33 | static_assert( partial_order(one, one) == partial_ordering::equivalent ); | |
34 | static_assert( partial_order(two, one) == partial_ordering::greater ); | |
0ff15d21 JW |
35 | static_assert( noexcept(partial_order(1, 1)) ); |
36 | } | |
37 | ||
38 | constexpr partial_ordering different_cv_quals(int i, const int j) | |
39 | { | |
40 | return partial_order(i, j); | |
41 | } | |
42 | ||
43 | void | |
44 | test02() | |
45 | { | |
6c3ae88d JW |
46 | const int fortytwo = 42, nines = 999, lots = 1000; |
47 | static_assert( different_cv_quals(fortytwo, nines) == partial_ordering::less ); | |
48 | static_assert( different_cv_quals(-nines, -nines) == partial_ordering::equivalent ); | |
49 | static_assert( different_cv_quals(-nines, -lots) == partial_ordering::greater ); | |
0ff15d21 JW |
50 | } |
51 | ||
52 | void | |
53 | test03() | |
54 | { | |
6c3ae88d JW |
55 | constexpr double zero = 0.0; |
56 | static_assert( partial_order(zero, zero) == partial_ordering::equivalent ); | |
57 | static_assert( partial_order(-zero, -zero) == partial_ordering::equivalent ); | |
58 | static_assert( partial_order(-zero, zero) == partial_ordering::equivalent ); | |
59 | static_assert( partial_order(zero, -zero) == partial_ordering::equivalent ); | |
0ff15d21 JW |
60 | static_assert( noexcept(partial_order(zero, 1.0)) ); |
61 | static_assert( partial_order(0.0, 1.0) == std::partial_ordering::less ); | |
62 | ||
6c3ae88d JW |
63 | constexpr double min = std::numeric_limits<double>::lowest(); |
64 | constexpr double max = std::numeric_limits<double>::max(); | |
65 | constexpr double nan = std::numeric_limits<double>::quiet_NaN(); | |
66 | constexpr double inf = std::numeric_limits<double>::infinity(); | |
67 | constexpr double denorm = std::numeric_limits<double>::denorm_min(); | |
68 | constexpr double smallest = std::numeric_limits<double>::min(); | |
69 | constexpr double epsilon = std::numeric_limits<double>::epsilon(); | |
70 | static_assert( partial_order(denorm, smallest) == partial_ordering::less ); | |
71 | static_assert( partial_order(denorm, 0.0) == partial_ordering::greater ); | |
4ed1dc12 | 72 | // FIXME: these should all use static_assert. See PR88173. |
0ff15d21 JW |
73 | VERIFY( partial_order(0.0, nan) == partial_ordering::unordered ); |
74 | VERIFY( partial_order(nan, nan) == partial_ordering::unordered ); | |
75 | VERIFY( partial_order(nan, 0.0) == partial_ordering::unordered ); | |
76 | VERIFY( partial_order(-nan, 0.0) == partial_ordering::unordered ); | |
77 | VERIFY( partial_order(-nan, min) == partial_ordering::unordered ); | |
6c3ae88d | 78 | static_assert( partial_order(-inf, min) == partial_ordering::less ); |
0ff15d21 JW |
79 | VERIFY( partial_order(-nan, -inf) == partial_ordering::unordered ); |
80 | VERIFY( partial_order(-inf, -nan) == partial_ordering::unordered ); | |
6c3ae88d JW |
81 | static_assert( partial_order(max, inf) == partial_ordering::less ); |
82 | static_assert( partial_order(inf, max) == partial_ordering::greater ); | |
4ed1dc12 | 83 | VERIFY( partial_order(inf, nan) == partial_ordering::unordered ); |
6c3ae88d | 84 | static_assert( partial_order(1.0, 1.0+epsilon) == partial_ordering::less ); |
0ff15d21 JW |
85 | } |
86 | ||
87 | namespace N | |
88 | { | |
89 | struct X { int i; }; | |
90 | ||
91 | constexpr partial_ordering operator<=>(X l, X r) | |
92 | { | |
93 | if (l.i < 0 && r.i < 0) | |
94 | return partial_ordering::equivalent; | |
95 | return r.i <=> l.i; | |
96 | } | |
3fd1c229 JW |
97 | |
98 | constexpr bool operator==(X l, X r) { return std::is_eq(l <=> r); } | |
99 | ||
100 | static_assert(std::three_way_comparable<X>); | |
0ff15d21 JW |
101 | } |
102 | ||
103 | void | |
104 | test04() | |
105 | { | |
106 | using N::X; | |
107 | X one{1}; | |
108 | X negone{-1}; | |
109 | ||
6c3ae88d | 110 | // FIXME: these should all use static_assert |
0ff15d21 JW |
111 | VERIFY( partial_order(one, X{1}) == partial_ordering::equivalent ); |
112 | VERIFY( partial_order(negone, X{-2}) == partial_ordering::equivalent ); | |
113 | VERIFY( partial_order(one, X{2}) == partial_ordering::greater ); | |
114 | static_assert( !noexcept(partial_order(X{1}, X{2})) ); | |
115 | } | |
116 | ||
117 | int main() | |
118 | { | |
119 | test01(); | |
120 | test02(); | |
121 | test03(); | |
122 | test04(); | |
123 | } |