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<T> R>
13 // constexpr iterator insert_range(const_iterator position, R&& rg); // C++23
17 #include "../../insert_range_sequence_containers.h"
18 #include "asan_testing.h"
19 #include "test_macros.h"
22 // - different kinds of insertions (inserting an {empty/one-element/mid-sized/long range} into an
23 // {empty/one-element/full} container at the {beginning/middle/end});
24 // - inserting move-only elements;
25 // - an exception is thrown when copying the elements or when allocating new elements.
27 constexpr bool test() {
28 for_all_iterators_and_allocators
<int, const int*>([]<class Iter
, class Sent
, class Alloc
>() {
29 test_sequence_insert_range
<std::vector
<int, Alloc
>, Iter
, Sent
>([]([[maybe_unused
]] auto&& c
) {
30 LIBCPP_ASSERT(c
.__invariants());
31 LIBCPP_ASSERT(is_contiguous_container_asan_correct(c
));
34 test_sequence_insert_range_move_only
<std::vector
>();
36 { // Vector may or may not need to reallocate because of the insertion -- make sure to test both cases.
37 { // Ensure reallocation happens.
38 int in
[] = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
39 std::vector
<int> v
= {1, 2, 3, 4, 5, 6, 7, 8};
41 assert(v
.capacity() < v
.size() + std::ranges::size(in
));
43 v
.insert_range(v
.end(), in
);
44 assert(std::ranges::equal(v
, std::array
{1, 2, 3, 4, 5, 6, 7, 8, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}));
47 { // Ensure no reallocation happens.
48 int in
[] = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
49 std::vector
<int> v
= {1, 2, 3, 4, 5, 6, 7, 8};
50 v
.reserve(v
.size() + std::ranges::size(in
));
51 assert(v
.capacity() >= v
.size() + std::ranges::size(in
));
53 v
.insert_range(v
.end(), in
);
54 assert(std::ranges::equal(v
, std::array
{1, 2, 3, 4, 5, 6, 7, 8, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}));
61 int main(int, char**) {
63 static_assert(test());
65 static_assert(test_constraints_insert_range
<std::vector
, int, double>());
67 test_insert_range_exception_safety_throwing_copy
<std::vector
>();
68 test_insert_range_exception_safety_throwing_allocator
<std::vector
, int>();