| 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 run { target c++20 } } |
| 19 |
|
| 20 |
#include <forward_list> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
|
| 23 |
void |
| 24 |
test01() |
| 25 |
{ |
| 26 |
std::forward_list<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::forward_list<int>> ); |
| 37 |
|
| 38 |
static_assert( std::three_way_comparable<std::forward_list<int>, |
| 39 |
std::strong_ordering> ); |
| 40 |
static_assert( ! std::three_way_comparable<std::forward_list<float>, |
| 41 |
std::strong_ordering> ); |
| 42 |
static_assert( ! std::three_way_comparable<std::forward_list<float>, |
| 43 |
std::weak_ordering> ); |
| 44 |
static_assert( std::three_way_comparable<std::forward_list<float>, |
| 45 |
std::partial_ordering> ); |
| 46 |
|
| 47 |
struct E |
| 48 |
{ |
| 49 |
bool operator==(E) { return true; } |
| 50 |
}; |
| 51 |
static_assert( ! std::totally_ordered<std::forward_list<E>> ); |
| 52 |
static_assert( ! std::three_way_comparable<E> ); |
| 53 |
static_assert( ! std::three_way_comparable<std::forward_list<E>> ); |
| 54 |
} |
| 55 |
|
| 56 |
void |
| 57 |
test02() |
| 58 |
{ |
| 59 |
struct W |
| 60 |
{ |
| 61 |
int value = 0; |
| 62 |
|
| 63 |
bool operator==(W rhs) const noexcept |
| 64 |
{ return (value | 1) == (rhs.value | 1); } |
| 65 |
|
| 66 |
std::weak_ordering |
| 67 |
operator<=>(W rhs) const noexcept |
| 68 |
{ return (value | 1) <=> (rhs.value | 1); } |
| 69 |
}; |
| 70 |
|
| 71 |
static_assert( std::totally_ordered<std::forward_list<W>> ); |
| 72 |
|
| 73 |
std::forward_list<W> c1{ {1}, {2}, {3} }, c2{ {0}, {3}, {3} }; |
| 74 |
static_assert( std::same_as<decltype(c1 <=> c1), std::weak_ordering> ); |
| 75 |
VERIFY( c1 == c2 ); |
| 76 |
VERIFY( std::is_eq(c1 <=> c2) ); |
| 77 |
} |
| 78 |
|
| 79 |
void |
| 80 |
test03() |
| 81 |
{ |
| 82 |
struct P |
| 83 |
{ |
| 84 |
int value = 0; |
| 85 |
|
| 86 |
bool operator==(P rhs) const noexcept |
| 87 |
{ |
| 88 |
if (value < 0 || rhs.value < 0) |
| 89 |
return false; |
| 90 |
return value == rhs.value; |
| 91 |
} |
| 92 |
|
| 93 |
std::partial_ordering |
| 94 |
operator<=>(P rhs) const noexcept |
| 95 |
{ |
| 96 |
if (value < 0 || rhs.value < 0) |
| 97 |
return std::partial_ordering::unordered; |
| 98 |
return value <=> rhs.value; |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
static_assert( std::totally_ordered<std::forward_list<P>> ); |
| 103 |
|
| 104 |
std::forward_list<P> c{ {1}, {2}, {-3} }; |
| 105 |
static_assert( std::three_way_comparable<P> ); |
| 106 |
static_assert( std::same_as<decltype(c <=> c), std::partial_ordering> ); |
| 107 |
VERIFY( (c <=> c) == std::partial_ordering::unordered ); |
| 108 |
} |
| 109 |
|
| 110 |
void |
| 111 |
test04() |
| 112 |
{ |
| 113 |
struct L |
| 114 |
{ |
| 115 |
int value = 0; |
| 116 |
|
| 117 |
bool operator<(L rhs) const noexcept { return value < rhs.value; } |
| 118 |
}; |
| 119 |
|
| 120 |
static_assert( std::totally_ordered<std::forward_list<L>> ); |
| 121 |
|
| 122 |
std::forward_list<L> c{ {1}, {2}, {3} }, d{ {1}, {2}, {3}, {4} }; |
| 123 |
static_assert( std::same_as<decltype(c <=> c), std::weak_ordering> ); |
| 124 |
VERIFY( std::is_lt(c <=> d) ); |
| 125 |
} |
| 126 |
|
| 127 |
static_assert( ! std::totally_ordered<std::forward_list<int>::iterator> ); |
| 128 |
static_assert( ! std::three_way_comparable<std::forward_list<int>::iterator> ); |
| 129 |
|
| 130 |
int |
| 131 |
main() |
| 132 |
{ |
| 133 |
test01(); |
| 134 |
test02(); |
| 135 |
test03(); |
| 136 |
test04(); |
| 137 |
} |