Debian Codegraph @

libstdc++-v3/testsuite/23_containers/priority_queue/cons_from_range.cc

raw
1 // { dg-do run { target c++23 } }
2
3 #include <queue>
4
5 #if __cpp_lib_containers_ranges != 202202L
6 # error "Feature-test macro __cpp_lib_containers_ranges has wrong value in <queue>"
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 <vector>
16
17 struct Gt {
18 template<typename T, typename U>
19 bool operator()(T const& l, U const & r) {
20 return l > r;
21 }
22 };
23
24 void
25 test_deduction_guide(long* p)
26 {
27 __gnu_test::test_input_range<long> r(p, p);
28 std::priority_queue pq(std::from_range, r);
29 static_assert(std::is_same_v<decltype(pq), std::priority_queue<long>>);
30
31 Gt cmp;
32 std::priority_queue pq3(std::from_range, r, cmp);
33 static_assert(std::is_same_v<decltype(pq3), std::priority_queue<long, std::vector<long>, Gt>>);
34
35 using Alloc = __gnu_test::SimpleAllocator<long>;
36 Alloc alloc;
37 std::priority_queue pq2(std::from_range, r, alloc);
38 static_assert(std::is_same_v<decltype(pq2), std::priority_queue<long, std::vector<long, Alloc>>>);
39
40 std::priority_queue pq4(std::from_range, r, cmp, alloc);
41 static_assert(std::is_same_v<decltype(pq4), std::priority_queue<long, std::vector<long, Alloc>, Gt>>);
42 }
43
44 template<typename Range, typename Cont, typename Cmp = std::less<int>>
45 constexpr void
46 do_test(Cmp cmp = Cmp())
47 {
48 // The queue's value_type.
49 using V = typename Cont::value_type;
50
51 // The range's value_type.
52 using T = std::ranges::range_value_t<Range>;
53 T a[]{1,2,3,4,5,6,7,8,9};
54
55 auto eq = [&](std::priority_queue<V, Cont, Cmp>& l, std::span<T> r) {
56 if (l.size() != r.size())
57 return false;
58
59 std::vector<T> s(r.begin(), r.end());
60 std::ranges::sort(s, cmp);
61 for (auto const& v : s | std::views::reverse) {
62 if (v != l.top())
63 return false;
64 l.pop();
65 }
66 return true;
67 };
68
69 std::priority_queue<V, Cont, Cmp> pq0(std::from_range, Range(a, a+0));
70 VERIFY( pq0.empty() );
71
72 std::priority_queue<V, Cont, Cmp> pq4(std::from_range, Range(a, a+4), cmp);
73 VERIFY( eq(pq4, {a, 4}) );
74
75 typename Cont::allocator_type alloc;
76 std::priority_queue<V, Cont, Cmp> pq7(std::from_range, Range(a, a+7), alloc);
77 VERIFY( eq(pq7, {a, 7}) );
78
79 std::priority_queue<V, Cont, Cmp> pq9(std::from_range, Range(a, a+9), cmp, alloc);
80 VERIFY( eq(pq9, {a, 9}) );
81 }
82
83 template<typename T, typename Alloc = std::allocator<T>>
84 struct NoFromRangeCont : std::vector<T, Alloc>
85 {
86 NoFromRangeCont() = default;
87 NoFromRangeCont(const Alloc& a) : std::vector<T, Alloc>(a) {}
88 };
89
90 template<typename Range>
91 void
92 do_test_c()
93 {
94 do_test<Range, std::vector<int>>();
95 do_test<Range, std::vector<int, __gnu_test::SimpleAllocator<int>>>();
96 do_test<Range, std::vector<int>, Gt>();
97 do_test<Range, std::vector<int, __gnu_test::SimpleAllocator<int>>, Gt>();
98 do_test<Range, std::deque<int>>();
99 do_test<Range, NoFromRangeCont<int>>();
100 }
101
102 bool
103 test_ranges()
104 {
105 using namespace __gnu_test;
106
107 do_test_c<test_forward_range<int>>();
108 do_test_c<test_range_nocopy<int, input_iterator_wrapper_nocopy>>();
109 do_test_c<test_forward_range<short>>();
110
111 return true;
112 }
113
114 int main()
115 {
116 test_ranges();
117 }