19
19
#include " asan_testing.h"
20
20
#include " test_iterators.h"
21
21
#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"
24
24
#endif
25
25
26
-
27
26
TEST_CONSTEXPR_CXX20 bool test () {
28
27
#if TEST_STD_VER >= 11
29
28
int arr1[] = {42 };
30
29
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 >;
33
32
using It = forward_iterator<int *>;
34
33
{
35
34
std::vector<T> v;
@@ -44,8 +43,8 @@ TEST_CONSTEXPR_CXX20 bool test() {
44
43
assert (v[2 ].value == 42 );
45
44
}
46
45
}
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 >;
49
48
using It = cpp17_input_iterator<int *>;
50
49
{
51
50
std::vector<T> v;
@@ -64,6 +63,100 @@ TEST_CONSTEXPR_CXX20 bool test() {
64
63
assert (v[2 ].value == 42 );
65
64
}
66
65
}
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 input_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
+ }
67
160
#endif
68
161
69
162
// Test with a number of elements in the source range that is greater than capacity
0 commit comments