]>
Commit | Line | Data |
---|---|---|
99dee823 | 1 | // Copyright (C) 2019-2021 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 | ||
18 | // { dg-options "-std=gnu++2a" } | |
19 | // { dg-do run { target c++2a } } | |
20 | ||
21 | #include <compare> | |
22 | #include <limits> | |
23 | #include <testsuite_hooks.h> | |
24 | ||
25 | using std::partial_order; | |
26 | using std::partial_ordering; | |
27 | ||
28 | void | |
29 | test01() | |
30 | { | |
6c3ae88d | 31 | const int one = 1, two = 2; |
0ff15d21 | 32 | |
6c3ae88d JW |
33 | static_assert( partial_order(one, two) == partial_ordering::less ); |
34 | static_assert( partial_order(one, one) == partial_ordering::equivalent ); | |
35 | static_assert( partial_order(two, one) == partial_ordering::greater ); | |
0ff15d21 JW |
36 | static_assert( noexcept(partial_order(1, 1)) ); |
37 | } | |
38 | ||
39 | constexpr partial_ordering different_cv_quals(int i, const int j) | |
40 | { | |
41 | return partial_order(i, j); | |
42 | } | |
43 | ||
44 | void | |
45 | test02() | |
46 | { | |
6c3ae88d JW |
47 | const int fortytwo = 42, nines = 999, lots = 1000; |
48 | static_assert( different_cv_quals(fortytwo, nines) == partial_ordering::less ); | |
49 | static_assert( different_cv_quals(-nines, -nines) == partial_ordering::equivalent ); | |
50 | static_assert( different_cv_quals(-nines, -lots) == partial_ordering::greater ); | |
0ff15d21 JW |
51 | } |
52 | ||
53 | void | |
54 | test03() | |
55 | { | |
6c3ae88d JW |
56 | constexpr double zero = 0.0; |
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 ); | |
60 | static_assert( partial_order(zero, -zero) == partial_ordering::equivalent ); | |
0ff15d21 JW |
61 | static_assert( noexcept(partial_order(zero, 1.0)) ); |
62 | static_assert( partial_order(0.0, 1.0) == std::partial_ordering::less ); | |
63 | ||
6c3ae88d JW |
64 | constexpr double min = std::numeric_limits<double>::lowest(); |
65 | constexpr double max = std::numeric_limits<double>::max(); | |
66 | constexpr double nan = std::numeric_limits<double>::quiet_NaN(); | |
67 | constexpr double inf = std::numeric_limits<double>::infinity(); | |
68 | constexpr double denorm = std::numeric_limits<double>::denorm_min(); | |
69 | constexpr double smallest = std::numeric_limits<double>::min(); | |
70 | constexpr double epsilon = std::numeric_limits<double>::epsilon(); | |
71 | static_assert( partial_order(denorm, smallest) == partial_ordering::less ); | |
72 | static_assert( partial_order(denorm, 0.0) == partial_ordering::greater ); | |
4ed1dc12 | 73 | // FIXME: these should all use static_assert. See PR88173. |
0ff15d21 JW |
74 | VERIFY( partial_order(0.0, nan) == partial_ordering::unordered ); |
75 | VERIFY( partial_order(nan, nan) == partial_ordering::unordered ); | |
76 | VERIFY( partial_order(nan, 0.0) == partial_ordering::unordered ); | |
77 | VERIFY( partial_order(-nan, 0.0) == partial_ordering::unordered ); | |
78 | VERIFY( partial_order(-nan, min) == partial_ordering::unordered ); | |
6c3ae88d | 79 | static_assert( partial_order(-inf, min) == partial_ordering::less ); |
0ff15d21 JW |
80 | VERIFY( partial_order(-nan, -inf) == partial_ordering::unordered ); |
81 | VERIFY( partial_order(-inf, -nan) == partial_ordering::unordered ); | |
6c3ae88d JW |
82 | static_assert( partial_order(max, inf) == partial_ordering::less ); |
83 | static_assert( partial_order(inf, max) == partial_ordering::greater ); | |
4ed1dc12 | 84 | VERIFY( partial_order(inf, nan) == partial_ordering::unordered ); |
6c3ae88d | 85 | static_assert( partial_order(1.0, 1.0+epsilon) == partial_ordering::less ); |
0ff15d21 JW |
86 | } |
87 | ||
88 | namespace N | |
89 | { | |
90 | struct X { int i; }; | |
91 | ||
92 | constexpr partial_ordering operator<=>(X l, X r) | |
93 | { | |
94 | if (l.i < 0 && r.i < 0) | |
95 | return partial_ordering::equivalent; | |
96 | return r.i <=> l.i; | |
97 | } | |
3fd1c229 JW |
98 | |
99 | constexpr bool operator==(X l, X r) { return std::is_eq(l <=> r); } | |
100 | ||
101 | static_assert(std::three_way_comparable<X>); | |
0ff15d21 JW |
102 | } |
103 | ||
104 | void | |
105 | test04() | |
106 | { | |
107 | using N::X; | |
108 | X one{1}; | |
109 | X negone{-1}; | |
110 | ||
6c3ae88d | 111 | // FIXME: these should all use static_assert |
0ff15d21 JW |
112 | VERIFY( partial_order(one, X{1}) == partial_ordering::equivalent ); |
113 | VERIFY( partial_order(negone, X{-2}) == partial_ordering::equivalent ); | |
114 | VERIFY( partial_order(one, X{2}) == partial_ordering::greater ); | |
115 | static_assert( !noexcept(partial_order(X{1}, X{2})) ); | |
116 | } | |
117 | ||
118 | int main() | |
119 | { | |
120 | test01(); | |
121 | test02(); | |
122 | test03(); | |
123 | test04(); | |
124 | } |