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, const value_type& x);
13 
14 // UNSUPPORTED: sanitizer-new-delete
15 
16 #if _LIBCPP_DEBUG >= 1
17 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
18 #endif
19 
20 #include <list>
21 #include <cstdlib>
22 #include <cassert>
23 
24 #include "min_allocator.h"
25 
26 int throw_next = 0xFFFF;
27 int count = 0;
28 
operator new(std::size_t s)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 
operator delete(void * p)38 void  operator delete(void* p) throw()
39 {
40     --count;
41     std::free(p);
42 }
43 
main()44 int main()
45 {
46     {
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));
68     }
69 #if _LIBCPP_DEBUG >= 1
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
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     }
102 #if _LIBCPP_DEBUG >= 1
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
112 }
113