| 1 |
// { dg-do run { target c++23 } } |
| 2 |
|
| 3 |
#include <map> |
| 4 |
|
| 5 |
#if __cpp_lib_containers_ranges != 202202L |
| 6 |
# error "Feature-test macro __cpp_lib_containers_ranges has wrong value in <map>" |
| 7 |
#endif |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
#include <ranges> |
| 11 |
#include <span> |
| 12 |
#include <testsuite_allocator.h> |
| 13 |
#include <testsuite_hooks.h> |
| 14 |
#include <testsuite_iterators.h> |
| 15 |
#include <tuple> |
| 16 |
|
| 17 |
struct StateCmp { |
| 18 |
int state = 7; |
| 19 |
|
| 20 |
template<typename T, typename U> |
| 21 |
bool operator()(T const& l, U const & r) const |
| 22 |
{ return l > r; } |
| 23 |
}; |
| 24 |
|
| 25 |
void |
| 26 |
test_deduction_guide() |
| 27 |
{ |
| 28 |
__gnu_test::test_input_range<std::pair<long, float>> r(0, 0); |
| 29 |
std::multimap m(std::from_range, r); |
| 30 |
static_assert(std::is_same_v<decltype(m), std::multimap<long, float>>); |
| 31 |
|
| 32 |
StateCmp cmp; |
| 33 |
std::multimap m2(std::from_range, r, cmp); |
| 34 |
static_assert(std::is_same_v<decltype(m2), std::multimap<long, float, StateCmp>>); |
| 35 |
|
| 36 |
using Alloc = __gnu_test::SimpleAllocator<std::pair<const long, float>>; |
| 37 |
Alloc alloc; |
| 38 |
std::multimap m3(std::from_range, r, alloc); |
| 39 |
static_assert(std::is_same_v<decltype(m3), std::multimap<long, float, std::less<long>, Alloc>>); |
| 40 |
|
| 41 |
std::multimap m4(std::from_range, r, cmp, alloc); |
| 42 |
static_assert(std::is_same_v<decltype(m4), std::multimap<long, float, StateCmp, Alloc>>); |
| 43 |
|
| 44 |
__gnu_test::test_input_range<std::pair<const long, const float>> r2(0, 0); |
| 45 |
std::multimap m5(std::from_range, r2); |
| 46 |
static_assert(std::is_same_v<decltype(m5), std::multimap<long, const float>>); |
| 47 |
|
| 48 |
// LWG4223: deduces multimap<const long&, float&> |
| 49 |
//__gnu_test::test_input_range<std::pair<const long&, float&>> r3(0, 0); |
| 50 |
// std::multimap m6(std::from_range, r3); |
| 51 |
|
| 52 |
__gnu_test::test_input_range<std::tuple<long, float>> r4(0, 0); |
| 53 |
std::multimap m7(std::from_range, r4); |
| 54 |
static_assert(std::is_same_v<decltype(m7), std::multimap<long, float>>); |
| 55 |
std::multimap it7(r4.begin(), r4.begin()); |
| 56 |
static_assert(std::is_same_v<decltype(it7), std::multimap<long, float>>); |
| 57 |
} |
| 58 |
|
| 59 |
template<typename T, typename U> |
| 60 |
constexpr bool is_equal(std::less<T>, std::less<U>) |
| 61 |
{ return true; } |
| 62 |
|
| 63 |
constexpr bool is_equal(StateCmp lhs, StateCmp rhs) |
| 64 |
{ return lhs.state = rhs.state; } |
| 65 |
|
| 66 |
constexpr auto get0 = [](auto const& t) { |
| 67 |
using std::get; |
| 68 |
return get<0>(t); |
| 69 |
}; |
| 70 |
|
| 71 |
template<typename Range, typename Alloc, typename Cmp> |
| 72 |
constexpr void |
| 73 |
do_test(Alloc alloc, Cmp cmp) |
| 74 |
{ |
| 75 |
// The multimap's value_type, key_type and multimapped_type. |
| 76 |
using P = typename Alloc::value_type; |
| 77 |
using K = typename P::first_type; |
| 78 |
using V = typename P::second_type; |
| 79 |
|
| 80 |
// The range's value_type. |
| 81 |
using T = std::ranges::range_value_t<Range>; |
| 82 |
T a[]{{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,0}, |
| 83 |
{1,1},{2,2},{3,3},{4,4},{5,5}}; |
| 84 |
|
| 85 |
auto eq = [&](std::multimap<K, V, Cmp, Alloc> const& l, std::span<T> r) { |
| 86 |
if (l.size() != r.size()) |
| 87 |
return false; |
| 88 |
|
| 89 |
std::vector<T> s(r.begin(), r.end()); |
| 90 |
std::ranges::stable_sort(s, cmp, get0); |
| 91 |
for (auto const& [vl, vr] : std::views::zip(l, s)) { |
| 92 |
if (vl != vr) |
| 93 |
return false; |
| 94 |
} |
| 95 |
return true; |
| 96 |
}; |
| 97 |
|
| 98 |
std::multimap<K, V, Cmp, Alloc> m0(std::from_range, Range(a, a+0)); |
| 99 |
VERIFY( m0.empty() ); |
| 100 |
VERIFY( m0.get_allocator() == Alloc() ); |
| 101 |
VERIFY( is_equal(m0.key_comp(), Cmp()) ); |
| 102 |
|
| 103 |
std::multimap<K, V, Cmp, Alloc> m4(std::from_range, Range(a, a+4), cmp); |
| 104 |
VERIFY( eq(m4, {a, 4}) ); |
| 105 |
VERIFY( m4.get_allocator() == Alloc() ); |
| 106 |
VERIFY( is_equal(m4.key_comp(), Cmp()) ); |
| 107 |
|
| 108 |
std::multimap<K, V, Cmp, Alloc> m9(std::from_range, Range(a, a+9), alloc); |
| 109 |
VERIFY( eq(m9, {a, 9}) ); |
| 110 |
VERIFY( m9.get_allocator() == alloc ); |
| 111 |
VERIFY( is_equal(m9.key_comp(), cmp) ); |
| 112 |
|
| 113 |
std::multimap<K, V, Cmp, Alloc> mr(std::from_range, Range(a, a+14), cmp, alloc); |
| 114 |
VERIFY( eq(mr, {a, 14}) ); |
| 115 |
VERIFY( mr.get_allocator() == alloc ); |
| 116 |
VERIFY( is_equal(mr.key_comp(), cmp) ); |
| 117 |
} |
| 118 |
|
| 119 |
template<typename Range> |
| 120 |
void |
| 121 |
do_test_ac() |
| 122 |
{ |
| 123 |
do_test<Range>(std::allocator<std::pair<const int, double>>(), std::less<int>()); |
| 124 |
do_test<Range>(std::allocator<std::pair<const int, double>>(), StateCmp{17}); |
| 125 |
do_test<Range>(__gnu_test::uneq_allocator<std::pair<const int, double>>(42), std::less<int>()); |
| 126 |
do_test<Range>(__gnu_test::uneq_allocator<std::pair<const int, double>>(42), StateCmp{17}); |
| 127 |
} |
| 128 |
|
| 129 |
struct MyPair { |
| 130 |
long x; |
| 131 |
long y; |
| 132 |
|
| 133 |
constexpr operator std::pair<int const, double>() const |
| 134 |
{ return {x, y}; } |
| 135 |
|
| 136 |
template<unsigned I> |
| 137 |
requires (I < 2) |
| 138 |
friend constexpr long get(MyPair p) |
| 139 |
{ return (I == 0) ? p.x : p.y; } |
| 140 |
|
| 141 |
constexpr friend bool operator==(MyPair lhs, std::pair<int const, double> rhs) |
| 142 |
{ return (lhs.x == rhs.first) && (lhs.y == rhs.second); } |
| 143 |
}; |
| 144 |
|
| 145 |
bool |
| 146 |
test_ranges() |
| 147 |
{ |
| 148 |
using namespace __gnu_test; |
| 149 |
|
| 150 |
do_test_ac<test_forward_range<std::pair<int, double>>>(); |
| 151 |
do_test_ac<test_range_nocopy<std::pair<int, double>, input_iterator_wrapper_nocopy>>(); |
| 152 |
do_test_ac<test_forward_range<std::pair<short, float>>>(); |
| 153 |
do_test_ac<test_forward_range<std::tuple<int, double>>>(); |
| 154 |
do_test_ac<test_forward_range<MyPair>>(); |
| 155 |
|
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
int main() |
| 160 |
{ |
| 161 |
test_ranges(); |
| 162 |
} |