1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <list>
11 
12 // iterator insert(const_iterator position, size_type n, const value_type& x);
13 
14 #if _LIBCPP_DEBUG >= 1
15 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
16 #endif
17 
18 #include <list>
19 #include <cstdlib>
20 #include <cassert>
21 
22 #include "min_allocator.h"
23 
24 int throw_next = 0xFFFF;
25 int count = 0;
26 
operator new(std::size_t s)27 void* operator new(std::size_t s) throw(std::bad_alloc)
28 {
29     if (throw_next == 0)
30         throw std::bad_alloc();
31     --throw_next;
32     ++count;
33     return std::malloc(s);
34 }
35 
operator delete(void * p)36 void  operator delete(void* p) throw()
37 {
38     --count;
39     std::free(p);
40 }
41 
main()42 int main()
43 {
44     {
45     int a1[] = {1, 2, 3};
46     int a2[] = {1, 4, 4, 4, 4, 4, 2, 3};
47     std::list<int> l1(a1, a1+3);
48     std::list<int>::iterator i = l1.insert(next(l1.cbegin()), 5, 4);
49     assert(i == next(l1.begin()));
50     assert(l1 == std::list<int>(a2, a2+8));
51     throw_next = 4;
52     int save_count = count;
53     try
54     {
55         i = l1.insert(i, 5, 5);
56         assert(false);
57     }
58     catch (...)
59     {
60     }
61     throw_next = 0xFFFF;
62     assert(save_count == count);
63     assert(l1 == std::list<int>(a2, a2+8));
64     }
65 #if _LIBCPP_DEBUG >= 1
66     {
67         std::list<int> c1(100);
68         std::list<int> c2;
69         std::list<int>::iterator i = c1.insert(next(c2.cbegin(), 10), 5, 1);
70         assert(false);
71     }
72 #endif
73 #if __cplusplus >= 201103L
74     {
75     int a1[] = {1, 2, 3};
76     int a2[] = {1, 4, 4, 4, 4, 4, 2, 3};
77     std::list<int, min_allocator<int>> l1(a1, a1+3);
78     std::list<int, min_allocator<int>>::iterator i = l1.insert(next(l1.cbegin()), 5, 4);
79     assert(i == next(l1.begin()));
80     assert((l1 == std::list<int, min_allocator<int>>(a2, a2+8)));
81     throw_next = 4;
82     int save_count = count;
83     try
84     {
85         i = l1.insert(i, 5, 5);
86         assert(false);
87     }
88     catch (...)
89     {
90     }
91     throw_next = 0xFFFF;
92     assert(save_count == count);
93     assert((l1 == std::list<int, min_allocator<int>>(a2, a2+8)));
94     }
95 #if _LIBCPP_DEBUG >= 1
96     {
97         std::list<int, min_allocator<int>> c1(100);
98         std::list<int, min_allocator<int>> c2;
99         std::list<int, min_allocator<int>>::iterator i = c1.insert(next(c2.cbegin(), 10), 5, 1);
100         assert(false);
101     }
102 #endif
103 #endif
104 }
105