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<T> R>
13// constexpr iterator insert_range(const_iterator position, R&& rg); // C++23
14
15#include <sstream>
16#include <vector>
17
18#include "../../insert_range_sequence_containers.h"
19#include "asan_testing.h"
20#include "test_macros.h"
21
22// Tested cases:
23// - different kinds of insertions (inserting an {empty/one-element/mid-sized/long range} into an
24// {empty/one-element/full} container at the {beginning/middle/end});
25// - inserting move-only elements;
26// - an exception is thrown when copying the elements or when allocating new elements.
27
28constexpr bool test() {
29 for_all_iterators_and_allocators<int, const int*>(f: []<class Iter, class Sent, class Alloc>() {
30 test_sequence_insert_range<std::vector<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
31 LIBCPP_ASSERT(c.__invariants());
32 LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
33 });
34 });
35 test_sequence_insert_range_move_only<std::vector>();
36
37 { // Vector may or may not need to reallocate because of the insertion -- make sure to test both cases.
38 { // Ensure reallocation happens.
39 int in[] = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
40 std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
41 v.shrink_to_fit();
42 assert(v.capacity() < v.size() + std::ranges::size(in));
43
44 v.insert_range(v.end(), in);
45 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}));
46 }
47
48 { // Ensure no reallocation happens.
49 int in[] = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
50 std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
51 v.reserve(v.size() + std::ranges::size(in));
52 assert(v.capacity() >= v.size() + std::ranges::size(in));
53
54 v.insert_range(v.end(), in);
55 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}));
56 }
57
58 { // Ensure input-only sized ranges are accepted.
59 using input_iter = cpp20_input_iterator<const int*>;
60 const int in[]{1, 2, 3, 4};
61 std::vector<int> v{-5, -6};
62 v.insert_range(v.begin(), std::views::counted(input_iter{std::ranges::begin(in)}, std::ranges::ssize(in)));
63 assert(std::ranges::equal(v, std::vector<int>{1, 2, 3, 4, -5, -6}));
64 }
65 }
66
67 { // Ensure that insert_range doesn't use unexpected assignment.
68 struct Wrapper {
69 constexpr Wrapper(int n) : n_(n) {}
70 void operator=(int) = delete;
71
72 int n_;
73 };
74
75 int a[]{1, 2, 3, 4, 5};
76 std::vector<Wrapper> v;
77 v.insert_range(v.end(), a);
78 assert(v.size() == std::size(a));
79 for (std::size_t i = 0; i != std::size(a); ++i)
80 assert(v[i].n_ == a[i]);
81 }
82
83 return true;
84}
85
86#ifndef TEST_HAS_NO_LOCALIZATION
87void test_counted_istream_view() {
88 std::istringstream is{"1 2 3 4"};
89 auto vals = std::views::istream<int>(is);
90 std::vector<int> v;
91 v.insert_range(v.end(), std::views::counted(vals.begin(), 3));
92 assert(v == (std::vector{1, 2, 3}));
93}
94#endif
95
96int main(int, char**) {
97 test();
98 static_assert(test());
99
100 static_assert(test_constraints_insert_range<std::vector, int, double>());
101
102 test_insert_range_exception_safety_throwing_copy<std::vector>();
103 test_insert_range_exception_safety_throwing_allocator<std::vector, int>();
104
105#ifndef TEST_HAS_NO_LOCALIZATION
106 test_counted_istream_view();
107#endif
108
109 return 0;
110}
111

source code of libcxx/test/std/containers/sequences/vector/vector.modifiers/insert_range.pass.cpp