Skip to content

Commit bd830c2

Browse files
committed
Add test cases for odd-sized vector<bool>
1 parent 83ae1ac commit bd830c2

File tree

7 files changed

+132
-85
lines changed

7 files changed

+132
-85
lines changed

libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/fill.pass.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ struct Test {
4747
}
4848
};
4949

50+
TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
51+
{ // Test with full bytes
52+
std::vector<bool> in(N, false);
53+
std::vector<bool> expected(N, true);
54+
std::fill(in.begin(), in.end(), true);
55+
assert(in == expected);
56+
}
57+
{ // Test with partial bytes with offset
58+
std::vector<bool> in(N, false);
59+
std::vector<bool> expected(N, true);
60+
std::fill(in.begin() + 4, in.end() - 4, true);
61+
assert(std::equal(in.begin() + 4, in.end() - 4, expected.begin()));
62+
}
63+
64+
return true;
65+
}
66+
5067
// Make sure std::fill behaves properly with std::vector<bool> iterators with custom size types.
5168
// See https://github.com/llvm/llvm-project/pull/122410.
5269
TEST_CONSTEXPR_CXX20 void test_bititer_with_custom_sized_types() {
@@ -85,15 +102,13 @@ TEST_CONSTEXPR_CXX20 bool test() {
85102
types::for_each(types::forward_iterator_list<int*>(), Test<int>());
86103

87104
{ // Test vector<bool>::iterator optimization
88-
for (std::size_t N = 8; N <= 256; N *= 2) {
89-
// Test with both full and partial bytes
90-
for (std::size_t offset = 0; offset <= 4; offset += 4) {
91-
std::vector<bool> in(N + 2 * offset);
92-
std::vector<bool> expected(N, true);
93-
std::fill(in.begin() + offset, in.end() - offset, true);
94-
assert(std::equal(in.begin() + offset, in.end() - offset, expected.begin()));
95-
}
96-
}
105+
assert(test_vector_bool(8));
106+
assert(test_vector_bool(19));
107+
assert(test_vector_bool(32));
108+
assert(test_vector_bool(49));
109+
assert(test_vector_bool(64));
110+
assert(test_vector_bool(199));
111+
assert(test_vector_bool(256));
97112
}
98113

99114
test_bititer_with_custom_sized_types();

libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ void test6() {
129129
std::fill_n(&foo[0], UDI(5), Storage());
130130
}
131131

132+
TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
133+
{ // Test with full bytes
134+
std::vector<bool> in(N, false);
135+
std::vector<bool> expected(N, true);
136+
std::fill_n(in.begin(), N, true);
137+
assert(in == expected);
138+
}
139+
{ // Test with partial bytes with offset
140+
std::vector<bool> in(N, false);
141+
std::vector<bool> expected(N, true);
142+
std::fill_n(in.begin() + 4, N - 4, true);
143+
assert(std::equal(in.begin() + 4, in.end() - 4, expected.begin()));
144+
}
145+
146+
return true;
147+
}
148+
132149
// Make sure std::fill_n behaves properly with std::vector<bool> iterators with custom size types.
133150
// See https://github.com/llvm/llvm-project/pull/122410.
134151
TEST_CONSTEXPR_CXX20 void test_bititer_with_custom_sized_types() {
@@ -183,15 +200,13 @@ int main(int, char**) {
183200
test6();
184201

185202
{ // Test vector<bool>::iterator optimization
186-
for (std::size_t N = 8; N <= 256; N *= 2) {
187-
// Test with both full and partial bytes
188-
for (std::size_t offset = 0; offset <= 4; offset += 4) {
189-
std::vector<bool> in(N + 2 * offset);
190-
std::vector<bool> expected(N, true);
191-
std::fill_n(in.begin() + offset, N + offset, true);
192-
assert(std::equal(in.begin() + offset, in.end() - offset, expected.begin()));
193-
}
194-
}
203+
assert(test_vector_bool(8));
204+
assert(test_vector_bool(19));
205+
assert(test_vector_bool(32));
206+
assert(test_vector_bool(49));
207+
assert(test_vector_bool(64));
208+
assert(test_vector_bool(199));
209+
assert(test_vector_bool(256));
195210
}
196211

197212
test_bititer_with_custom_sized_types();

libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/ranges.fill.pass.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,23 @@ TEST_CONSTEXPR_CXX20 void test_bititer_with_custom_sized_types() {
115115
assert(in == expected);
116116
}
117117
}
118+
119+
constexpr bool test_vector_bool(std::size_t N) {
120+
{ // Test with full bytes
121+
std::vector<bool> in(N, false);
122+
std::vector<bool> expected(N, true);
123+
std::ranges::fill(std::ranges::begin(in), std::ranges::end(in), true);
124+
assert(in == expected);
125+
}
126+
{ // Test with partial bytes with offset
127+
std::vector<bool> in(N, false);
128+
std::vector<bool> expected(N, true);
129+
std::ranges::fill(std::ranges::begin(in) + 4, std::ranges::end(in) - 4, true);
130+
assert(std::equal(in.begin() + 4, in.end() - 4, expected.begin()));
131+
}
132+
133+
return true;
134+
}
118135
#endif
119136

120137
constexpr bool test() {
@@ -173,17 +190,15 @@ constexpr bool test() {
173190
}
174191
}
175192

176-
#if TEST_STD_VER >= 23
193+
#if TEST_STD_VER >= 23
177194
{ // Test vector<bool>::iterator optimization
178-
for (std::size_t N = 8; N <= 256; N *= 2) {
179-
// Test with both full and partial bytes
180-
for (std::size_t offset : {0, 4}) {
181-
std::vector<bool> in(N + 2 * offset);
182-
std::vector<bool> expected(N, true);
183-
std::ranges::fill(std::ranges::begin(in) + offset, std::ranges::end(in) - offset, true);
184-
assert(std::equal(in.begin() + offset, in.end() - offset, expected.begin()));
185-
}
186-
}
195+
assert(test_vector_bool(8));
196+
assert(test_vector_bool(19));
197+
assert(test_vector_bool(32));
198+
assert(test_vector_bool(49));
199+
assert(test_vector_bool(64));
200+
assert(test_vector_bool(199));
201+
assert(test_vector_bool(256));
187202
}
188203

189204
test_bititer_with_custom_sized_types();

libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/ranges.fill_n.pass.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ TEST_CONSTEXPR_CXX20 void test_bititer_with_custom_sized_types() {
8888
assert(in == expected);
8989
}
9090
}
91+
92+
constexpr bool test_vector_bool(std::size_t N) {
93+
{ // Test with full bytes
94+
std::vector<bool> in(N, false);
95+
std::vector<bool> expected(N, true);
96+
std::ranges::fill_n(std::ranges::begin(in), N, true);
97+
assert(in == expected);
98+
}
99+
{ // Test with partial bytes with offset
100+
std::vector<bool> in(N, false);
101+
std::vector<bool> expected(N, true);
102+
std::ranges::fill_n(std::ranges::begin(in) + 4, N - 4, true);
103+
assert(std::equal(in.begin() + 4, in.end(), expected.begin()));
104+
}
105+
106+
return true;
107+
}
91108
#endif
92109

93110
constexpr bool test() {
@@ -123,15 +140,13 @@ constexpr bool test() {
123140

124141
#if TEST_STD_VER >= 23
125142
{ // Test vector<bool>::iterator optimization
126-
for (std::size_t N = 8; N <= 256; N *= 2) {
127-
// Test with both full and partial bytes
128-
for (std::size_t offset : {0, 4}) {
129-
std::vector<bool> in(N + 2 * offset);
130-
std::vector<bool> expected(N, true);
131-
std::ranges::fill_n(std::ranges::begin(in) + offset, N, true);
132-
assert(std::equal(in.begin() + offset, in.end() - offset, expected.begin()));
133-
}
134-
}
143+
assert(test_vector_bool(8));
144+
assert(test_vector_bool(19));
145+
assert(test_vector_bool(32));
146+
assert(test_vector_bool(49));
147+
assert(test_vector_bool(64));
148+
assert(test_vector_bool(199));
149+
assert(test_vector_bool(256));
135150
}
136151

137152
test_bititer_with_custom_sized_types();

0 commit comments

Comments
 (0)