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// <list>
10
11// template <InputIterator Iter>
12// iterator insert(const_iterator position, Iter first, Iter last); // constexpr since C++26
13
14#include <list>
15#include <cstdlib>
16#include <cassert>
17
18#include "test_macros.h"
19#include "test_iterators.h"
20#include "min_allocator.h"
21#include "count_new.h"
22
23template <class List>
24TEST_CONSTEXPR_CXX26 void test() {
25 int a1[] = {1, 2, 3};
26 List l1;
27 typename List::iterator i = l1.insert(l1.begin(), a1, a1 + 3);
28 assert(i == l1.begin());
29 assert(l1.size() == 3);
30 assert(std::distance(l1.begin(), l1.end()) == 3);
31 i = l1.begin();
32 assert(*i == 1);
33 ++i;
34 assert(*i == 2);
35 ++i;
36 assert(*i == 3);
37 int a2[] = {4, 5, 6};
38 i = l1.insert(i, a2, a2 + 3);
39 assert(*i == 4);
40 assert(l1.size() == 6);
41 assert(std::distance(l1.begin(), l1.end()) == 6);
42 i = l1.begin();
43 assert(*i == 1);
44 ++i;
45 assert(*i == 2);
46 ++i;
47 assert(*i == 4);
48 ++i;
49 assert(*i == 5);
50 ++i;
51 assert(*i == 6);
52 ++i;
53 assert(*i == 3);
54
55#if !defined(TEST_HAS_NO_EXCEPTIONS) && !defined(DISABLE_NEW_COUNT)
56 if (!TEST_IS_CONSTANT_EVALUATED) {
57 globalMemCounter.throw_after = 2;
58 int save_count = globalMemCounter.outstanding_new;
59 try {
60 i = l1.insert(i, a2, a2 + 3);
61 assert(false);
62 } catch (...) {
63 }
64 assert(globalMemCounter.checkOutstandingNewEq(save_count));
65 assert(l1.size() == 6);
66 assert(std::distance(l1.begin(), l1.end()) == 6);
67 i = l1.begin();
68 assert(*i == 1);
69 ++i;
70 assert(*i == 2);
71 ++i;
72 assert(*i == 4);
73 ++i;
74 assert(*i == 5);
75 ++i;
76 assert(*i == 6);
77 ++i;
78 assert(*i == 3);
79 }
80#endif
81}
82
83TEST_CONSTEXPR_CXX26 bool test() {
84 test<std::list<int> >();
85#if TEST_STD_VER >= 11
86 test<std::list<int, min_allocator<int>>>();
87#endif
88
89 return true;
90}
91
92int main(int, char**) {
93 assert(test());
94#if TEST_STD_VER >= 26
95 static_assert(test());
96#endif
97
98 return 0;
99}
100

source code of libcxx/test/std/containers/sequences/list/list.modifiers/insert_iter_iter_iter.pass.cpp