1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10 // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=2000000
12 // template<container-compatible-range<bool> R>
13 // constexpr void append_range(R&& rg); // C++23
17 #include "../insert_range_sequence_containers.h"
18 #include "test_macros.h"
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.
24 constexpr bool test() {
25 static_assert(test_constraints_append_range
<std::vector
, bool, char>());
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>`.
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;
38 std::vector
<bool> v
= {0, 0, 0, 1, 1, 0, 0, 0};
40 assert(v
.capacity() < v
.size() + std::ranges::size(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
)));
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
));
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}));
64 int main(int, char**) {
66 static_assert(test());
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>();