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); |
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 | |
21 | template <class List> |
22 | 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 | globalMemCounter.throw_after = 0; |
34 | int save_count = globalMemCounter.outstanding_new; |
35 | try { |
36 | i = l1.insert(i, 5); |
37 | assert(false); |
38 | } catch (...) { |
39 | } |
40 | assert(globalMemCounter.checkOutstandingNewEq(save_count)); |
41 | assert(l1 == List(a2, a2 + 4)); |
42 | #endif |
43 | } |
44 | |
45 | int main(int, char**) { |
46 | test<std::list<int> >(); |
47 | #if TEST_STD_VER >= 11 |
48 | test<std::list<int, min_allocator<int>>>(); |
49 | #endif |
50 | |
51 | return 0; |
52 | } |
53 | |