| 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); |
| 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 | |
| 23 | template <class List> |
| 24 | 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 | globalMemCounter.throw_after = 2; |
| 57 | int save_count = globalMemCounter.outstanding_new; |
| 58 | try { |
| 59 | i = l1.insert(i, a2, a2 + 3); |
| 60 | assert(false); |
| 61 | } catch (...) { |
| 62 | } |
| 63 | assert(globalMemCounter.checkOutstandingNewEq(save_count)); |
| 64 | assert(l1.size() == 6); |
| 65 | assert(std::distance(l1.begin(), l1.end()) == 6); |
| 66 | i = l1.begin(); |
| 67 | assert(*i == 1); |
| 68 | ++i; |
| 69 | assert(*i == 2); |
| 70 | ++i; |
| 71 | assert(*i == 4); |
| 72 | ++i; |
| 73 | assert(*i == 5); |
| 74 | ++i; |
| 75 | assert(*i == 6); |
| 76 | ++i; |
| 77 | assert(*i == 3); |
| 78 | #endif |
| 79 | } |
| 80 | |
| 81 | int main(int, char**) { |
| 82 | test<std::list<int> >(); |
| 83 | #if TEST_STD_VER >= 11 |
| 84 | test<std::list<int, min_allocator<int>>>(); |
| 85 | #endif |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |