| 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 <map> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
|
| 23 |
void |
| 24 |
test01() |
| 25 |
{ |
| 26 |
std::multimap<int, int> c1{ {1,1}, {2,1}, {3,1} }; |
| 27 |
std::multimap<int, int> c2{ {1,1}, {2,1}, {3,1}, {4,1} }; |
| 28 |
std::multimap<int, int> c3{ {1,1}, {2,1}, {3,2} }; |
| 29 |
VERIFY( c1 == c1 ); |
| 30 |
VERIFY( std::is_eq(c1 <=> c1) ); |
| 31 |
VERIFY( c1 < c2 ); |
| 32 |
VERIFY( std::is_lt(c1 <=> c2) ); |
| 33 |
VERIFY( c1 < c3 ); |
| 34 |
VERIFY( std::is_lt(c1 <=> c3) ); |
| 35 |
VERIFY( c2 < c3 ); |
| 36 |
VERIFY( std::is_lt(c2 <=> c3) ); |
| 37 |
|
| 38 |
static_assert( std::totally_ordered<std::multimap<int, int>> ); |
| 39 |
|
| 40 |
static_assert( std::three_way_comparable<std::multimap<int, int>, |
| 41 |
std::strong_ordering> ); |
| 42 |
static_assert( ! std::three_way_comparable<std::multimap<int, float>, |
| 43 |
std::strong_ordering> ); |
| 44 |
static_assert( ! std::three_way_comparable<std::multimap<int, float>, |
| 45 |
std::weak_ordering> ); |
| 46 |
static_assert( std::three_way_comparable<std::multimap<int, float>, |
| 47 |
std::partial_ordering> ); |
| 48 |
|
| 49 |
struct E |
| 50 |
{ |
| 51 |
bool operator==(E) { return true; } |
| 52 |
}; |
| 53 |
static_assert( ! std::totally_ordered<std::multimap<int, E>> ); |
| 54 |
static_assert( ! std::three_way_comparable<E> ); |
| 55 |
static_assert( ! std::three_way_comparable<std::multimap<int, E>> ); |
| 56 |
} |
| 57 |
|
| 58 |
void |
| 59 |
test02() |
| 60 |
{ |
| 61 |
struct W |
| 62 |
{ |
| 63 |
int value = 0; |
| 64 |
|
| 65 |
bool operator==(W rhs) const noexcept |
| 66 |
{ return (value | 1) == (rhs.value | 1); } |
| 67 |
|
| 68 |
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::multimap<int, W>> ); |
| 74 |
|
| 75 |
using P = std::pair<const W, W>; |
| 76 |
std::multimap<W, W> c1{ P{1,1}, P{2,2}, P{3,3} }, c2{ P{1,0}, P{3,2}, P{3,3} }; |
| 77 |
static_assert( std::same_as<decltype(c1 <=> c1), std::weak_ordering> ); |
| 78 |
VERIFY( c1 == c2 ); |
| 79 |
VERIFY( std::is_eq(c1 <=> c2) ); |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
test04() |
| 84 |
{ |
| 85 |
struct L |
| 86 |
{ |
| 87 |
int value = 0; |
| 88 |
|
| 89 |
bool operator<(L rhs) const noexcept { return value < rhs.value; } |
| 90 |
}; |
| 91 |
|
| 92 |
static_assert( std::totally_ordered<std::multimap<int, L>> ); |
| 93 |
|
| 94 |
using P = std::pair<const L, L>; |
| 95 |
std::multimap<L, L> c{ P{1,1}, P{2,2}, P{3,3} }, d{ P{1,1}, P{2,2}, P{3,4} }; |
| 96 |
static_assert( std::same_as<decltype(c <=> c), std::weak_ordering> ); |
| 97 |
VERIFY( std::is_lt(c <=> d) ); |
| 98 |
} |
| 99 |
|
| 100 |
// Associative container iterators are not random access |
| 101 |
static_assert( ! std::totally_ordered<std::multimap<int, int>::iterator> ); |
| 102 |
static_assert( ! std::three_way_comparable<std::multimap<int, int>::iterator> ); |
| 103 |
|
| 104 |
int |
| 105 |
main() |
| 106 |
{ |
| 107 |
test01(); |
| 108 |
test02(); |
| 109 |
test04(); |
| 110 |
} |