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
18TEST_CONSTEXPR_CXX20 bool tests() {
19 { // Test with various cases where assign may or may not trigger reallocations
20 { // Reallocation happens
21 std::size_t N = 128;
22 std::vector<bool> v(5, false);
23 assert(v.capacity() < N);
24 v.assign(n: N, x: 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 within current size
30 std::size_t N = 5;
31 std::vector<bool> v(2 * N, false);
32 v.assign(n: N, x: 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 within spare space
38 std::size_t N = 5;
39 std::vector<bool> v(N / 2, false);
40 v.reserve(n: N * 2);
41 v.assign(n: N, x: 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
51int main(int, char**) {
52 tests();
53#if TEST_STD_VER > 17
54 static_assert(tests());
55#endif
56 return 0;
57}
58

source code of libcxx/test/std/containers/sequences/vector.bool/assign_size_value.pass.cpp