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// iterator insert(const_iterator position, const value_type& x); // constexpr since C++26
12
13#include <list>
14#include <cstdlib>
15#include <cassert>
16
17#include "test_macros.h"
18#include "min_allocator.h"
19#include "count_new.h"
20
21template <class List>
22TEST_CONSTEXPR_CXX26 void test() {
23 int a1[] = {1, 2, 3};
24 int a2[] = {1, 4, 2, 3};
25 List l1(a1, a1 + 3);
26 typename List::iterator i = l1.insert(std::next(l1.cbegin()), 4);
27 assert(i == std::next(l1.begin()));
28 assert(l1.size() == 4);
29 assert(std::distance(l1.begin(), l1.end()) == 4);
30 assert(l1 == List(a2, a2 + 4));
31
32#if !defined(TEST_HAS_NO_EXCEPTIONS) && !defined(DISABLE_NEW_COUNT)
33 if (!TEST_IS_CONSTANT_EVALUATED) {
34 globalMemCounter.throw_after = 0;
35 int save_count = globalMemCounter.outstanding_new;
36 try {
37 i = l1.insert(i, 5);
38 assert(false);
39 } catch (...) {
40 }
41 assert(globalMemCounter.checkOutstandingNewEq(save_count));
42 assert(l1 == List(a2, a2 + 4));
43 }
44#endif
45}
46
47TEST_CONSTEXPR_CXX26 bool test() {
48 test<std::list<int> >();
49#if TEST_STD_VER >= 11
50 test<std::list<int, min_allocator<int>>>();
51#endif
52
53 return true;
54}
55
56int main(int, char**) {
57 assert(test());
58#if TEST_STD_VER >= 26
59 static_assert(test());
60#endif
61
62 return 0;
63}
64

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