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 | |
| 12 | // iterator insert(const_iterator position, const value_type& x); |
| 13 | |
Eric Fiselier | 07a4bec | 2015-03-10 20:46:04 +0000 | [diff] [blame] | 14 | // UNSUPPORTED: sanitizer-new-delete |
Eric Fiselier | 72aab5f | 2014-11-04 05:11:41 +0000 | [diff] [blame] | 15 | |
Howard Hinnant | 5e57142 | 2013-08-23 20:10:18 +0000 | [diff] [blame] | 16 | #if _LIBCPP_DEBUG >= 1 |
Howard Hinnant | 79a3557 | 2013-04-05 00:18:49 +0000 | [diff] [blame] | 17 | #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0)) |
| 18 | #endif |
| 19 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 20 | #include <list> |
| 21 | #include <cstdlib> |
| 22 | #include <cassert> |
| 23 | |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 24 | #include "min_allocator.h" |
Howard Hinnant | 29f7432 | 2013-06-25 16:08:47 +0000 | [diff] [blame] | 25 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | int throw_next = 0xFFFF; |
| 27 | int count = 0; |
| 28 | |
| 29 | void* operator new(std::size_t s) throw(std::bad_alloc) |
| 30 | { |
| 31 | if (throw_next == 0) |
| 32 | throw std::bad_alloc(); |
| 33 | --throw_next; |
| 34 | ++count; |
| 35 | return std::malloc(s); |
| 36 | } |
| 37 | |
| 38 | void operator delete(void* p) throw() |
| 39 | { |
| 40 | --count; |
| 41 | std::free(p); |
| 42 | } |
| 43 | |
| 44 | int main() |
| 45 | { |
Howard Hinnant | 29f7432 | 2013-06-25 16:08:47 +0000 | [diff] [blame] | 46 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 47 | int a1[] = {1, 2, 3}; |
| 48 | int a2[] = {1, 4, 2, 3}; |
| 49 | std::list<int> l1(a1, a1+3); |
| 50 | std::list<int>::iterator i = l1.insert(next(l1.cbegin()), 4); |
| 51 | assert(i == next(l1.begin())); |
| 52 | assert(l1.size() == 4); |
| 53 | assert(distance(l1.begin(), l1.end()) == 4); |
| 54 | assert(l1 == std::list<int>(a2, a2+4)); |
| 55 | throw_next = 0; |
| 56 | int save_count = count; |
| 57 | try |
| 58 | { |
| 59 | i = l1.insert(i, 5); |
| 60 | assert(false); |
| 61 | } |
| 62 | catch (...) |
| 63 | { |
| 64 | } |
| 65 | throw_next = 0xFFFF; |
| 66 | assert(save_count == count); |
| 67 | assert(l1 == std::list<int>(a2, a2+4)); |
Howard Hinnant | 29f7432 | 2013-06-25 16:08:47 +0000 | [diff] [blame] | 68 | } |
Howard Hinnant | 5e57142 | 2013-08-23 20:10:18 +0000 | [diff] [blame] | 69 | #if _LIBCPP_DEBUG >= 1 |
Howard Hinnant | 79a3557 | 2013-04-05 00:18:49 +0000 | [diff] [blame] | 70 | { |
| 71 | std::list<int> v1(3); |
| 72 | std::list<int> v2(3); |
| 73 | int i = 4; |
| 74 | v1.insert(v2.begin(), i); |
| 75 | assert(false); |
| 76 | } |
| 77 | #endif |
Howard Hinnant | 29f7432 | 2013-06-25 16:08:47 +0000 | [diff] [blame] | 78 | #if __cplusplus >= 201103L |
| 79 | { |
| 80 | int a1[] = {1, 2, 3}; |
| 81 | int a2[] = {1, 4, 2, 3}; |
| 82 | std::list<int, min_allocator<int>> l1(a1, a1+3); |
| 83 | std::list<int, min_allocator<int>>::iterator i = l1.insert(next(l1.cbegin()), 4); |
| 84 | assert(i == next(l1.begin())); |
| 85 | assert(l1.size() == 4); |
| 86 | assert(distance(l1.begin(), l1.end()) == 4); |
| 87 | assert((l1 == std::list<int, min_allocator<int>>(a2, a2+4))); |
| 88 | throw_next = 0; |
| 89 | int save_count = count; |
| 90 | try |
| 91 | { |
| 92 | i = l1.insert(i, 5); |
| 93 | assert(false); |
| 94 | } |
| 95 | catch (...) |
| 96 | { |
| 97 | } |
| 98 | throw_next = 0xFFFF; |
| 99 | assert(save_count == count); |
| 100 | assert((l1 == std::list<int, min_allocator<int>>(a2, a2+4))); |
| 101 | } |
Howard Hinnant | 5e57142 | 2013-08-23 20:10:18 +0000 | [diff] [blame] | 102 | #if _LIBCPP_DEBUG >= 1 |
Howard Hinnant | 29f7432 | 2013-06-25 16:08:47 +0000 | [diff] [blame] | 103 | { |
| 104 | std::list<int, min_allocator<int>> v1(3); |
| 105 | std::list<int, min_allocator<int>> v2(3); |
| 106 | int i = 4; |
| 107 | v1.insert(v2.begin(), i); |
| 108 | assert(false); |
| 109 | } |
| 110 | #endif |
| 111 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 112 | } |