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// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=2000000
11
12// template<container-compatible-range<bool> R>
13// constexpr void append_range(R&& rg); // C++23
14
15#include <vector>
16
17#include "../insert_range_sequence_containers.h"
18#include "test_macros.h"
19
20// Tested cases:
21// - different kinds of insertions (appending an {empty/one-element/mid-sized/long range} into an
22// {empty/one-element/full} container);
23// - an exception is thrown when allocating new elements.
24constexpr bool test() {
25 static_assert(test_constraints_append_range<std::vector, bool, char>());
26
27 for_all_iterators_and_allocators<bool, const int*>([]<class Iter, class Sent, class Alloc>() {
28 test_sequence_append_range<std::vector<bool, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
29 LIBCPP_ASSERT(c.__invariants());
30 // `is_contiguous_container_asan_correct` doesn't work on `vector<bool>`.
31 });
32 });
33
34 { // Vector may or may not need to reallocate because of the insertion -- make sure to test both cases.
35 { // Ensure reallocation happens.
36 constexpr int N = 255;
37 bool in[N] = {};
38 std::vector<bool> v = {0, 0, 0, 1, 1, 0, 0, 0};
39 auto initial = v;
40 assert(v.capacity() < v.size() + std::ranges::size(in));
41
42 v.append_range(in);
43 // Because `in` is very large (it has to be to exceed the large capacity that `vector<bool>` allocates), it is
44 // impractical to have the expected value as a literal.
45 assert(v.size() == initial.size() + N);
46 assert(std::ranges::equal(v.begin(), v.begin() + initial.size(), initial.begin(), initial.end()));
47 assert(std::ranges::equal(v.begin() + initial.size(), v.end(), std::ranges::begin(in), std::ranges::end(in)));
48 }
49
50 { // Ensure no reallocation happens.
51 bool in[] = {1, 1, 1, 1, 0, 0, 1, 1, 1, 1};
52 std::vector<bool> v = {0, 0, 0, 1, 1, 0, 0, 0};
53 v.reserve(v.size() + std::ranges::size(in));
54 assert(v.capacity() >= v.size() + std::ranges::size(in));
55
56 v.append_range(in);
57 assert(std::ranges::equal(v, std::vector<bool>{0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1}));
58 }
59 }
60
61 return true;
62}
63
64int main(int, char**) {
65 test();
66 static_assert(test());
67
68 // Note: `test_append_range_exception_safety_throwing_copy` doesn't apply because copying booleans cannot throw.
69 test_append_range_exception_safety_throwing_allocator<std::vector, bool>();
70
71 return 0;
72}
73

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