20
20
#include < cassert>
21
21
#include < iterator>
22
22
#include < memory>
23
+ #include < vector>
23
24
24
25
#include " MoveOnly.h"
25
26
#include " test_iterators.h"
@@ -45,15 +46,15 @@ struct Test {
45
46
template <class OutIter >
46
47
TEST_CONSTEXPR_CXX20 void operator ()() {
47
48
const unsigned N = 1000 ;
48
- int ia[N] = {};
49
+ int ia[N] = {};
49
50
for (unsigned i = 0 ; i < N; ++i)
50
- ia[i] = i;
51
+ ia[i] = i;
51
52
int ib[N] = {0 };
52
53
53
- OutIter r = std::move (InIter (ia), InIter (ia+ N), OutIter (ib));
54
- assert (base (r) == ib+ N);
54
+ OutIter r = std::move (InIter (ia), InIter (ia + N), OutIter (ib));
55
+ assert (base (r) == ib + N);
55
56
for (unsigned i = 0 ; i < N; ++i)
56
- assert (ia[i] == ib[i]);
57
+ assert (ia[i] == ib[i]);
57
58
}
58
59
};
59
60
@@ -73,13 +74,13 @@ struct Test1 {
73
74
const unsigned N = 100 ;
74
75
std::unique_ptr<int > ia[N];
75
76
for (unsigned i = 0 ; i < N; ++i)
76
- ia[i].reset (new int (i));
77
+ ia[i].reset (new int (i));
77
78
std::unique_ptr<int > ib[N];
78
79
79
- OutIter r = std::move (InIter (ia), InIter (ia+ N), OutIter (ib));
80
- assert (base (r) == ib+ N);
80
+ OutIter r = std::move (InIter (ia), InIter (ia + N), OutIter (ib));
81
+ assert (base (r) == ib + N);
81
82
for (unsigned i = 0 ; i < N; ++i)
82
- assert (*ib[i] == static_cast <int >(i));
83
+ assert (*ib[i] == static_cast <int >(i));
83
84
}
84
85
};
85
86
@@ -92,6 +93,26 @@ struct Test1OutIters {
92
93
}
93
94
};
94
95
96
+ TEST_CONSTEXPR_CXX20 bool test_vector_bool (std::size_t N) {
97
+ std::vector<bool > in (N, false );
98
+ for (std::size_t i = 0 ; i < N; i += 2 )
99
+ in[i] = true ;
100
+
101
+ { // Test move with aligned bytes
102
+ std::vector<bool > out (N);
103
+ std::move (in.begin (), in.end (), out.begin ());
104
+ assert (in == out);
105
+ }
106
+ { // Test move with unaligned bytes
107
+ std::vector<bool > out (N + 8 );
108
+ std::move (in.begin (), in.end (), out.begin () + 4 );
109
+ for (std::size_t i = 0 ; i < N; ++i)
110
+ assert (out[i + 4 ] == in[i]);
111
+ }
112
+
113
+ return true ;
114
+ }
115
+
95
116
TEST_CONSTEXPR_CXX20 bool test () {
96
117
types::for_each (types::cpp17_input_iterator_list<int *>(), TestOutIters ());
97
118
if (TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED)
@@ -118,7 +139,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
118
139
// When non-trivial
119
140
{
120
141
MoveOnly from[3 ] = {1 , 2 , 3 };
121
- MoveOnly to[3 ] = {};
142
+ MoveOnly to[3 ] = {};
122
143
std::move (std::begin (from), std::end (from), std::begin (to));
123
144
assert (to[0 ] == MoveOnly (1 ));
124
145
assert (to[1 ] == MoveOnly (2 ));
@@ -127,14 +148,24 @@ TEST_CONSTEXPR_CXX20 bool test() {
127
148
// When trivial
128
149
{
129
150
TrivialMoveOnly from[3 ] = {1 , 2 , 3 };
130
- TrivialMoveOnly to[3 ] = {};
151
+ TrivialMoveOnly to[3 ] = {};
131
152
std::move (std::begin (from), std::end (from), std::begin (to));
132
153
assert (to[0 ] == TrivialMoveOnly (1 ));
133
154
assert (to[1 ] == TrivialMoveOnly (2 ));
134
155
assert (to[2 ] == TrivialMoveOnly (3 ));
135
156
}
136
157
}
137
158
159
+ { // Test vector<bool>::iterator optimization
160
+ assert (test_vector_bool (8 ));
161
+ assert (test_vector_bool (19 ));
162
+ assert (test_vector_bool (32 ));
163
+ assert (test_vector_bool (49 ));
164
+ assert (test_vector_bool (64 ));
165
+ assert (test_vector_bool (199 ));
166
+ assert (test_vector_bool (256 ));
167
+ }
168
+
138
169
return true ;
139
170
}
140
171
0 commit comments