1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03 && !stdlib=libc++
10
11// <vector>
12
13// template <class... Args> iterator emplace(const_iterator pos, Args&&... args);
14
15#include <vector>
16#include <cassert>
17
18#include "test_macros.h"
19#include "test_allocator.h"
20#include "min_allocator.h"
21#include "asan_testing.h"
22
23class A
24{
25 int i_;
26 double d_;
27
28 A(const A&);
29 A& operator=(const A&);
30public:
31 TEST_CONSTEXPR_CXX14 A(int i, double d)
32 : i_(i), d_(d) {}
33
34 TEST_CONSTEXPR_CXX14 A(A&& a)
35 : i_(a.i_),
36 d_(a.d_)
37 {
38 a.i_ = 0;
39 a.d_ = 0;
40 }
41
42 TEST_CONSTEXPR_CXX14 A& operator=(A&& a)
43 {
44 i_ = a.i_;
45 d_ = a.d_;
46 a.i_ = 0;
47 a.d_ = 0;
48 return *this;
49 }
50
51 TEST_CONSTEXPR_CXX14 int geti() const {return i_;}
52 TEST_CONSTEXPR_CXX14 double getd() const {return d_;}
53};
54
55TEST_CONSTEXPR_CXX20 bool tests()
56{
57 {
58 std::vector<A> c;
59 std::vector<A>::iterator i = c.emplace(position: c.cbegin(), args: 2, args: 3.5);
60 assert(i == c.begin());
61 assert(c.size() == 1);
62 assert(c.front().geti() == 2);
63 assert(c.front().getd() == 3.5);
64 assert(is_contiguous_container_asan_correct(c));
65 i = c.emplace(position: c.cend(), args: 3, args: 4.5);
66 assert(i == c.end()-1);
67 assert(c.size() == 2);
68 assert(c.front().geti() == 2);
69 assert(c.front().getd() == 3.5);
70 assert(c.back().geti() == 3);
71 assert(c.back().getd() == 4.5);
72 assert(is_contiguous_container_asan_correct(c));
73 i = c.emplace(position: c.cbegin()+1, args: 4, args: 6.5);
74 assert(i == c.begin()+1);
75 assert(c.size() == 3);
76 assert(c.front().geti() == 2);
77 assert(c.front().getd() == 3.5);
78 assert(c[1].geti() == 4);
79 assert(c[1].getd() == 6.5);
80 assert(c.back().geti() == 3);
81 assert(c.back().getd() == 4.5);
82 assert(is_contiguous_container_asan_correct(c));
83 }
84 {
85 std::vector<A, limited_allocator<A, 7> > c;
86 std::vector<A, limited_allocator<A, 7> >::iterator i = c.emplace(c.cbegin(), 2, 3.5);
87 assert(i == c.begin());
88 assert(c.size() == 1);
89 assert(c.front().geti() == 2);
90 assert(c.front().getd() == 3.5);
91 assert(is_contiguous_container_asan_correct(c));
92 i = c.emplace(c.cend(), 3, 4.5);
93 assert(i == c.end()-1);
94 assert(c.size() == 2);
95 assert(c.front().geti() == 2);
96 assert(c.front().getd() == 3.5);
97 assert(c.back().geti() == 3);
98 assert(c.back().getd() == 4.5);
99 assert(is_contiguous_container_asan_correct(c));
100 i = c.emplace(c.cbegin()+1, 4, 6.5);
101 assert(i == c.begin()+1);
102 assert(c.size() == 3);
103 assert(c.front().geti() == 2);
104 assert(c.front().getd() == 3.5);
105 assert(c[1].geti() == 4);
106 assert(c[1].getd() == 6.5);
107 assert(c.back().geti() == 3);
108 assert(c.back().getd() == 4.5);
109 assert(is_contiguous_container_asan_correct(c));
110 }
111 {
112 std::vector<A, min_allocator<A> > c;
113 std::vector<A, min_allocator<A> >::iterator i = c.emplace(c.cbegin(), 2, 3.5);
114 assert(i == c.begin());
115 assert(c.size() == 1);
116 assert(c.front().geti() == 2);
117 assert(c.front().getd() == 3.5);
118 i = c.emplace(c.cend(), 3, 4.5);
119 assert(i == c.end()-1);
120 assert(c.size() == 2);
121 assert(c.front().geti() == 2);
122 assert(c.front().getd() == 3.5);
123 assert(c.back().geti() == 3);
124 assert(c.back().getd() == 4.5);
125 i = c.emplace(c.cbegin()+1, 4, 6.5);
126 assert(i == c.begin()+1);
127 assert(c.size() == 3);
128 assert(c.front().geti() == 2);
129 assert(c.front().getd() == 3.5);
130 assert(c[1].geti() == 4);
131 assert(c[1].getd() == 6.5);
132 assert(c.back().geti() == 3);
133 assert(c.back().getd() == 4.5);
134 }
135 {
136 std::vector<A, safe_allocator<A> > c;
137 std::vector<A, safe_allocator<A> >::iterator i = c.emplace(c.cbegin(), 2, 3.5);
138 assert(i == c.begin());
139 assert(c.size() == 1);
140 assert(c.front().geti() == 2);
141 assert(c.front().getd() == 3.5);
142 i = c.emplace(c.cend(), 3, 4.5);
143 assert(i == c.end() - 1);
144 assert(c.size() == 2);
145 assert(c.front().geti() == 2);
146 assert(c.front().getd() == 3.5);
147 assert(c.back().geti() == 3);
148 assert(c.back().getd() == 4.5);
149 i = c.emplace(c.cbegin() + 1, 4, 6.5);
150 assert(i == c.begin() + 1);
151 assert(c.size() == 3);
152 assert(c.front().geti() == 2);
153 assert(c.front().getd() == 3.5);
154 assert(c[1].geti() == 4);
155 assert(c[1].getd() == 6.5);
156 assert(c.back().geti() == 3);
157 assert(c.back().getd() == 4.5);
158 }
159
160 return true;
161}
162
163int main(int, char**)
164{
165 tests();
166#if TEST_STD_VER > 17
167 static_assert(tests());
168#endif
169 return 0;
170}
171

source code of libcxx/test/std/containers/sequences/vector/vector.modifiers/emplace.pass.cpp