Skip to content

Commit f6a40c3

Browse files
committed
Improve tests for assign in std::vector
1 parent 331f3cc commit f6a40c3

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
@@ -19,17 +19,16 @@
1919
#include "asan_testing.h"
2020
#include "test_iterators.h"
2121
#if TEST_STD_VER >= 11
22-
#include "emplace_constructible.h"
23-
#include "container_test_types.h"
22+
# include "emplace_constructible.h"
23+
# include "container_test_types.h"
2424
#endif
2525

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

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

0 commit comments

Comments
 (0)