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