1 // { dg-do compile { target c++20 } }
4 #include <testsuite_hooks.h>
5 #include <testsuite_iterators.h>
8 struct Alloc
: std::allocator
<T
>
10 using std::allocator
<T
>::allocator
;
12 constexpr explicit Alloc(int p
) : personality(p
) { }
15 constexpr Alloc(const Alloc
<U
>& a
) : personality(a
.personality
) { }
17 #if __cplusplus <= 202302L
18 using is_always_equal
= std::false_type
;
23 constexpr Alloc
select_on_container_copy_construction() const
26 constexpr bool operator==(const Alloc
& a
) const noexcept
27 { return personality
== a
.personality
; }
30 namespace default_constructor_global_scope
32 constexpr std::vector
<bool> v1
;
33 static_assert(v1
.size() == 0);
34 static_assert(v1
.capacity() == 0);
36 constexpr std::allocator
<bool> a
;
37 constexpr std::vector
<bool> v2(a
);
38 static_assert(v2
.size() == 0);
39 static_assert(v2
.capacity() == 0);
41 constexpr Alloc
<bool> aa(10);
42 constexpr std::vector
<bool, Alloc
<bool>> v3(aa
);
43 static_assert(v3
.size() == 0);
44 static_assert(v3
.capacity() == 0);
45 static_assert(v3
.get_allocator() == aa
);
49 default_constructor_function_scope()
54 VERIFY(v1
.size() == 0);
55 VERIFY(v1
.capacity() == 0);
57 // vector(const Allocator&)
59 const std::allocator
<bool> a
;
60 std::vector
<bool> v2(a
);
61 VERIFY(v2
.size() == 0);
62 VERIFY(v2
.capacity() == 0);
64 const Alloc
<bool> aa(10);
65 std::vector
<bool, Alloc
<bool>> v3(aa
);
66 VERIFY(v3
.size() == 0);
67 VERIFY(v3
.capacity() == 0);
68 VERIFY(v3
.get_allocator() == aa
);
73 static_assert( default_constructor_function_scope() );
76 sequence_constructors()
78 // vector(size_type, const Allocator& = Allocator())
80 std::vector
<bool> v0(0);
81 VERIFY(v0
.size() == 0);
83 std::vector
<bool> v1(1);
84 VERIFY(v1
.size() == 1);
86 std::vector
<bool> v2(2);
87 VERIFY(v2
.size() == 2);
89 std::vector
<bool> v50(50);
90 VERIFY(v50
.size() == 50);
92 const std::allocator
<bool> a
;
93 std::vector
<bool> a0(0, a
);
94 VERIFY(a0
.size() == 0);
96 std::vector
<bool> a1(1, a
);
97 VERIFY(a1
.size() == 1);
99 std::vector
<bool> a2(2, a
);
100 VERIFY(a2
.size() == 2);
102 std::vector
<bool> a50(50, a
);
103 VERIFY(a50
.size() == 50);
105 const Alloc
<bool> la(10);
106 std::vector
<bool, Alloc
<bool>> l0(0, la
);
107 VERIFY(l0
.size() == 0);
108 VERIFY(l0
.get_allocator() == la
);
110 std::vector
<bool, Alloc
<bool>> l1(1, la
);
111 VERIFY(l1
.size() == 1);
112 VERIFY(l1
.get_allocator() == la
);
114 std::vector
<bool, Alloc
<bool>> l2(2, la
);
115 VERIFY(l2
.size() == 2);
116 VERIFY(l2
.get_allocator() == la
);
118 std::vector
<bool, Alloc
<bool>> l50(50, la
);
119 VERIFY(l50
.size() == 50);
120 VERIFY(l50
.get_allocator() == la
);
122 // vector(size_type, const T&, const Allocator& = Allocator())
124 std::vector
<bool> v3(3, true);
125 VERIFY(v3
.size() == 3);
126 VERIFY(v3
[0] == true && v3
[2] == true);
128 std::vector
<bool> a3(3, false, a
);
129 VERIFY(a3
.size() == 3);
130 VERIFY(a3
[0] == false && a3
[2] == false);
132 std::vector
<bool, Alloc
<bool>> l3(3, true, la
);
133 VERIFY(l3
.size() == 3);
134 VERIFY(l3
[0] == true && l3
[2] == true);
135 VERIFY(l3
.get_allocator() == la
);
140 static_assert(sequence_constructors());
143 iterator_range_constructor()
145 // vector(InputIterator, InputIterator, const Allocator& = Allocator())
147 short range
[3] = { true, false, true };
149 std::vector
<bool> v0(std::begin(range
), std::end(range
));
150 VERIFY(v0
.size() == std::size(range
));
151 VERIFY(v0
[0] == true && v0
[1] == false && v0
[2] == true);
153 const Alloc
<bool> a(5);
154 std::vector
<bool, Alloc
<bool>> l0(std::begin(range
), std::end(range
), a
);
155 VERIFY(l0
.size() == std::size(range
));
156 VERIFY(l0
.get_allocator() == a
);
157 VERIFY(l0
[0] == true && l0
[1] == false && l0
[2] == true);
159 struct input_iterator
161 using iterator_category
= std::input_iterator_tag
;
162 using value_type
= bool;
163 using pointer
= const bool*;
164 using reference
= bool;
165 using difference_type
= int;
167 constexpr input_iterator() : val(0) { }
168 constexpr input_iterator(int i
) : val(i
) { }
170 constexpr input_iterator
& operator++() { --val
; return *this; }
171 constexpr input_iterator
operator++(int) { return {val
--}; }
173 constexpr bool operator*() const { return val
% 2; }
174 constexpr const bool* operator->() const { return nullptr; }
176 constexpr bool operator==(const input_iterator
&) const = default;
181 std::vector
<bool> v1(input_iterator(3), input_iterator());
182 VERIFY(v1
.size() == 3);
183 VERIFY(v1
[0] == true && v1
[1] == false && v1
[2] == true);
185 std::vector
<bool, Alloc
<bool>> l1(input_iterator(2), input_iterator(), a
);
186 VERIFY(l1
.size() == 2);
187 VERIFY(l1
.get_allocator() == a
);
188 VERIFY(l1
[0] == false && l1
[1] == true);
193 static_assert(iterator_range_constructor());
196 initializer_list_constructor()
198 // vector(initializer_list<T>, const Allocator& = Allocator())
200 std::vector
<bool> v0({ false, true, false });
201 VERIFY(v0
.size() == 3);
202 VERIFY(v0
[0] == false && v0
[1] == true && v0
[2] == false);
204 const Alloc
<bool> a(5);
205 std::vector
<bool, Alloc
<bool>> l0({ true, false, false }, a
);
206 VERIFY(l0
.size() == 3);
207 VERIFY(l0
.get_allocator() == a
);
208 VERIFY(l0
[0] == true && l0
[1] == false && l0
[2] == false);
213 static_assert(initializer_list_constructor());
218 const std::vector
<bool> v0({ 1, 0, 0, 1, 0, 1, 1, 0 });
219 const std::vector
<bool, Alloc
<bool>> l0({ 0, 0, 1, 1, 0, 1, 0, 1, 1, 1 });
221 // vector(const vector&)
223 std::vector
<bool> v1(v0
);
224 VERIFY( v1
.size() == v0
.size() );
225 VERIFY( v1
[0] == v0
[0] && v1
[1] == v0
[1] && v1
[2] == v0
[2] );
226 VERIFY( v1
.get_allocator() == v0
.get_allocator() );
228 const Alloc
<bool> as(6);
229 std::vector
<bool, Alloc
<bool>> s1(3, true, as
);
230 std::vector
<bool, Alloc
<bool>> s2(s1
);
231 VERIFY( s2
.size() == s1
.size() );
232 VERIFY( s2
.get_allocator().personality
== -1 );
234 // vector(const vector&, const Allocator&)
236 const Alloc
<bool> a(6);
237 std::vector
<bool, Alloc
<bool>> l1(l0
, a
);
238 VERIFY( l1
.size() == l0
.size() );
239 VERIFY( l1
[0] == l0
[0] && l1
[1] == l0
[1] && l1
[2] == l0
[2] );
240 VERIFY( l1
.get_allocator() == a
);
241 VERIFY( l1
.get_allocator() != l0
.get_allocator() );
246 static_assert(copy_constructor());
251 const std::vector
<bool> v0({ 1, 0, 0, 1, 0, 1, 1, 0 });
252 const std::vector
<bool, Alloc
<bool>> l0({ 0, 0, 1, 1, 0, 1, 0, 1, 1, 1 });
254 // vector(const vector&)
256 std::vector
<bool> v1(v0
);
257 std::vector
<bool> v2(std::move(v1
));
258 VERIFY( v2
.size() == v0
.size() );
259 VERIFY( v1
.empty() );
260 VERIFY( v2
[0] == v0
[0] && v2
[1] == v0
[1] && v2
[2] == v0
[2] );
261 VERIFY( v2
.get_allocator() == v0
.get_allocator() );
263 // vector(const vector&, const Allocator&)
265 const Alloc
<bool> a(6);
266 std::vector
<bool, Alloc
<bool>> l1(l0
);
267 std::vector
<bool, Alloc
<bool>> l2(std::move(l1
), a
);
268 VERIFY( l2
.size() == l0
.size() );
269 VERIFY( l2
[0] == l0
[0] && l2
[1] == l0
[1] && l2
[2] == l0
[2] );
270 VERIFY( l2
.get_allocator() == a
);
271 VERIFY( l2
.get_allocator() != l0
.get_allocator() );
273 std::vector
<bool, Alloc
<bool>> l3(std::move(l2
), a
);
274 VERIFY( l3
.size() == l0
.size() );
275 VERIFY( l3
[0] == l0
[0] && l3
[1] == l0
[1] && l3
[2] == l0
[2] );
276 VERIFY( l3
.get_allocator() == a
);
277 VERIFY( l3
.get_allocator() == l2
.get_allocator() );
282 static_assert(move_constructor());