Debian Codegraph @

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

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