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 // <vector>
11 
12 // template <class... Args> iterator emplace(const_iterator pos, Args&&... args);
13 
14 #if _LIBCPP_DEBUG >= 1
15 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
16 #endif
17 
18 #include <vector>
19 #include <cassert>
20 #include "../../../stack_allocator.h"
21 #include "min_allocator.h"
22 #include "asan_testing.h"
23 
24 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
25 
26 class A
27 {
28     int i_;
29     double d_;
30 
31     A(const A&);
32     A& operator=(const A&);
33 public:
A(int i,double d)34     A(int i, double d)
35         : i_(i), d_(d) {}
36 
A(A && a)37     A(A&& a)
38         : i_(a.i_),
39           d_(a.d_)
40     {
41         a.i_ = 0;
42         a.d_ = 0;
43     }
44 
operator =(A && a)45     A& operator=(A&& a)
46     {
47         i_ = a.i_;
48         d_ = a.d_;
49         a.i_ = 0;
50         a.d_ = 0;
51         return *this;
52     }
53 
geti() const54     int geti() const {return i_;}
getd() const55     double getd() const {return d_;}
56 };
57 
58 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
59 
main()60 int main()
61 {
62 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
63     {
64         std::vector<A> c;
65         std::vector<A>::iterator i = c.emplace(c.cbegin(), 2, 3.5);
66         assert(i == c.begin());
67         assert(c.size() == 1);
68         assert(c.front().geti() == 2);
69         assert(c.front().getd() == 3.5);
70         assert(is_contiguous_container_asan_correct(c));
71         i = c.emplace(c.cend(), 3, 4.5);
72         assert(i == c.end()-1);
73         assert(c.size() == 2);
74         assert(c.front().geti() == 2);
75         assert(c.front().getd() == 3.5);
76         assert(c.back().geti() == 3);
77         assert(c.back().getd() == 4.5);
78         assert(is_contiguous_container_asan_correct(c));
79         i = c.emplace(c.cbegin()+1, 4, 6.5);
80         assert(i == c.begin()+1);
81         assert(c.size() == 3);
82         assert(c.front().geti() == 2);
83         assert(c.front().getd() == 3.5);
84         assert(c[1].geti() == 4);
85         assert(c[1].getd() == 6.5);
86         assert(c.back().geti() == 3);
87         assert(c.back().getd() == 4.5);
88         assert(is_contiguous_container_asan_correct(c));
89     }
90     {
91         std::vector<A, stack_allocator<A, 7> > c;
92         std::vector<A, stack_allocator<A, 7> >::iterator i = c.emplace(c.cbegin(), 2, 3.5);
93         assert(i == c.begin());
94         assert(c.size() == 1);
95         assert(c.front().geti() == 2);
96         assert(c.front().getd() == 3.5);
97         assert(is_contiguous_container_asan_correct(c));
98         i = c.emplace(c.cend(), 3, 4.5);
99         assert(i == c.end()-1);
100         assert(c.size() == 2);
101         assert(c.front().geti() == 2);
102         assert(c.front().getd() == 3.5);
103         assert(c.back().geti() == 3);
104         assert(c.back().getd() == 4.5);
105         assert(is_contiguous_container_asan_correct(c));
106         i = c.emplace(c.cbegin()+1, 4, 6.5);
107         assert(i == c.begin()+1);
108         assert(c.size() == 3);
109         assert(c.front().geti() == 2);
110         assert(c.front().getd() == 3.5);
111         assert(c[1].geti() == 4);
112         assert(c[1].getd() == 6.5);
113         assert(c.back().geti() == 3);
114         assert(c.back().getd() == 4.5);
115         assert(is_contiguous_container_asan_correct(c));
116     }
117 #if _LIBCPP_DEBUG >= 1
118     {
119         std::vector<A> c1;
120         std::vector<A> c2;
121         std::vector<A>::iterator i = c1.emplace(c2.cbegin(), 2, 3.5);
122         assert(false);
123     }
124 #endif
125 #if __cplusplus >= 201103L
126     {
127         std::vector<A, min_allocator<A>> c;
128         std::vector<A, min_allocator<A>>::iterator i = c.emplace(c.cbegin(), 2, 3.5);
129         assert(i == c.begin());
130         assert(c.size() == 1);
131         assert(c.front().geti() == 2);
132         assert(c.front().getd() == 3.5);
133         i = c.emplace(c.cend(), 3, 4.5);
134         assert(i == c.end()-1);
135         assert(c.size() == 2);
136         assert(c.front().geti() == 2);
137         assert(c.front().getd() == 3.5);
138         assert(c.back().geti() == 3);
139         assert(c.back().getd() == 4.5);
140         i = c.emplace(c.cbegin()+1, 4, 6.5);
141         assert(i == c.begin()+1);
142         assert(c.size() == 3);
143         assert(c.front().geti() == 2);
144         assert(c.front().getd() == 3.5);
145         assert(c[1].geti() == 4);
146         assert(c[1].getd() == 6.5);
147         assert(c.back().geti() == 3);
148         assert(c.back().getd() == 4.5);
149     }
150 #if _LIBCPP_DEBUG >= 1
151     {
152         std::vector<A, min_allocator<A>> c1;
153         std::vector<A, min_allocator<A>> c2;
154         std::vector<A, min_allocator<A>>::iterator i = c1.emplace(c2.cbegin(), 2, 3.5);
155         assert(false);
156     }
157 #endif
158 #endif
159 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
160 }
161