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