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 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <list> |
| 11 | |
| 12 | // template <InputIterator Iter> |
| 13 | // iterator insert(const_iterator position, Iter first, Iter last); |
| 14 | |
| 15 | #include <list> |
| 16 | #include <cstdlib> |
| 17 | #include <cassert> |
| 18 | |
| 19 | int throw_next = 0xFFFF; |
| 20 | int count = 0; |
| 21 | |
| 22 | void* operator new(std::size_t s) throw(std::bad_alloc) |
| 23 | { |
| 24 | if (throw_next == 0) |
| 25 | throw std::bad_alloc(); |
| 26 | --throw_next; |
| 27 | ++count; |
| 28 | return std::malloc(s); |
| 29 | } |
| 30 | |
| 31 | void operator delete(void* p) throw() |
| 32 | { |
| 33 | --count; |
| 34 | std::free(p); |
| 35 | } |
| 36 | |
| 37 | int main() |
| 38 | { |
| 39 | int a1[] = {1, 2, 3}; |
| 40 | std::list<int> l1; |
| 41 | std::list<int>::iterator i = l1.insert(l1.begin(), a1, a1+3); |
| 42 | assert(i == l1.begin()); |
| 43 | assert(l1.size() == 3); |
| 44 | assert(distance(l1.begin(), l1.end()) == 3); |
| 45 | i = l1.begin(); |
| 46 | assert(*i == 1); |
| 47 | ++i; |
| 48 | assert(*i == 2); |
| 49 | ++i; |
| 50 | assert(*i == 3); |
| 51 | int a2[] = {4, 5, 6}; |
| 52 | i = l1.insert(i, a2, a2+3); |
| 53 | assert(*i == 4); |
| 54 | assert(l1.size() == 6); |
| 55 | assert(distance(l1.begin(), l1.end()) == 6); |
| 56 | i = l1.begin(); |
| 57 | assert(*i == 1); |
| 58 | ++i; |
| 59 | assert(*i == 2); |
| 60 | ++i; |
| 61 | assert(*i == 4); |
| 62 | ++i; |
| 63 | assert(*i == 5); |
| 64 | ++i; |
| 65 | assert(*i == 6); |
| 66 | ++i; |
| 67 | assert(*i == 3); |
| 68 | throw_next = 2; |
| 69 | int save_count = count; |
| 70 | try |
| 71 | { |
| 72 | i = l1.insert(i, a2, a2+3); |
| 73 | assert(false); |
| 74 | } |
| 75 | catch (...) |
| 76 | { |
| 77 | } |
| 78 | assert(save_count == count); |
| 79 | assert(l1.size() == 6); |
| 80 | assert(distance(l1.begin(), l1.end()) == 6); |
| 81 | i = l1.begin(); |
| 82 | assert(*i == 1); |
| 83 | ++i; |
| 84 | assert(*i == 2); |
| 85 | ++i; |
| 86 | assert(*i == 4); |
| 87 | ++i; |
| 88 | assert(*i == 5); |
| 89 | ++i; |
| 90 | assert(*i == 6); |
| 91 | ++i; |
| 92 | assert(*i == 3); |
| 93 | } |