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 // UNSUPPORTED: c++98, c++03
11
12 // <vector>
13
14 // template <class... Args> iterator emplace(const_iterator pos, Args&&... args);
15
16 #include <vector>
17 #include <cassert>
18
19 #include "test_macros.h"
20 #include "test_allocator.h"
21 #include "min_allocator.h"
22 #include "asan_testing.h"
23
24 class A
25 {
26 int i_;
27 double d_;
28
29 A(const A&);
30 A& operator=(const A&);
31 public:
A(int i,double d)32 A(int i, double d)
33 : i_(i), d_(d) {}
34
A(A && a)35 A(A&& a)
36 : i_(a.i_),
37 d_(a.d_)
38 {
39 a.i_ = 0;
40 a.d_ = 0;
41 }
42
operator =(A && a)43 A& operator=(A&& a)
44 {
45 i_ = a.i_;
46 d_ = a.d_;
47 a.i_ = 0;
48 a.d_ = 0;
49 return *this;
50 }
51
geti() const52 int geti() const {return i_;}
getd() const53 double getd() const {return d_;}
54 };
55
main()56 int main()
57 {
58 {
59 std::vector<A> c;
60 std::vector<A>::iterator i = c.emplace(c.cbegin(), 2, 3.5);
61 assert(i == c.begin());
62 assert(c.size() == 1);
63 assert(c.front().geti() == 2);
64 assert(c.front().getd() == 3.5);
65 assert(is_contiguous_container_asan_correct(c));
66 i = c.emplace(c.cend(), 3, 4.5);
67 assert(i == c.end()-1);
68 assert(c.size() == 2);
69 assert(c.front().geti() == 2);
70 assert(c.front().getd() == 3.5);
71 assert(c.back().geti() == 3);
72 assert(c.back().getd() == 4.5);
73 assert(is_contiguous_container_asan_correct(c));
74 i = c.emplace(c.cbegin()+1, 4, 6.5);
75 assert(i == c.begin()+1);
76 assert(c.size() == 3);
77 assert(c.front().geti() == 2);
78 assert(c.front().getd() == 3.5);
79 assert(c[1].geti() == 4);
80 assert(c[1].getd() == 6.5);
81 assert(c.back().geti() == 3);
82 assert(c.back().getd() == 4.5);
83 assert(is_contiguous_container_asan_correct(c));
84 }
85 {
86 std::vector<A, limited_allocator<A, 7> > c;
87 std::vector<A, limited_allocator<A, 7> >::iterator i = c.emplace(c.cbegin(), 2, 3.5);
88 assert(i == c.begin());
89 assert(c.size() == 1);
90 assert(c.front().geti() == 2);
91 assert(c.front().getd() == 3.5);
92 assert(is_contiguous_container_asan_correct(c));
93 i = c.emplace(c.cend(), 3, 4.5);
94 assert(i == c.end()-1);
95 assert(c.size() == 2);
96 assert(c.front().geti() == 2);
97 assert(c.front().getd() == 3.5);
98 assert(c.back().geti() == 3);
99 assert(c.back().getd() == 4.5);
100 assert(is_contiguous_container_asan_correct(c));
101 i = c.emplace(c.cbegin()+1, 4, 6.5);
102 assert(i == c.begin()+1);
103 assert(c.size() == 3);
104 assert(c.front().geti() == 2);
105 assert(c.front().getd() == 3.5);
106 assert(c[1].geti() == 4);
107 assert(c[1].getd() == 6.5);
108 assert(c.back().geti() == 3);
109 assert(c.back().getd() == 4.5);
110 assert(is_contiguous_container_asan_correct(c));
111 }
112 {
113 std::vector<A, min_allocator<A>> c;
114 std::vector<A, min_allocator<A>>::iterator i = c.emplace(c.cbegin(), 2, 3.5);
115 assert(i == c.begin());
116 assert(c.size() == 1);
117 assert(c.front().geti() == 2);
118 assert(c.front().getd() == 3.5);
119 i = c.emplace(c.cend(), 3, 4.5);
120 assert(i == c.end()-1);
121 assert(c.size() == 2);
122 assert(c.front().geti() == 2);
123 assert(c.front().getd() == 3.5);
124 assert(c.back().geti() == 3);
125 assert(c.back().getd() == 4.5);
126 i = c.emplace(c.cbegin()+1, 4, 6.5);
127 assert(i == c.begin()+1);
128 assert(c.size() == 3);
129 assert(c.front().geti() == 2);
130 assert(c.front().getd() == 3.5);
131 assert(c[1].geti() == 4);
132 assert(c[1].getd() == 6.5);
133 assert(c.back().geti() == 3);
134 assert(c.back().getd() == 4.5);
135 }
136 }
137