Skip to content

Commit 34bbd48

Browse files
committed
Fix test coverage and inline tests for vector<bool>
1 parent df759c7 commit 34bbd48

File tree

4 files changed

+276
-182
lines changed

4 files changed

+276
-182
lines changed

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

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -49,55 +49,50 @@ struct Test {
4949
};
5050

5151
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);
52+
{ // Test cases validating leading/trailing bits unfilled remain unchanged
53+
{ // Leading bits are not filled
54+
std::vector<bool> in(N, false);
55+
std::vector<bool> expected(N, true);
56+
expected[0] = expected[1] = false;
57+
std::fill(in.begin() + 2, in.end(), true);
58+
assert(in == expected);
59+
}
60+
{ // Trailing bits are not filled
61+
std::vector<bool> in(N, false);
62+
std::vector<bool> expected(N, true);
63+
expected[N - 1] = expected[N - 2] = false;
64+
std::fill(in.begin(), in.end() - 2, true);
65+
assert(in == expected);
66+
}
67+
{ // Leading and trailing bits are not filled
68+
std::vector<bool> in(N, false);
69+
std::vector<bool> expected(N, true);
70+
expected[0] = expected[1] = expected[N - 1] = expected[N - 2] = false;
71+
std::fill(in.begin() + 2, in.end() - 2, true);
72+
assert(in == expected);
73+
}
5774
}
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()));
75+
76+
{ // Test cases with full or partial bytes filled
77+
{ // Full bytes filled
78+
std::vector<bool> in(N, false);
79+
std::vector<bool> expected(N, true);
80+
std::fill(in.begin(), in.end(), true);
81+
assert(in == expected);
82+
}
83+
{ // Partial bytes with offset filled
84+
std::vector<bool> in(N, false);
85+
std::vector<bool> expected(N, true);
86+
std::fill(in.begin() + 4, in.end() - 4, true);
87+
std::fill(expected.begin(), expected.begin() + 4, false);
88+
std::fill(expected.end() - 4, expected.end(), false);
89+
assert(in == expected);
90+
}
6391
}
6492

6593
return true;
6694
}
6795

68-
// Make sure std::fill behaves properly with std::vector<bool> iterators with custom size types.
69-
// See https://github.com/llvm/llvm-project/pull/122410.
70-
TEST_CONSTEXPR_CXX20 void test_bititer_with_custom_sized_types() {
71-
{
72-
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
73-
std::vector<bool, Alloc> in(100, false, Alloc(1));
74-
std::vector<bool, Alloc> expected(100, true, Alloc(1));
75-
std::fill(in.begin(), in.end(), true);
76-
assert(in == expected);
77-
}
78-
{
79-
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
80-
std::vector<bool, Alloc> in(200, false, Alloc(1));
81-
std::vector<bool, Alloc> expected(200, true, Alloc(1));
82-
std::fill(in.begin(), in.end(), true);
83-
assert(in == expected);
84-
}
85-
{
86-
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
87-
std::vector<bool, Alloc> in(200, false, Alloc(1));
88-
std::vector<bool, Alloc> expected(200, true, Alloc(1));
89-
std::fill(in.begin(), in.end(), true);
90-
assert(in == expected);
91-
}
92-
{
93-
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
94-
std::vector<bool, Alloc> in(200, false, Alloc(1));
95-
std::vector<bool, Alloc> expected(200, true, Alloc(1));
96-
std::fill(in.begin(), in.end(), true);
97-
assert(in == expected);
98-
}
99-
}
100-
10196
TEST_CONSTEXPR_CXX20 bool test() {
10297
types::for_each(types::forward_iterator_list<char*>(), Test<char>());
10398
types::for_each(types::forward_iterator_list<int*>(), Test<int>());
@@ -110,9 +105,38 @@ TEST_CONSTEXPR_CXX20 bool test() {
110105
assert(test_vector_bool(64));
111106
assert(test_vector_bool(199));
112107
assert(test_vector_bool(256));
113-
}
114108

115-
test_bititer_with_custom_sized_types();
109+
// Make sure std::fill behaves properly with std::vector<bool> iterators with custom size types.
110+
// See https://github.com/llvm/llvm-project/pull/122410.
111+
{
112+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
113+
std::vector<bool, Alloc> in(100, false, Alloc(1));
114+
std::vector<bool, Alloc> expected(100, true, Alloc(1));
115+
std::fill(in.begin(), in.end(), true);
116+
assert(in == expected);
117+
}
118+
{
119+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
120+
std::vector<bool, Alloc> in(200, false, Alloc(1));
121+
std::vector<bool, Alloc> expected(200, true, Alloc(1));
122+
std::fill(in.begin(), in.end(), true);
123+
assert(in == expected);
124+
}
125+
{
126+
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
127+
std::vector<bool, Alloc> in(200, false, Alloc(1));
128+
std::vector<bool, Alloc> expected(200, true, Alloc(1));
129+
std::fill(in.begin(), in.end(), true);
130+
assert(in == expected);
131+
}
132+
{
133+
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
134+
std::vector<bool, Alloc> in(200, false, Alloc(1));
135+
std::vector<bool, Alloc> expected(200, true, Alloc(1));
136+
std::fill(in.begin(), in.end(), true);
137+
assert(in == expected);
138+
}
139+
}
116140

117141
return true;
118142
}

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

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -110,55 +110,50 @@ struct Storage {
110110
};
111111

112112
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);
113+
{ // Test cases validating leading/trailing bits unfilled remain unchanged
114+
{ // Leading bits are not filled
115+
std::vector<bool> in(N, false);
116+
std::vector<bool> expected(N, true);
117+
expected[0] = expected[1] = false;
118+
std::fill_n(in.begin() + 2, N - 2, true);
119+
assert(in == expected);
120+
}
121+
{ // Trailing bits are not filled
122+
std::vector<bool> in(N, false);
123+
std::vector<bool> expected(N, true);
124+
expected[N - 1] = expected[N - 2] = false;
125+
std::fill_n(in.begin(), N - 2, true);
126+
assert(in == expected);
127+
}
128+
{ // Leading and trailing bits are not filled
129+
std::vector<bool> in(N, false);
130+
std::vector<bool> expected(N, true);
131+
expected[0] = expected[1] = expected[N - 1] = expected[N - 2] = false;
132+
std::fill_n(in.begin() + 2, N - 4, true);
133+
assert(in == expected);
134+
}
118135
}
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()));
136+
137+
{ // Test cases with full or partial bytes filled
138+
{ // Full bytes filled
139+
std::vector<bool> in(N, false);
140+
std::vector<bool> expected(N, true);
141+
std::fill_n(in.begin(), N, true);
142+
assert(in == expected);
143+
}
144+
{ // Partial bytes with offset filled
145+
std::vector<bool> in(N, false);
146+
std::vector<bool> expected(N, true);
147+
std::fill_n(in.begin() + 4, N - 8, true);
148+
std::fill_n(expected.begin(), 4, false);
149+
std::fill_n(expected.end() - 4, 4, false);
150+
assert(in == expected);
151+
}
124152
}
125153

126154
return true;
127155
}
128156

129-
// Make sure std::fill_n behaves properly with std::vector<bool> iterators with custom size types.
130-
// See https://github.com/llvm/llvm-project/pull/122410.
131-
TEST_CONSTEXPR_CXX20 void test_bititer_with_custom_sized_types() {
132-
{
133-
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
134-
std::vector<bool, Alloc> in(100, false, Alloc(1));
135-
std::vector<bool, Alloc> expected(100, true, Alloc(1));
136-
std::fill_n(in.begin(), in.size(), true);
137-
assert(in == expected);
138-
}
139-
{
140-
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
141-
std::vector<bool, Alloc> in(200, false, Alloc(1));
142-
std::vector<bool, Alloc> expected(200, true, Alloc(1));
143-
std::fill_n(in.begin(), in.size(), true);
144-
assert(in == expected);
145-
}
146-
{
147-
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
148-
std::vector<bool, Alloc> in(200, false, Alloc(1));
149-
std::vector<bool, Alloc> expected(200, true, Alloc(1));
150-
std::fill_n(in.begin(), in.size(), true);
151-
assert(in == expected);
152-
}
153-
{
154-
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
155-
std::vector<bool, Alloc> in(200, false, Alloc(1));
156-
std::vector<bool, Alloc> expected(200, true, Alloc(1));
157-
std::fill_n(in.begin(), in.size(), true);
158-
assert(in == expected);
159-
}
160-
}
161-
162157
TEST_CONSTEXPR_CXX20 void test_struct_array() {
163158
{
164159
A a[3];
@@ -197,9 +192,38 @@ TEST_CONSTEXPR_CXX20 bool test() {
197192
assert(test_vector_bool(64));
198193
assert(test_vector_bool(199));
199194
assert(test_vector_bool(256));
200-
}
201195

202-
test_bititer_with_custom_sized_types();
196+
// Make sure std::fill_n behaves properly with std::vector<bool> iterators with custom size types.
197+
// See https://github.com/llvm/llvm-project/pull/122410.
198+
{
199+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
200+
std::vector<bool, Alloc> in(100, false, Alloc(1));
201+
std::vector<bool, Alloc> expected(100, true, Alloc(1));
202+
std::fill_n(in.begin(), in.size(), true);
203+
assert(in == expected);
204+
}
205+
{
206+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
207+
std::vector<bool, Alloc> in(200, false, Alloc(1));
208+
std::vector<bool, Alloc> expected(200, true, Alloc(1));
209+
std::fill_n(in.begin(), in.size(), true);
210+
assert(in == expected);
211+
}
212+
{
213+
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
214+
std::vector<bool, Alloc> in(200, false, Alloc(1));
215+
std::vector<bool, Alloc> expected(200, true, Alloc(1));
216+
std::fill_n(in.begin(), in.size(), true);
217+
assert(in == expected);
218+
}
219+
{
220+
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
221+
std::vector<bool, Alloc> in(200, false, Alloc(1));
222+
std::vector<bool, Alloc> expected(200, true, Alloc(1));
223+
std::fill_n(in.begin(), in.size(), true);
224+
assert(in == expected);
225+
}
226+
}
203227

204228
return true;
205229
}

0 commit comments

Comments
 (0)