Skip to content

Commit e478ce3

Browse files
committed
Improve tests for assign in std::vector
1 parent 669f704 commit e478ce3

File tree

1 file changed

+100
-7
lines changed

1 file changed

+100
-7
lines changed

libcxx/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp

Lines changed: 100 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818
#include "asan_testing.h"
1919
#include "test_iterators.h"
2020
#if TEST_STD_VER >= 11
21-
#include "emplace_constructible.h"
22-
#include "container_test_types.h"
21+
# include "emplace_constructible.h"
22+
# include "container_test_types.h"
2323
#endif
2424

25-
2625
TEST_CONSTEXPR_CXX20 bool test() {
2726
#if TEST_STD_VER >= 11
2827
int arr1[] = {42};
2928
int arr2[] = {1, 101, 42};
30-
{
31-
using T = EmplaceConstructibleMoveableAndAssignable<int>;
29+
{ // Test with new_size > capacity() == 0 for forward_iterator, resulting in reallocation during assign
30+
using T = EmplaceConstructibleMoveableAndAssignable<int>;
3231
using It = forward_iterator<int*>;
3332
{
3433
std::vector<T> v;
@@ -43,8 +42,8 @@ TEST_CONSTEXPR_CXX20 bool test() {
4342
assert(v[2].value == 42);
4443
}
4544
}
46-
{
47-
using T = EmplaceConstructibleMoveableAndAssignable<int>;
45+
{ // Test with new_size > capacity() == 0 for input_iterator, resulting in reallocation during assign
46+
using T = EmplaceConstructibleMoveableAndAssignable<int>;
4847
using It = cpp17_input_iterator<int*>;
4948
{
5049
std::vector<T> v;
@@ -63,6 +62,100 @@ TEST_CONSTEXPR_CXX20 bool test() {
6362
assert(v[2].value == 42);
6463
}
6564
}
65+
66+
{ // Test with new_size < size() for forward_iterator, resulting in destruction at end during assign
67+
using T = EmplaceConstructibleMoveableAndAssignable<int>;
68+
using It = forward_iterator<int*>;
69+
{
70+
std::vector<T> v;
71+
v.reserve(5);
72+
for (std::size_t i = 0; i < v.capacity(); ++i)
73+
v.emplace_back(99);
74+
v.assign(It(arr1), It(std::end(arr1)));
75+
assert(v.size() == 1);
76+
assert(v[0].value == 42);
77+
}
78+
{
79+
std::vector<T> v;
80+
v.reserve(5);
81+
for (std::size_t i = 0; i < v.capacity(); ++i)
82+
v.emplace_back(99);
83+
v.assign(It(arr2), It(std::end(arr2)));
84+
assert(v.size() == 3);
85+
assert(v[0].value == 1);
86+
assert(v[1].value == 101);
87+
assert(v[2].value == 42);
88+
}
89+
}
90+
{ // Test with new_size < size() for input_iterator, resulting in destruction at end during assign
91+
using T = EmplaceConstructibleMoveableAndAssignable<int>;
92+
using It = cpp17_input_iterator<int*>;
93+
{
94+
std::vector<T> v;
95+
v.reserve(5);
96+
for (std::size_t i = 0; i < v.capacity(); ++i)
97+
v.emplace_back(99);
98+
v.assign(It(arr1), It(std::end(arr1)));
99+
assert(v.size() == 1);
100+
assert(v[0].value == 42);
101+
}
102+
{
103+
std::vector<T> v;
104+
v.reserve(5);
105+
for (std::size_t i = 0; i < v.capacity(); ++i)
106+
v.emplace_back(99);
107+
v.assign(It(arr2), It(std::end(arr2)));
108+
assert(v.size() == 3);
109+
assert(v[0].value == 1);
110+
assert(v[1].value == 101);
111+
assert(v[2].value == 42);
112+
}
113+
}
114+
115+
{ // Test with size() < new_size < capacity() for forward_iterator, resulting in construction at end during assign
116+
using T = EmplaceConstructibleMoveableAndAssignable<int>;
117+
using It = forward_iterator<int*>;
118+
{
119+
std::vector<T> v;
120+
v.reserve(5);
121+
v.assign(It(arr1), It(std::end(arr1)));
122+
assert(v.size() == 1);
123+
assert(v[0].value == 42);
124+
}
125+
{
126+
std::vector<T> v;
127+
v.reserve(5);
128+
for (std::size_t i = 0; i < 2; ++i)
129+
v.emplace_back(99);
130+
v.assign(It(arr2), It(std::end(arr2)));
131+
assert(v.size() == 3);
132+
assert(v[0].value == 1);
133+
assert(v[1].value == 101);
134+
assert(v[2].value == 42);
135+
}
136+
}
137+
{ // Test with size() < new_size < capacity() for inputs_iterator, resulting in construction at end during assign
138+
using T = EmplaceConstructibleMoveableAndAssignable<int>;
139+
using It = cpp17_input_iterator<int*>;
140+
{
141+
std::vector<T> v;
142+
v.reserve(5);
143+
v.assign(It(arr1), It(std::end(arr1)));
144+
assert(v.size() == 1);
145+
assert(v[0].value == 42);
146+
}
147+
{
148+
std::vector<T> v;
149+
v.reserve(5);
150+
for (std::size_t i = 0; i < 2; ++i)
151+
v.emplace_back(99);
152+
v.assign(It(arr2), It(std::end(arr2)));
153+
assert(v.size() == 3);
154+
assert(v[0].value == 1);
155+
assert(v[1].value == 101);
156+
assert(v[2].value == 42);
157+
}
158+
}
66159
#endif
67160

68161
// Test with a number of elements in the source range that is greater than capacity

0 commit comments

Comments
 (0)