[PowerPC][NFC] Cleanup PPCCTRLoopsVerify pass
[llvm-project.git] / libcxx / test / std / containers / sequences / vector / vector.modifiers / emplace_back.pass.cpp
blobc375933adda5baebf4fbf318f0a7716c38267440
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03
11 // <vector>
13 // template <class... Args> reference emplace_back(Args&&... args);
14 // return type is 'reference' in C++17; 'void' before
16 #include <vector>
17 #include <cassert>
18 #include "test_macros.h"
19 #include "test_allocator.h"
20 #include "min_allocator.h"
21 #include "test_allocator.h"
22 #include "asan_testing.h"
24 class A
26 int i_;
27 double d_;
29 A(const A&);
30 A& operator=(const A&);
31 public:
32 A(int i, double d)
33 : i_(i), d_(d) {}
35 A(A&& a)
36 : i_(a.i_),
37 d_(a.d_)
39 a.i_ = 0;
40 a.d_ = 0;
43 A& operator=(A&& a)
45 i_ = a.i_;
46 d_ = a.d_;
47 a.i_ = 0;
48 a.d_ = 0;
49 return *this;
52 int geti() const {return i_;}
53 double getd() const {return d_;}
56 int main(int, char**)
59 std::vector<A> c;
60 #if TEST_STD_VER > 14
61 A& r1 = c.emplace_back(2, 3.5);
62 assert(c.size() == 1);
63 assert(&r1 == &c.back());
64 assert(c.front().geti() == 2);
65 assert(c.front().getd() == 3.5);
66 assert(is_contiguous_container_asan_correct(c));
67 A& r2 = c.emplace_back(3, 4.5);
68 assert(c.size() == 2);
69 assert(&r2 == &c.back());
70 #else
71 c.emplace_back(2, 3.5);
72 assert(c.size() == 1);
73 assert(c.front().geti() == 2);
74 assert(c.front().getd() == 3.5);
75 assert(is_contiguous_container_asan_correct(c));
76 c.emplace_back(3, 4.5);
77 assert(c.size() == 2);
78 #endif
79 assert(c.front().geti() == 2);
80 assert(c.front().getd() == 3.5);
81 assert(c.back().geti() == 3);
82 assert(c.back().getd() == 4.5);
83 assert(is_contiguous_container_asan_correct(c));
86 std::vector<A, limited_allocator<A, 4> > c;
87 #if TEST_STD_VER > 14
88 A& r1 = c.emplace_back(2, 3.5);
89 assert(c.size() == 1);
90 assert(&r1 == &c.back());
91 assert(c.front().geti() == 2);
92 assert(c.front().getd() == 3.5);
93 assert(is_contiguous_container_asan_correct(c));
94 A& r2 = c.emplace_back(3, 4.5);
95 assert(c.size() == 2);
96 assert(&r2 == &c.back());
97 #else
98 c.emplace_back(2, 3.5);
99 assert(c.size() == 1);
100 assert(c.front().geti() == 2);
101 assert(c.front().getd() == 3.5);
102 assert(is_contiguous_container_asan_correct(c));
103 c.emplace_back(3, 4.5);
104 assert(c.size() == 2);
105 #endif
106 assert(c.front().geti() == 2);
107 assert(c.front().getd() == 3.5);
108 assert(c.back().geti() == 3);
109 assert(c.back().getd() == 4.5);
110 assert(is_contiguous_container_asan_correct(c));
113 std::vector<A, min_allocator<A>> c;
114 #if TEST_STD_VER > 14
115 A& r1 = c.emplace_back(2, 3.5);
116 assert(c.size() == 1);
117 assert(&r1 == &c.back());
118 assert(c.front().geti() == 2);
119 assert(c.front().getd() == 3.5);
120 assert(is_contiguous_container_asan_correct(c));
121 A& r2 = c.emplace_back(3, 4.5);
122 assert(c.size() == 2);
123 assert(&r2 == &c.back());
124 #else
125 c.emplace_back(2, 3.5);
126 assert(c.size() == 1);
127 assert(c.front().geti() == 2);
128 assert(c.front().getd() == 3.5);
129 assert(is_contiguous_container_asan_correct(c));
130 c.emplace_back(3, 4.5);
131 assert(c.size() == 2);
132 #endif
133 assert(c.front().geti() == 2);
134 assert(c.front().getd() == 3.5);
135 assert(c.back().geti() == 3);
136 assert(c.back().getd() == 4.5);
137 assert(is_contiguous_container_asan_correct(c));
140 std::vector<Tag_X, TaggingAllocator<Tag_X>> c;
141 c.emplace_back();
142 assert(c.size() == 1);
143 c.emplace_back(1, 2, 3);
144 assert(c.size() == 2);
145 assert(is_contiguous_container_asan_correct(c));
148 { // LWG 2164
149 int arr[] = {0, 1, 2, 3, 4};
150 int sz = 5;
151 std::vector<int> c(arr, arr+sz);
152 while (c.size() < c.capacity())
153 c.push_back(sz++);
154 c.emplace_back(c.front());
155 assert(c.back() == 0);
156 for (int i = 0; i < sz; ++i)
157 assert(c[i] == i);
159 return 0;