]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/23_containers/vector/modifiers/emplace/self_emplace.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / vector / modifiers / emplace / self_emplace.cc
CommitLineData
52066eae 1// { dg-do run { target c++11 } }
097e8994 2
8d9254fc 3// Copyright (C) 2016-2020 Free Software Foundation, Inc.
097e8994
FD
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
19
20#include <vector>
21#include "testsuite_hooks.h"
22
097e8994
FD
23void
24test01()
25{
26 std::vector<std::vector<int>> vv =
27 {
28 { 2, 3 },
29 { 4, 5 },
30 { 0, 1 }
31 };
32
33 // Make sure emplace will imply reallocation.
34 VERIFY( vv.capacity() == 3 );
35
36 vv.emplace(vv.begin(), vv[0]);
37
38 VERIFY( vv.size() == 4 );
39 VERIFY( vv[0].size() == 2 );
40 VERIFY( vv[0][0] == 2 );
41 VERIFY( vv[0][1] == 3 );
42}
43
44void
45test02()
46{
47 std::vector<std::vector<int>> vv =
48 {
49 { 2, 3 },
50 { 4, 5 },
51 { 0, 1 }
52 };
53
54 // Make sure emplace won't reallocate.
55 vv.reserve(4);
56 vv.emplace(vv.begin(), vv[0]);
57
58 VERIFY( vv.size() == 4 );
59 VERIFY( vv[0].size() == 2 );
60 VERIFY( vv[0][0] == 2 );
61 VERIFY( vv[0][1] == 3 );
62}
63
64struct A
65{
66 A(int i) : _i(i)
67 { }
68
69 A(const A& other) : _i(other._i)
70 {
71 VERIFY( other._i >= 0 );
72 }
73
74 A(A&& other) : _i(other._i)
75 {
76 VERIFY( other._i >= 0 );
77
78 other._i = -1;
79 }
80
81 A(std::vector<A>::iterator it) : _i(it->_i)
82 {
83 VERIFY( it->_i >= 0 );
84 }
85
86 A& operator=(const A&) = default;
87 A& operator=(A&& other)
88 {
89 VERIFY(other._i >= 0 );
90
91 _i = other._i;
92 other._i = -1;
93 return *this;
94 }
95
96 int _i;
97};
98
99void
100test03()
101{
102 std::vector<A> va =
103 {
104 { A(1) },
105 { A(2) },
106 { A(3) }
107 };
108
109 // Make sure emplace will imply reallocation.
110 VERIFY( va.capacity() == 3 );
111
112 va.emplace(va.begin(), va.begin());
113
114 VERIFY( va.size() == 4 );
115 VERIFY( va[0]._i == 1 );
116}
117
118void
119test04()
120{
121 std::vector<A> va =
122 {
123 { A(1) },
124 { A(2) },
125 { A(3) }
126 };
127
128 // Make sure emplace won't reallocate.
129 va.reserve(4);
130 va.emplace(va.begin(), va.begin());
131
132 VERIFY( va.size() == 4 );
133 VERIFY( va[0]._i == 1 );
134}
135
17b31c05
JW
136void
137test05()
138{
139 // LWG DR 2164
140 std::vector<int> v;
141 v.reserve(4);
142 v = { 1, 2, 3 };
143 v.emplace(v.begin(), v.back());
144 VERIFY( v[0] == 3 );
145 VERIFY( v[1] == 1 );
146 VERIFY( v[2] == 2 );
147 VERIFY( v[3] == 3 );
148}
149
097e8994
FD
150int main()
151{
152 test01();
153 test02();
154 test03();
155 test04();
156}