Debian Codegraph @

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

raw
1 // { dg-do run { target c++23 } }
2
3 #include <vector>
4
5 #if __cpp_lib_containers_ranges != 202202L
6 # error "Feature-test macro __cpp_lib_containers_ranges has wrong value in <vector>"
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::vector v(std::from_range, r);
19 static_assert(std::is_same_v<decltype(v), std::vector<long>>);
20
21 using Alloc = __gnu_test::SimpleAllocator<long>;
22 Alloc alloc;
23 std::vector v2(std::from_range, r, alloc);
24 static_assert(std::is_same_v<decltype(v2), std::vector<long, Alloc>>);
25 }
26
27 template<typename Range, typename Alloc>
28 constexpr void
29 do_test(Alloc alloc)
30 {
31 // The vector'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};
37
38 auto eq = [](const std::vector<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::vector<V, Alloc> v0(std::from_range, Range(a, a+0));
48 VERIFY( v0.empty() );
49 VERIFY( v0.get_allocator() == Alloc() );
50
51 std::vector<V, Alloc> v4(std::from_range, Range(a, a+4));
52 VERIFY( eq(v4, {a, 4}) );
53 VERIFY( v4.get_allocator() == Alloc() );
54
55 std::vector<V, Alloc> v9(std::from_range, Range(a, a+9), alloc);
56 VERIFY( eq(v9, {a, 9}) );
57 VERIFY( v9.get_allocator() == alloc );
58 }
59
60 template<typename Range>
61 void
62 do_test_a()
63 {
64 do_test<Range>(std::allocator<int>());
65 do_test<Range>(__gnu_test::uneq_allocator<int>(42));
66 }
67
68 bool
69 test_ranges()
70 {
71 using namespace __gnu_test;
72
73 do_test_a<test_forward_range<int>>();
74 do_test_a<test_forward_sized_range<int>>();
75 do_test_a<test_sized_range_sized_sent<int, forward_iterator_wrapper>>();
76
77 do_test_a<test_input_range<int>>();
78 do_test_a<test_input_sized_range<int>>();
79 do_test_a<test_sized_range_sized_sent<int, input_iterator_wrapper>>();
80
81 do_test_a<test_range<int, input_iterator_wrapper_nocopy>>();
82 do_test_a<test_sized_range<int, input_iterator_wrapper_nocopy>>();
83 do_test_a<test_sized_range_sized_sent<int, input_iterator_wrapper_nocopy>>();
84
85 do_test_a<test_forward_range<short>>();
86 do_test_a<test_input_range<short>>();
87
88 // Not lvalue-convertible to int
89 struct C {
90 C(int v) : val(v) { }
91 operator int() && { return val; }
92 bool operator==(int b) const { return b == val; }
93 int val;
94 };
95 using rvalue_input_range = test_range<C, input_iterator_wrapper_rval>;
96 do_test<rvalue_input_range>(std::allocator<int>());
97
98 return true;
99 }
100
101 constexpr bool
102 test_constexpr()
103 {
104 // XXX: this doesn't test the non-forward_range code paths are constexpr.
105 do_test<std::span<short>>(std::allocator<int>());
106 return true;
107 }
108
109 void
110 test_pr120367()
111 {
112 #ifdef __cpp_exceptions
113 struct X
114 {
115 X(int) { throw 1; } // Cannot successfully construct an X.
116 ~X() { VERIFY(false); } // So should never need to destroy one.
117 };
118
119 try
120 {
121 int i[1]{};
122 std::vector<X> v(std::from_range, i);
123 }
124 catch (int)
125 {
126 }
127 #endif
128 }
129
130 int main()
131 {
132 test_ranges();
133 static_assert( test_constexpr() );
134 test_pr120367();
135 }