blob: 406e93a376a1b00f7cb033d81b4f7f118515b862 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <list>
11
12// iterator insert(const_iterator position, const value_type& x);
13
Eric Fiselier07a4bec2015-03-10 20:46:04 +000014// UNSUPPORTED: sanitizer-new-delete
Eric Fiselier72aab5f2014-11-04 05:11:41 +000015
Howard Hinnant5e571422013-08-23 20:10:18 +000016#if _LIBCPP_DEBUG >= 1
Howard Hinnant79a35572013-04-05 00:18:49 +000017#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
18#endif
19
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000020#include <list>
21#include <cstdlib>
22#include <cassert>
23
Marshall Clow061d0cc2013-11-26 20:58:02 +000024#include "min_allocator.h"
Howard Hinnant29f74322013-06-25 16:08:47 +000025
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026int throw_next = 0xFFFF;
27int count = 0;
28
29void* 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
38void operator delete(void* p) throw()
39{
40 --count;
41 std::free(p);
42}
43
44int main()
45{
Howard Hinnant29f74322013-06-25 16:08:47 +000046 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047 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 Hinnant29f74322013-06-25 16:08:47 +000068 }
Howard Hinnant5e571422013-08-23 20:10:18 +000069#if _LIBCPP_DEBUG >= 1
Howard Hinnant79a35572013-04-05 00:18:49 +000070 {
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 Hinnant29f74322013-06-25 16:08:47 +000078#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 Hinnant5e571422013-08-23 20:10:18 +0000102#if _LIBCPP_DEBUG >= 1
Howard Hinnant29f74322013-06-25 16:08:47 +0000103 {
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 Hinnantbc8d3f92010-05-11 19:42:16 +0000112}