| 1 | // { dg-do run { target c++23 } } |
| 2 | |
| 3 | #include <algorithm> |
| 4 | #include <queue> |
| 5 | #include <ranges> |
| 6 | #include <span> |
| 7 | #include <testsuite_allocator.h> |
| 8 | #include <testsuite_hooks.h> |
| 9 | #include <testsuite_iterators.h> |
| 10 | #include <vector> |
| 11 | |
| 12 | struct Gt { |
| 13 | template<typename T, typename U> |
| 14 | bool operator()(T const& l, U const & r) { |
| 15 | return l > r; |
| 16 | } |
| 17 | }; |
| 18 | |
| 19 | template<typename Range, typename Cont, typename Cmp = std::less<int>> |
| 20 | constexpr void |
| 21 | do_test(Cmp cmp = Cmp()) |
| 22 | { |
| 23 | // The queue's value_type. |
| 24 | using V = typename Cont::value_type; |
| 25 | |
| 26 | // The range's value_type. |
| 27 | using T = std::ranges::range_value_t<Range>; |
| 28 | T a[]{1,2,3,4,5,6,7,8,9}; |
| 29 | |
| 30 | auto eq = [&](std::priority_queue<V, Cont, Cmp> l, std::span<T> r) { |
| 31 | if (l.size() != r.size()) |
| 32 | return false; |
| 33 | |
| 34 | std::vector<T> s(r.begin(), r.end()); |
| 35 | std::ranges::sort(s, cmp); |
| 36 | for (auto const& v : s | std::views::reverse) { |
| 37 | if (v != l.top()) |
| 38 | return false; |
| 39 | l.pop(); |
| 40 | } |
| 41 | return true; |
| 42 | }; |
| 43 | |
| 44 | std::priority_queue<V, Cont, Cmp> pq(std::from_range, Range(a, a+0)); |
| 45 | pq.push_range(Range(a, a+0)); |
| 46 | VERIFY( pq.empty() ); |
| 47 | |
| 48 | pq.push_range(Range(a, a+4)); |
| 49 | VERIFY( eq(pq, {a, 4}) ); |
| 50 | |
| 51 | pq.push_range(Range(a+4, a+9)); |
| 52 | VERIFY( eq(pq, {a, 9}) ); |
| 53 | } |
| 54 | |
| 55 | template<typename T, typename Alloc = std::allocator<T>> |
| 56 | struct NoAppendRangeCont : std::vector<T, Alloc> |
| 57 | { |
| 58 | template<typename R> |
| 59 | void append_range(R&&) = delete; |
| 60 | }; |
| 61 | |
| 62 | template<typename Range> |
| 63 | void |
| 64 | do_test_c() |
| 65 | { |
| 66 | do_test<Range, std::vector<int>>(); |
| 67 | do_test<Range, std::vector<int>, Gt>(); |
| 68 | do_test<Range, std::deque<int>>(); |
| 69 | do_test<Range, NoAppendRangeCont<int>>(); |
| 70 | } |
| 71 | |
| 72 | bool |
| 73 | test_ranges() |
| 74 | { |
| 75 | using namespace __gnu_test; |
| 76 | |
| 77 | do_test_c<test_forward_range<int>>(); |
| 78 | do_test_c<test_range_nocopy<int, input_iterator_wrapper_nocopy>>(); |
| 79 | do_test_c<test_forward_range<short>>(); |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | int main() |
| 85 | { |
| 86 | test_ranges(); |
| 87 | } |