| 1 |
// { dg-do run { target c++11 } } |
| 2 |
|
| 3 |
// Copyright (C) 2016-2025 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 |
void |
| 24 |
test01() |
| 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 |
|
| 44 |
void |
| 45 |
test02() |
| 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 |
|
| 64 |
struct 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 |
|
| 99 |
void |
| 100 |
test03() |
| 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 |
|
| 118 |
void |
| 119 |
test04() |
| 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 |
|
| 136 |
void |
| 137 |
test05() |
| 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 |
|
| 150 |
int main() |
| 151 |
{ |
| 152 |
test01(); |
| 153 |
test02(); |
| 154 |
test03(); |
| 155 |
test04(); |
| 156 |
} |