Skip to content

Commit fe70499

Browse files
committed
Add test cases for odd-sized vector<bool>
1 parent aa949ce commit fe70499

File tree

7 files changed

+133
-86
lines changed

7 files changed

+133
-86
lines changed

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

+24-9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ struct Test {
4848
}
4949
};
5050

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

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

100115
test_bititer_with_custom_sized_types();

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

+25-10
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ struct Storage {
109109
};
110110
};
111111

112+
TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
113+
{ // Test with full bytes
114+
std::vector<bool> in(N, false);
115+
std::vector<bool> expected(N, true);
116+
std::fill_n(in.begin(), N, true);
117+
assert(in == expected);
118+
}
119+
{ // Test with partial bytes with offset
120+
std::vector<bool> in(N, false);
121+
std::vector<bool> expected(N, true);
122+
std::fill_n(in.begin() + 4, N - 4, true);
123+
assert(std::equal(in.begin() + 4, in.end() - 4, expected.begin()));
124+
}
125+
126+
return true;
127+
}
128+
112129
// Make sure std::fill_n behaves properly with std::vector<bool> iterators with custom size types.
113130
// See https://github.com/llvm/llvm-project/pull/122410.
114131
TEST_CONSTEXPR_CXX20 void test_bititer_with_custom_sized_types() {
@@ -173,17 +190,15 @@ TEST_CONSTEXPR_CXX20 bool test() {
173190
test_int_array_struct_source();
174191

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

189204
return true;

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

+25-10
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

+24-9
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)