Debian Codegraph @

libstdc++-v3/testsuite/23_containers/deque/cons/from_range.cc

raw
1 // { dg-do run { target c++23 } }
2
3 #include <deque>
4
5 #if __cpp_lib_containers_ranges != 202202L
6 # error "Feature-test macro __cpp_lib_containers_ranges has wrong value in <deque>"
7 #endif
8
9 #include <span>
10 #include <testsuite_hooks.h>
11 #include <testsuite_iterators.h>
12 #include <testsuite_allocator.h>
13
14 void
15 test_deduction_guide(long* p)
16 {
17 __gnu_test::test_input_range<long> r(p, p);
18 std::deque d(std::from_range, r);
19 static_assert(std::is_same_v<decltype(d), std::deque<long>>);
20
21 using Alloc = __gnu_test::SimpleAllocator<long>;
22 Alloc alloc;
23 std::deque d2(std::from_range, r, alloc);
24 static_assert(std::is_same_v<decltype(d2), std::deque<long, Alloc>>);
25 }
26
27 template<typename Range, typename Alloc>
28 constexpr void
29 do_test(Alloc alloc)
30 {
31 // The deque's value_type.
32 using V = typename std::allocator_traits<Alloc>::value_type;
33
34 // The range's value_type.
35 using T = std::ranges::range_value_t<Range>;
36 T a[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14};
37
38 auto eq = [](const std::deque<V, Alloc>& l, std::span<T> r) {
39 if (l.size() != r.size())
40 return false;
41 for (auto i = 0u; i < l.size(); ++i)
42 if (l[i] != r[i])
43 return false;
44 return true;
45 };
46
47 std::deque<V, Alloc> d0(std::from_range, Range(a, a+0));
48 VERIFY( d0.empty() );
49 VERIFY( d0.get_allocator() == Alloc() );
50
51 std::deque<V, Alloc> d4(std::from_range, Range(a, a+4));
52 VERIFY( eq(d4, {a, 4}) );
53 VERIFY( d4.get_allocator() == Alloc() );
54
55 std::deque<V, Alloc> d8(std::from_range, Range(a, a+8));
56 VERIFY( eq(d8, {a, 8}) );
57 VERIFY( d8.get_allocator() == Alloc() );
58
59 std::deque<V, Alloc> d9(std::from_range, Range(a, a+14), alloc);
60 VERIFY( eq(d9, {a, 14}) );
61 VERIFY( d9.get_allocator() == alloc );
62 }
63
64 struct EightInBuf
65 {
66 EightInBuf(int x) : elems{x}
67 { }
68
69 private:
70 int elems[512 / (sizeof(int) * 8)];
71
72 friend constexpr bool operator==(EightInBuf const& lhs, int rhs)
73 { return lhs.elems[0] == rhs; }
74 };
75
76
77 template<typename Range>
78 void
79 do_test_a()
80 {
81 do_test<Range>(std::allocator<int>());
82 do_test<Range>(__gnu_test::uneq_allocator<int>(42));
83 do_test<Range>(std::allocator<EightInBuf>());
84 }
85
86 bool
87 test_ranges()
88 {
89 using namespace __gnu_test;
90
91 do_test_a<test_forward_range<int>>();
92 do_test_a<test_forward_sized_range<int>>();
93 do_test_a<test_sized_range_sized_sent<int, forward_iterator_wrapper>>();
94
95 do_test_a<test_input_range<int>>();
96 do_test_a<test_input_sized_range<int>>();
97 do_test_a<test_sized_range_sized_sent<int, input_iterator_wrapper>>();
98
99 do_test_a<test_range_nocopy<int, input_iterator_wrapper_nocopy>>();
100 do_test_a<test_sized_range<int, input_iterator_wrapper_nocopy>>();
101 do_test_a<test_sized_range_sized_sent<int, input_iterator_wrapper_nocopy>>();
102
103 do_test_a<test_forward_range<short>>();
104 do_test_a<test_input_range<short>>();
105
106 // Not lvalue-convertible to int
107 struct C {
108 C(int v) : val(v) { }
109 operator int() && { return val; }
110 bool operator==(int b) const { return b == val; }
111 int val;
112 };
113 using rvalue_input_range = test_range<C, input_iterator_wrapper_rval>;
114 do_test<rvalue_input_range>(std::allocator<int>());
115
116 return true;
117 }
118
119 int main()
120 {
121 test_ranges();
122 }