Skip to content

Commit 7c90e0e

Browse files
committed
Add test coverage for vector<bool>::assign
1 parent f6a40c3 commit 7c90e0e

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <vector>
10+
11+
// void assign(_InputIterator __first, _InputIterator __last);
12+
// void assign(_ForwardIterator __first, _ForwardIterator __last);
13+
14+
#include <vector>
15+
#include <cassert>
16+
#include "test_macros.h"
17+
#include "test_iterators.h"
18+
19+
TEST_CONSTEXPR_CXX20 bool tests() {
20+
{ // Test with various cases where assign may or may not trigger reallocations for forward_iterator
21+
{ // Reallocation happens
22+
std::vector<bool> in(128, true);
23+
std::vector<bool> v(5, false);
24+
assert(v.capacity() < in.size());
25+
using It = forward_iterator<std::vector<bool>::iterator>;
26+
v.assign(It(in.begin()), It(in.end()));
27+
assert(v == in);
28+
}
29+
{ // No reallocation: fit wintin current size
30+
bool in[] = {false, true, false, true, true};
31+
TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]);
32+
std::vector<bool> v(2 * N, false);
33+
using It = forward_iterator<bool*>;
34+
v.assign(It(in), It(in + N));
35+
assert(v.size() == N);
36+
for (std::size_t i = 0; i < N; ++i)
37+
assert(v[i] == in[i]);
38+
}
39+
{ // No reallocation: fit wintin spare space
40+
bool in[] = {false, true, false, true, true};
41+
TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]);
42+
std::vector<bool> v(N / 2, false);
43+
v.reserve(N * 2);
44+
using It = forward_iterator<bool*>;
45+
v.assign(It(in), It(in + N));
46+
assert(v.size() == N);
47+
for (std::size_t i = 0; i < N; ++i)
48+
assert(v[i] == in[i]);
49+
}
50+
}
51+
52+
{ // Test with various cases where assign may or may not trigger reallocations for input_iterator
53+
{ // Reallocation happens
54+
std::vector<bool> in(128, true);
55+
std::vector<bool> v(5, false);
56+
assert(v.capacity() < in.size());
57+
using It = cpp17_input_iterator<std::vector<bool>::iterator>;
58+
v.assign(It(in.begin()), It(in.end()));
59+
assert(v == in);
60+
}
61+
{ // No reallocation: fit wintin current size
62+
bool in[] = {false, true, false, true, true};
63+
TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]);
64+
std::vector<bool> v(2 * N, false);
65+
using It = cpp17_input_iterator<bool*>;
66+
v.assign(It(in), It(in + N));
67+
assert(v.size() == N);
68+
for (std::size_t i = 0; i < N; ++i)
69+
assert(v[i] == in[i]);
70+
}
71+
{ // No reallocation: fit wintin spare space
72+
bool in[] = {false, true, false, true, true};
73+
TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]);
74+
std::vector<bool> v(N / 2, false);
75+
v.reserve(N * 2);
76+
using It = cpp17_input_iterator<bool*>;
77+
v.assign(It(in), It(in + N));
78+
assert(v.size() == N);
79+
for (std::size_t i = 0; i < N; ++i)
80+
assert(v[i] == in[i]);
81+
}
82+
}
83+
84+
return true;
85+
}
86+
87+
int main(int, char**) {
88+
tests();
89+
#if TEST_STD_VER > 17
90+
static_assert(tests());
91+
#endif
92+
return 0;
93+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <vector>
10+
11+
// void assign(size_type __n, const value_type& __x);
12+
13+
#include <vector>
14+
#include <cassert>
15+
#include "test_macros.h"
16+
#include "test_iterators.h"
17+
18+
TEST_CONSTEXPR_CXX20 bool tests() {
19+
{ // Test with various cases where assign may or may not trigger reallocations
20+
{ // Reallocation happens
21+
TEST_CONSTEXPR std::size_t N = 128;
22+
std::vector<bool> v(5, false);
23+
assert(v.capacity() < N);
24+
v.assign(N, true);
25+
assert(v.size() == N);
26+
for (std::size_t i = 0; i < N; ++i)
27+
assert(v[i] == true);
28+
}
29+
{ // No reallocation: fit wintin current size
30+
TEST_CONSTEXPR std::size_t N = 5;
31+
std::vector<bool> v(2 * N, false);
32+
v.assign(N, true);
33+
assert(v.size() == N);
34+
for (std::size_t i = 0; i < N; ++i)
35+
assert(v[i] == true);
36+
}
37+
{ // No reallocation: fit wintin spare space
38+
TEST_CONSTEXPR std::size_t N = 5;
39+
std::vector<bool> v(N / 2, false);
40+
v.reserve(N * 2);
41+
v.assign(N, true);
42+
assert(v.size() == N);
43+
for (std::size_t i = 0; i < N; ++i)
44+
assert(v[i] == true);
45+
}
46+
}
47+
48+
return true;
49+
}
50+
51+
int main(int, char**) {
52+
tests();
53+
#if TEST_STD_VER > 17
54+
static_assert(tests());
55+
#endif
56+
return 0;
57+
}

libcxx/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
135135
assert(v[2].value == 42);
136136
}
137137
}
138-
{ // Test with size() < new_size < capacity() for inputs_iterator, resulting in construction at end during assign
138+
{ // Test with size() < new_size < capacity() for input_iterator, resulting in construction at end during assign
139139
using T = EmplaceConstructibleMoveableAndAssignable<int>;
140140
using It = cpp17_input_iterator<int*>;
141141
{

0 commit comments

Comments
 (0)