]>
Commit | Line | Data |
---|---|---|
097e8994 FD |
1 | // { dg-options "-std=gnu++11" } |
2 | ||
3 | // Copyright (C) 2016 Free Software Foundation, Inc. | |
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 | ||
23 | bool test __attribute__((unused)) = true; | |
24 | ||
25 | void | |
26 | test01() | |
27 | { | |
28 | std::vector<std::vector<int>> vv = | |
29 | { | |
30 | { 2, 3 }, | |
31 | { 4, 5 }, | |
32 | { 0, 1 } | |
33 | }; | |
34 | ||
35 | // Make sure emplace will imply reallocation. | |
36 | VERIFY( vv.capacity() == 3 ); | |
37 | ||
38 | vv.emplace(vv.begin(), vv[0]); | |
39 | ||
40 | VERIFY( vv.size() == 4 ); | |
41 | VERIFY( vv[0].size() == 2 ); | |
42 | VERIFY( vv[0][0] == 2 ); | |
43 | VERIFY( vv[0][1] == 3 ); | |
44 | } | |
45 | ||
46 | void | |
47 | test02() | |
48 | { | |
49 | std::vector<std::vector<int>> vv = | |
50 | { | |
51 | { 2, 3 }, | |
52 | { 4, 5 }, | |
53 | { 0, 1 } | |
54 | }; | |
55 | ||
56 | // Make sure emplace won't reallocate. | |
57 | vv.reserve(4); | |
58 | vv.emplace(vv.begin(), vv[0]); | |
59 | ||
60 | VERIFY( vv.size() == 4 ); | |
61 | VERIFY( vv[0].size() == 2 ); | |
62 | VERIFY( vv[0][0] == 2 ); | |
63 | VERIFY( vv[0][1] == 3 ); | |
64 | } | |
65 | ||
66 | struct A | |
67 | { | |
68 | A(int i) : _i(i) | |
69 | { } | |
70 | ||
71 | A(const A& other) : _i(other._i) | |
72 | { | |
73 | VERIFY( other._i >= 0 ); | |
74 | } | |
75 | ||
76 | A(A&& other) : _i(other._i) | |
77 | { | |
78 | VERIFY( other._i >= 0 ); | |
79 | ||
80 | other._i = -1; | |
81 | } | |
82 | ||
83 | A(std::vector<A>::iterator it) : _i(it->_i) | |
84 | { | |
85 | VERIFY( it->_i >= 0 ); | |
86 | } | |
87 | ||
88 | A& operator=(const A&) = default; | |
89 | A& operator=(A&& other) | |
90 | { | |
91 | VERIFY(other._i >= 0 ); | |
92 | ||
93 | _i = other._i; | |
94 | other._i = -1; | |
95 | return *this; | |
96 | } | |
97 | ||
98 | int _i; | |
99 | }; | |
100 | ||
101 | void | |
102 | test03() | |
103 | { | |
104 | std::vector<A> va = | |
105 | { | |
106 | { A(1) }, | |
107 | { A(2) }, | |
108 | { A(3) } | |
109 | }; | |
110 | ||
111 | // Make sure emplace will imply reallocation. | |
112 | VERIFY( va.capacity() == 3 ); | |
113 | ||
114 | va.emplace(va.begin(), va.begin()); | |
115 | ||
116 | VERIFY( va.size() == 4 ); | |
117 | VERIFY( va[0]._i == 1 ); | |
118 | } | |
119 | ||
120 | void | |
121 | test04() | |
122 | { | |
123 | std::vector<A> va = | |
124 | { | |
125 | { A(1) }, | |
126 | { A(2) }, | |
127 | { A(3) } | |
128 | }; | |
129 | ||
130 | // Make sure emplace won't reallocate. | |
131 | va.reserve(4); | |
132 | va.emplace(va.begin(), va.begin()); | |
133 | ||
134 | VERIFY( va.size() == 4 ); | |
135 | VERIFY( va[0]._i == 1 ); | |
136 | } | |
137 | ||
17b31c05 JW |
138 | void |
139 | test05() | |
140 | { | |
141 | // LWG DR 2164 | |
142 | std::vector<int> v; | |
143 | v.reserve(4); | |
144 | v = { 1, 2, 3 }; | |
145 | v.emplace(v.begin(), v.back()); | |
146 | VERIFY( v[0] == 3 ); | |
147 | VERIFY( v[1] == 1 ); | |
148 | VERIFY( v[2] == 2 ); | |
149 | VERIFY( v[3] == 3 ); | |
150 | } | |
151 | ||
097e8994 FD |
152 | int main() |
153 | { | |
154 | test01(); | |
155 | test02(); | |
156 | test03(); | |
157 | test04(); | |
158 | } |