Debian Codegraph @

libstdc++-v3/testsuite/23_containers/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 <list>
10 #include <span>
11 #include <testsuite_allocator.h>
12 #include <testsuite_hooks.h>
13 #include <testsuite_iterators.h>
14
15 void
16 test_deduction_guide(long* p)
17 {
18 __gnu_test::test_input_range<long> r(p, p);
19 std::queue q(std::from_range, r);
20 static_assert(std::is_same_v<decltype(q), std::queue<long>>);
21
22 using Alloc = __gnu_test::SimpleAllocator<long>;
23 Alloc alloc;
24 std::queue q2(std::from_range, r, alloc);
25 static_assert(std::is_same_v<decltype(q2), std::queue<long, std::deque<long, Alloc>>>);
26 }
27
28 template<typename Range, typename Cont>
29 constexpr void
30 do_test()
31 {
32 // The queue's value_type.
33 using V = typename Cont::value_type;
34
35 // The range's value_type.
36 using T = std::ranges::range_value_t<Range>;
37 T a[]{1,2,3,4,5,6,7,8,9};
38
39 auto eq = [](std::queue<V, Cont>& l, std::span<T> r) {
40 if (l.size() != r.size())
41 return false;
42 for (auto const& v : r) {
43 if (v != l.front())
44 return false;
45 l.pop();
46 }
47 return true;
48 };
49
50 std::queue<V, Cont> q0(std::from_range, Range(a, a+0));
51 VERIFY( q0.empty() );
52
53 std::queue<V, Cont> q4(std::from_range, Range(a, a+4));
54 VERIFY( eq(q4, {a, 4}) );
55
56 typename Cont::allocator_type alloc;
57 std::queue<V, Cont> q9(std::from_range, Range(a, a+9), alloc);
58 VERIFY( eq(q9, {a, 9}) );
59 }
60
61 template<typename T, typename Alloc = std::allocator<T>>
62 struct NoFromRangeCont : std::deque<T, Alloc>
63 {
64 NoFromRangeCont() = default;
65 NoFromRangeCont(const Alloc& a) : std::deque<T, Alloc>(a) {}
66 };
67
68 template<typename Range>
69 void
70 do_test_c()
71 {
72 do_test<Range, std::deque<int>>();
73 do_test<Range, std::deque<int, __gnu_test::SimpleAllocator<int>>>();
74 do_test<Range, std::list<int>>();
75 do_test<Range, NoFromRangeCont<int>>();
76 }
77
78 bool
79 test_ranges()
80 {
81 using namespace __gnu_test;
82
83 do_test_c<test_forward_range<int>>();
84 do_test_c<test_range_nocopy<int, input_iterator_wrapper_nocopy>>();
85 do_test_c<test_forward_range<short>>();
86
87 return true;
88 }
89
90 int main()
91 {
92 test_ranges();
93 }