| 1 | // { dg-do run { target c++23 } } |
| 2 | |
| 3 | #include <algorithm> |
| 4 | #include <map> |
| 5 | #include <ranges> |
| 6 | #include <span> |
| 7 | #include <testsuite_allocator.h> |
| 8 | #include <testsuite_hooks.h> |
| 9 | #include <testsuite_iterators.h> |
| 10 | #include <tuple> |
| 11 | |
| 12 | struct Gt { |
| 13 | template<typename T, typename U> |
| 14 | bool operator()(T const& l, U const & r) const |
| 15 | { return l > r; } |
| 16 | }; |
| 17 | |
| 18 | constexpr auto get0 = [](auto const& t) { |
| 19 | using std::get; |
| 20 | return get<0>(t); |
| 21 | }; |
| 22 | |
| 23 | template<typename Range, typename K, typename V, typename Cmp> |
| 24 | constexpr void |
| 25 | do_test(Cmp cmp = Cmp()) |
| 26 | { |
| 27 | // The range's value_type. |
| 28 | using T = std::ranges::range_value_t<Range>; |
| 29 | T a[]{{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,0}, |
| 30 | {1,1},{2,2},{3,3},{4,4},{5,5}}; |
| 31 | |
| 32 | auto eq = [&](std::map<K, V, Cmp> const& l, std::span<T> r) { |
| 33 | if (l.size() != r.size()) |
| 34 | return false; |
| 35 | |
| 36 | std::vector<T> s(r.begin(), r.end()); |
| 37 | std::ranges::sort(s, cmp, get0); |
| 38 | for (auto const& [vl, vr] : std::views::zip(l, s)) { |
| 39 | if (vl != vr) |
| 40 | return false; |
| 41 | } |
| 42 | return true; |
| 43 | }; |
| 44 | |
| 45 | std::map<K, V, Cmp> s; |
| 46 | VERIFY( s.empty() ); |
| 47 | |
| 48 | s.insert_range(Range(a, a+4)); |
| 49 | VERIFY( eq(s, {a, 4}) ); |
| 50 | |
| 51 | s.insert_range(Range(a+4, a+9)); |
| 52 | VERIFY( eq(s, {a, 9}) ); |
| 53 | |
| 54 | s.insert_range(Range(a, a+14)); |
| 55 | VERIFY( eq(s, {a, 9}) ); |
| 56 | |
| 57 | s.insert_range(Range(a, a+14)); |
| 58 | VERIFY( eq(s, {a, 9}) ); |
| 59 | } |
| 60 | |
| 61 | template<typename Range> |
| 62 | void |
| 63 | do_test_c() |
| 64 | { |
| 65 | do_test<Range, int, double, std::less<int>>(); |
| 66 | do_test<Range, int, double, Gt>(); |
| 67 | } |
| 68 | |
| 69 | struct MyPair { |
| 70 | long x; |
| 71 | long y; |
| 72 | |
| 73 | constexpr operator std::pair<int const, double>() const |
| 74 | { return {x, y}; } |
| 75 | |
| 76 | template<unsigned I> |
| 77 | requires (I < 2) |
| 78 | friend constexpr long get(MyPair p) |
| 79 | { return (I == 0) ? p.x : p.y; } |
| 80 | |
| 81 | constexpr friend bool operator==(MyPair lhs, std::pair<int const, double> rhs) |
| 82 | { return (lhs.x == rhs.first) && (lhs.y == rhs.second); } |
| 83 | }; |
| 84 | |
| 85 | bool |
| 86 | test_ranges() |
| 87 | { |
| 88 | using namespace __gnu_test; |
| 89 | |
| 90 | do_test_c<test_forward_range<std::pair<int, double>>>(); |
| 91 | do_test_c<test_range_nocopy<std::pair<int, double>, input_iterator_wrapper_nocopy>>(); |
| 92 | do_test_c<test_forward_range<std::pair<short, float>>>(); |
| 93 | do_test_c<test_forward_range<std::tuple<int, double>>>(); |
| 94 | do_test_c<test_forward_range<MyPair>>(); |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | int main() |
| 100 | { |
| 101 | test_ranges(); |
| 102 | } |