| 1 |
// Copyright (C) 2020-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 compile { target c++20 } } |
| 19 |
|
| 20 |
#include <vector> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
|
| 23 |
constexpr bool |
| 24 |
test01() |
| 25 |
{ |
| 26 |
std::vector<int> c1{ 1, 2, 3 }, c2{ 1, 2, 3, 4 }, c3{ 1, 2, 4 }; |
| 27 |
VERIFY( c1 == c1 ); |
| 28 |
VERIFY( std::is_eq(c1 <=> c1) ); |
| 29 |
VERIFY( c1 < c2 ); |
| 30 |
VERIFY( std::is_lt(c1 <=> c2) ); |
| 31 |
VERIFY( c1 < c3 ); |
| 32 |
VERIFY( std::is_lt(c1 <=> c3) ); |
| 33 |
VERIFY( c2 < c3 ); |
| 34 |
VERIFY( std::is_lt(c2 <=> c3) ); |
| 35 |
|
| 36 |
static_assert( std::totally_ordered<std::vector<int>> ); |
| 37 |
|
| 38 |
static_assert( std::three_way_comparable<std::vector<int>, |
| 39 |
std::strong_ordering> ); |
| 40 |
static_assert( ! std::three_way_comparable<std::vector<float>, |
| 41 |
std::strong_ordering> ); |
| 42 |
static_assert( ! std::three_way_comparable<std::vector<float>, |
| 43 |
std::weak_ordering> ); |
| 44 |
static_assert( std::three_way_comparable<std::vector<float>, |
| 45 |
std::partial_ordering> ); |
| 46 |
|
| 47 |
struct E |
| 48 |
{ |
| 49 |
constexpr bool operator==(E) const { return true; } |
| 50 |
}; |
| 51 |
static_assert( ! std::totally_ordered<std::vector<E>> ); |
| 52 |
static_assert( ! std::three_way_comparable<E> ); |
| 53 |
static_assert( ! std::three_way_comparable<std::vector<E>> ); |
| 54 |
|
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
constexpr bool |
| 59 |
test02() |
| 60 |
{ |
| 61 |
struct W |
| 62 |
{ |
| 63 |
int value = 0; |
| 64 |
|
| 65 |
constexpr bool operator==(W rhs) const noexcept |
| 66 |
{ return (value | 1) == (rhs.value | 1); } |
| 67 |
|
| 68 |
constexpr std::weak_ordering |
| 69 |
operator<=>(W rhs) const noexcept |
| 70 |
{ return (value | 1) <=> (rhs.value | 1); } |
| 71 |
}; |
| 72 |
|
| 73 |
static_assert( std::totally_ordered<std::vector<W>> ); |
| 74 |
|
| 75 |
std::vector<W> c1{ {1}, {2}, {3} }, c2{ {0}, {3}, {3} }; |
| 76 |
static_assert( std::same_as<decltype(c1 <=> c1), std::weak_ordering> ); |
| 77 |
VERIFY( c1 == c2 ); |
| 78 |
VERIFY( std::is_eq(c1 <=> c2) ); |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
constexpr bool |
| 84 |
test03() |
| 85 |
{ |
| 86 |
struct P |
| 87 |
{ |
| 88 |
int value = 0; |
| 89 |
|
| 90 |
constexpr bool operator==(P rhs) const noexcept |
| 91 |
{ |
| 92 |
if (value < 0 || rhs.value < 0) |
| 93 |
return false; |
| 94 |
return value == rhs.value; |
| 95 |
} |
| 96 |
|
| 97 |
constexpr std::partial_ordering |
| 98 |
operator<=>(P rhs) const noexcept |
| 99 |
{ |
| 100 |
if (value < 0 || rhs.value < 0) |
| 101 |
return std::partial_ordering::unordered; |
| 102 |
return value <=> rhs.value; |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
static_assert( std::totally_ordered<std::vector<P>> ); |
| 107 |
|
| 108 |
std::vector<P> c{ {1}, {2}, {-3} }; |
| 109 |
static_assert( std::three_way_comparable<P> ); |
| 110 |
static_assert( std::same_as<decltype(c <=> c), std::partial_ordering> ); |
| 111 |
VERIFY( (c <=> c) == std::partial_ordering::unordered ); |
| 112 |
|
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
constexpr bool |
| 117 |
test04() |
| 118 |
{ |
| 119 |
struct L |
| 120 |
{ |
| 121 |
int value = 0; |
| 122 |
|
| 123 |
constexpr bool operator<(L rhs) const noexcept { return value < rhs.value; } |
| 124 |
}; |
| 125 |
|
| 126 |
static_assert( std::totally_ordered<std::vector<L>> ); |
| 127 |
|
| 128 |
std::vector<L> c{ {1}, {2}, {3} }, d{ {1}, {2}, {3}, {4} }; |
| 129 |
static_assert( std::same_as<decltype(c <=> c), std::weak_ordering> ); |
| 130 |
VERIFY( std::is_lt(c <=> d) ); |
| 131 |
|
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
constexpr bool |
| 136 |
test05() |
| 137 |
{ |
| 138 |
// vector iterators are random access, so should support <=> |
| 139 |
|
| 140 |
std::vector<int> c{ 1, 2, 3 }; |
| 141 |
VERIFY( c.begin() == c.cbegin() ); |
| 142 |
VERIFY( std::is_eq(c.begin() <=> c.cbegin()) ); |
| 143 |
|
| 144 |
VERIFY( c.begin() < c.end() ); |
| 145 |
VERIFY( std::is_lt(c.begin() <=> c.end()) ); |
| 146 |
|
| 147 |
VERIFY( c.begin() < c.cend() ); |
| 148 |
VERIFY( std::is_lt(c.begin() <=> c.cend()) ); |
| 149 |
|
| 150 |
VERIFY( c.crbegin() == c.rbegin() ); |
| 151 |
VERIFY( std::is_eq(c.crbegin() <=> c.rbegin()) ); |
| 152 |
|
| 153 |
VERIFY( c.rend() > c.rbegin() ); |
| 154 |
VERIFY( std::is_gt(c.rend() <=> c.rbegin()) ); |
| 155 |
|
| 156 |
static_assert( std::same_as<decltype(c.begin() <=> c.begin()), |
| 157 |
std::strong_ordering> ); |
| 158 |
|
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
static_assert( test01() ); |
| 163 |
static_assert( test02() ); |
| 164 |
static_assert( test03() ); |
| 165 |
static_assert( test04() ); |
| 166 |
static_assert( test05() ); |