Skip to content

Commit ab4f159

Browse files
committed
Add test cases for odd-sized vector<bool>
1 parent 907d665 commit ab4f159

File tree

4 files changed

+100
-36
lines changed

4 files changed

+100
-36
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
@@ -46,20 +46,35 @@ struct Test {
4646
}
4747
};
4848

49+
TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
50+
{ // Test with full bytes
51+
std::vector<bool> in(N, false);
52+
std::vector<bool> expected(N, true);
53+
std::fill(in.begin(), in.end(), true);
54+
assert(in == expected);
55+
}
56+
{ // Test with partial bytes
57+
std::vector<bool> in(N, false);
58+
std::vector<bool> expected(N, true);
59+
std::fill(in.begin() + 4, in.end() - 4, true);
60+
assert(std::equal(in.begin() + 4, in.end() - 4, expected.begin()));
61+
}
62+
63+
return true;
64+
}
65+
4966
TEST_CONSTEXPR_CXX20 bool test() {
5067
types::for_each(types::forward_iterator_list<char*>(), Test<char>());
5168
types::for_each(types::forward_iterator_list<int*>(), Test<int>());
5269

5370
{ // Test vector<bool>::iterator optimization
54-
for (std::size_t N = 8; N <= 256; N *= 2) {
55-
// Test with both full and partial bytes
56-
for (std::size_t offset = 0; offset <= 4; offset += 4) {
57-
std::vector<bool> in(N + 2 * offset);
58-
std::vector<bool> expected(N, true);
59-
std::fill(in.begin() + offset, in.end() - offset, true);
60-
assert(std::equal(in.begin() + offset, in.end() - offset, expected.begin()));
61-
}
62-
}
71+
assert(test_vector_bool(8));
72+
assert(test_vector_bool(19));
73+
assert(test_vector_bool(32));
74+
assert(test_vector_bool(49));
75+
assert(test_vector_bool(64));
76+
assert(test_vector_bool(199));
77+
assert(test_vector_bool(256));
6378
}
6479

6580
return true;

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
@@ -128,6 +128,23 @@ void test6() {
128128
std::fill_n(&foo[0], UDI(5), Storage());
129129
}
130130

131+
TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
132+
{ // Test with full bytes
133+
std::vector<bool> in(N, false);
134+
std::vector<bool> expected(N, true);
135+
std::fill_n(in.begin(), N, true);
136+
assert(in == expected);
137+
}
138+
{ // Test with partial bytes
139+
std::vector<bool> in(N, false);
140+
std::vector<bool> expected(N, true);
141+
std::fill_n(in.begin() + 4, N - 4, true);
142+
assert(std::equal(in.begin() + 4, in.end() - 4, expected.begin()));
143+
}
144+
145+
return true;
146+
}
147+
131148
int main(int, char**) {
132149
test_char<cpp17_output_iterator<char*> >();
133150
test_char<forward_iterator<char*> >();
@@ -149,15 +166,13 @@ int main(int, char**) {
149166
test6();
150167

151168
{ // Test vector<bool>::iterator optimization
152-
for (std::size_t N = 8; N <= 256; N *= 2) {
153-
// Test with both full and partial bytes
154-
for (std::size_t offset = 0; offset <= 4; offset += 4) {
155-
std::vector<bool> in(N + 2 * offset);
156-
std::vector<bool> expected(N, true);
157-
std::fill_n(in.begin() + offset, N + offset, true);
158-
assert(std::equal(in.begin() + offset, in.end() - offset, expected.begin()));
159-
}
160-
}
169+
assert(test_vector_bool(8));
170+
assert(test_vector_bool(19));
171+
assert(test_vector_bool(32));
172+
assert(test_vector_bool(49));
173+
assert(test_vector_bool(64));
174+
assert(test_vector_bool(199));
175+
assert(test_vector_bool(256));
161176
}
162177

163178
#if TEST_STD_VER > 17

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ constexpr void test_iterators() {
7777
}
7878
}
7979

80+
#if TEST_STD_VER >= 23
81+
constexpr bool test_vector_bool(std::size_t N) {
82+
{ // Test with full bytes
83+
std::vector<bool> in(N, false);
84+
std::vector<bool> expected(N, true);
85+
std::ranges::fill(std::ranges::begin(in), std::ranges::end(in), true);
86+
assert(in == expected);
87+
}
88+
{ // Test with partial bytes
89+
std::vector<bool> in(N, false);
90+
std::vector<bool> expected(N, true);
91+
std::ranges::fill(std::ranges::begin(in) + 4, std::ranges::end(in) - 4, true);
92+
assert(std::equal(in.begin() + 4, in.end() - 4, expected.begin()));
93+
}
94+
95+
return true;
96+
}
97+
#endif
98+
8099
constexpr bool test() {
81100
test_iterators<cpp17_output_iterator<int*>, sentinel_wrapper<cpp17_output_iterator<int*>>>();
82101
test_iterators<cpp20_output_iterator<int*>, sentinel_wrapper<cpp20_output_iterator<int*>>>();
@@ -135,15 +154,13 @@ constexpr bool test() {
135154

136155
#if TEST_STD_VER >= 23
137156
{ // Test vector<bool>::iterator optimization
138-
for (std::size_t N = 8; N <= 256; N *= 2) {
139-
// Test with both full and partial bytes
140-
for (std::size_t offset : {0, 4}) {
141-
std::vector<bool> in(N + 2 * offset);
142-
std::vector<bool> expected(N, true);
143-
std::ranges::fill(std::ranges::begin(in) + offset, std::ranges::end(in) - offset, true);
144-
assert(std::equal(in.begin() + offset, in.end() - offset, expected.begin()));
145-
}
146-
}
157+
assert(test_vector_bool(8));
158+
assert(test_vector_bool(19));
159+
assert(test_vector_bool(32));
160+
assert(test_vector_bool(49));
161+
assert(test_vector_bool(64));
162+
assert(test_vector_bool(199));
163+
assert(test_vector_bool(256));
147164
}
148165
#endif
149166

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@ constexpr void test_iterators() {
5050
}
5151
}
5252

53+
#if TEST_STD_VER >= 23
54+
constexpr bool test_vector_bool(std::size_t N) {
55+
{ // Test with full bytes
56+
std::vector<bool> in(N, false);
57+
std::vector<bool> expected(N, true);
58+
std::ranges::fill_n(std::ranges::begin(in), N, true);
59+
assert(in == expected);
60+
}
61+
{ // Test with partial bytes
62+
std::vector<bool> in(N, false);
63+
std::vector<bool> expected(N, true);
64+
std::ranges::fill_n(std::ranges::begin(in) + 4, N - 4, true);
65+
assert(std::equal(in.begin() + 4, in.end(), expected.begin()));
66+
}
67+
68+
return true;
69+
}
70+
#endif
71+
5372
constexpr bool test() {
5473
test_iterators<cpp17_output_iterator<int*>, sentinel_wrapper<cpp17_output_iterator<int*>>>();
5574
test_iterators<cpp20_output_iterator<int*>, sentinel_wrapper<cpp20_output_iterator<int*>>>();
@@ -83,15 +102,13 @@ constexpr bool test() {
83102

84103
#if TEST_STD_VER >= 23
85104
{ // Test vector<bool>::iterator optimization
86-
for (std::size_t N = 8; N <= 256; N *= 2) {
87-
// Test with both full and partial bytes
88-
for (std::size_t offset : {0, 4}) {
89-
std::vector<bool> in(N + 2 * offset);
90-
std::vector<bool> expected(N, true);
91-
std::ranges::fill_n(std::ranges::begin(in) + offset, N, true);
92-
assert(std::equal(in.begin() + offset, in.end() - offset, expected.begin()));
93-
}
94-
}
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));
95112
}
96113
#endif
97114

0 commit comments

Comments
 (0)