Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <list> |
| 11 | |
Howard Hinnant | 6046ace | 2010-08-22 00:15:28 +0000 | [diff] [blame] | 12 | // template <InputIterator Iter> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 13 | // iterator insert(const_iterator position, Iter first, Iter last); |
| 14 | |
Eric Fiselier | 07a4bec | 2015-03-10 20:46:04 +0000 | [diff] [blame] | 15 | // UNSUPPORTED: sanitizer-new-delete |
Eric Fiselier | 72aab5f | 2014-11-04 05:11:41 +0000 | [diff] [blame] | 16 | |
Howard Hinnant | 5e57142 | 2013-08-23 20:10:18 +0000 | [diff] [blame] | 17 | #if _LIBCPP_DEBUG >= 1 |
Howard Hinnant | 79a3557 | 2013-04-05 00:18:49 +0000 | [diff] [blame] | 18 | #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) |
| 19 | #endif |
| 20 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | #include <list> |
| 22 | #include <cstdlib> |
| 23 | #include <cassert> |
Howard Hinnant | 79a3557 | 2013-04-05 00:18:49 +0000 | [diff] [blame] | 24 | #include "test_iterators.h" |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 25 | #include "min_allocator.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | |
| 27 | int throw_next = 0xFFFF; |
| 28 | int count = 0; |
| 29 | |
| 30 | void* operator new(std::size_t s) throw(std::bad_alloc) |
| 31 | { |
| 32 | if (throw_next == 0) |
| 33 | throw std::bad_alloc(); |
| 34 | --throw_next; |
| 35 | ++count; |
| 36 | return std::malloc(s); |
| 37 | } |
| 38 | |
| 39 | void operator delete(void* p) throw() |
| 40 | { |
| 41 | --count; |
| 42 | std::free(p); |
| 43 | } |
| 44 | |
| 45 | int main() |
| 46 | { |
Howard Hinnant | 79a3557 | 2013-04-05 00:18:49 +0000 | [diff] [blame] | 47 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 48 | int a1[] = {1, 2, 3}; |
| 49 | std::list<int> l1; |
| 50 | std::list<int>::iterator i = l1.insert(l1.begin(), a1, a1+3); |
| 51 | assert(i == l1.begin()); |
| 52 | assert(l1.size() == 3); |
| 53 | assert(distance(l1.begin(), l1.end()) == 3); |
| 54 | i = l1.begin(); |
| 55 | assert(*i == 1); |
| 56 | ++i; |
| 57 | assert(*i == 2); |
| 58 | ++i; |
| 59 | assert(*i == 3); |
| 60 | int a2[] = {4, 5, 6}; |
| 61 | i = l1.insert(i, a2, a2+3); |
| 62 | assert(*i == 4); |
| 63 | assert(l1.size() == 6); |
| 64 | assert(distance(l1.begin(), l1.end()) == 6); |
| 65 | i = l1.begin(); |
| 66 | assert(*i == 1); |
| 67 | ++i; |
| 68 | assert(*i == 2); |
| 69 | ++i; |
| 70 | assert(*i == 4); |
| 71 | ++i; |
| 72 | assert(*i == 5); |
| 73 | ++i; |
| 74 | assert(*i == 6); |
| 75 | ++i; |
| 76 | assert(*i == 3); |
| 77 | throw_next = 2; |
| 78 | int save_count = count; |
| 79 | try |
| 80 | { |
| 81 | i = l1.insert(i, a2, a2+3); |
| 82 | assert(false); |
| 83 | } |
| 84 | catch (...) |
| 85 | { |
| 86 | } |
| 87 | assert(save_count == count); |
| 88 | assert(l1.size() == 6); |
| 89 | assert(distance(l1.begin(), l1.end()) == 6); |
| 90 | i = l1.begin(); |
| 91 | assert(*i == 1); |
| 92 | ++i; |
| 93 | assert(*i == 2); |
| 94 | ++i; |
| 95 | assert(*i == 4); |
| 96 | ++i; |
| 97 | assert(*i == 5); |
| 98 | ++i; |
| 99 | assert(*i == 6); |
| 100 | ++i; |
| 101 | assert(*i == 3); |
Howard Hinnant | 79a3557 | 2013-04-05 00:18:49 +0000 | [diff] [blame] | 102 | } |
Howard Hinnant | 29f7432 | 2013-06-25 16:08:47 +0000 | [diff] [blame] | 103 | throw_next = 0xFFFF; |
Howard Hinnant | 5e57142 | 2013-08-23 20:10:18 +0000 | [diff] [blame] | 104 | #if _LIBCPP_DEBUG >= 1 |
Howard Hinnant | 79a3557 | 2013-04-05 00:18:49 +0000 | [diff] [blame] | 105 | { |
Howard Hinnant | 79a3557 | 2013-04-05 00:18:49 +0000 | [diff] [blame] | 106 | std::list<int> v(100); |
| 107 | std::list<int> v2(100); |
| 108 | int a[] = {1, 2, 3, 4, 5}; |
| 109 | const int N = sizeof(a)/sizeof(a[0]); |
| 110 | std::list<int>::iterator i = v.insert(next(v2.cbegin(), 10), input_iterator<const int*>(a), |
| 111 | input_iterator<const int*>(a+N)); |
| 112 | assert(false); |
| 113 | } |
| 114 | #endif |
Howard Hinnant | 29f7432 | 2013-06-25 16:08:47 +0000 | [diff] [blame] | 115 | #if __cplusplus >= 201103L |
| 116 | { |
| 117 | int a1[] = {1, 2, 3}; |
| 118 | std::list<int, min_allocator<int>> l1; |
| 119 | std::list<int, min_allocator<int>>::iterator i = l1.insert(l1.begin(), a1, a1+3); |
| 120 | assert(i == l1.begin()); |
| 121 | assert(l1.size() == 3); |
| 122 | assert(distance(l1.begin(), l1.end()) == 3); |
| 123 | i = l1.begin(); |
| 124 | assert(*i == 1); |
| 125 | ++i; |
| 126 | assert(*i == 2); |
| 127 | ++i; |
| 128 | assert(*i == 3); |
| 129 | int a2[] = {4, 5, 6}; |
| 130 | i = l1.insert(i, a2, a2+3); |
| 131 | assert(*i == 4); |
| 132 | assert(l1.size() == 6); |
| 133 | assert(distance(l1.begin(), l1.end()) == 6); |
| 134 | i = l1.begin(); |
| 135 | assert(*i == 1); |
| 136 | ++i; |
| 137 | assert(*i == 2); |
| 138 | ++i; |
| 139 | assert(*i == 4); |
| 140 | ++i; |
| 141 | assert(*i == 5); |
| 142 | ++i; |
| 143 | assert(*i == 6); |
| 144 | ++i; |
| 145 | assert(*i == 3); |
| 146 | throw_next = 2; |
| 147 | int save_count = count; |
| 148 | try |
| 149 | { |
| 150 | i = l1.insert(i, a2, a2+3); |
| 151 | assert(false); |
| 152 | } |
| 153 | catch (...) |
| 154 | { |
| 155 | } |
| 156 | assert(save_count == count); |
| 157 | assert(l1.size() == 6); |
| 158 | assert(distance(l1.begin(), l1.end()) == 6); |
| 159 | i = l1.begin(); |
| 160 | assert(*i == 1); |
| 161 | ++i; |
| 162 | assert(*i == 2); |
| 163 | ++i; |
| 164 | assert(*i == 4); |
| 165 | ++i; |
| 166 | assert(*i == 5); |
| 167 | ++i; |
| 168 | assert(*i == 6); |
| 169 | ++i; |
| 170 | assert(*i == 3); |
| 171 | } |
Howard Hinnant | 5e57142 | 2013-08-23 20:10:18 +0000 | [diff] [blame] | 172 | #if _LIBCPP_DEBUG >= 1 |
Howard Hinnant | 29f7432 | 2013-06-25 16:08:47 +0000 | [diff] [blame] | 173 | { |
| 174 | throw_next = 0xFFFF; |
| 175 | std::list<int, min_allocator<int>> v(100); |
| 176 | std::list<int, min_allocator<int>> v2(100); |
| 177 | int a[] = {1, 2, 3, 4, 5}; |
| 178 | const int N = sizeof(a)/sizeof(a[0]); |
| 179 | std::list<int, min_allocator<int>>::iterator i = v.insert(next(v2.cbegin(), 10), input_iterator<const int*>(a), |
| 180 | input_iterator<const int*>(a+N)); |
| 181 | assert(false); |
| 182 | } |
| 183 | #endif |
| 184 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 185 | } |