1 // { dg-do run { target c++11 } }
3 // Copyright (C) 2005-2025 Free Software Foundation, Inc.
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)
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.
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/>.
22 #include <testsuite_hooks.h>
23 #include <testsuite_rvalref.h>
25 using namespace __gnu_test
;
27 // Test vector::push_back makes no unneeded copies.
31 std::vector
<copycounter
> a
;
33 copycounter::copycount
= 0;
34 for(int i
= 0; i
< 10; ++i
)
36 VERIFY(copycounter::copycount
== 10);
38 for(int i
= 0; i
< 100; ++i
)
39 a
.insert(a
.begin() + i
, c
);
40 VERIFY(copycounter::copycount
== 110);
42 for(int i
= 0; i
< 1000; ++i
)
44 VERIFY(copycounter::copycount
== 1110);
47 // Test vector::insert(iterator, iterator, iterator) makes no unneeded copies
48 // when it has to also reallocate the vector's internal buffer.
53 std::vector
<copycounter
> a(10, c
), b(100, c
);
54 copycounter::copycount
= 0;
55 a
.insert(a
.begin(), b
.begin(), b
.begin() + 20);
56 VERIFY(copycounter::copycount
== 20);
57 a
.insert(a
.end(), b
.begin(), b
.begin() + 50);
58 VERIFY(copycounter::copycount
== 70);
59 a
.insert(a
.begin() + 50, b
.begin(), b
.end());
60 VERIFY(copycounter::copycount
== 170);
63 // Test vector::insert(iterator, iterator, iterator) makes no unneeded copies
64 // when it doesn't have to reallocate the vector's internal buffer.
69 std::vector
<copycounter
> a(10, c
), b(100, c
);
70 copycounter::copycount
= 0;
72 VERIFY(copycounter::copycount
== 0);
73 a
.insert(a
.begin(), b
.begin(), b
.begin() + 20);
74 VERIFY(copycounter::copycount
== 20);
75 a
.insert(a
.end(), b
.begin(), b
.begin() + 50);
76 VERIFY(copycounter::copycount
== 70);
77 a
.insert(a
.begin() + 50, b
.begin(), b
.end());
78 VERIFY(copycounter::copycount
== 170);
81 // Test vector::insert(iterator, count, value) makes no unneeded copies
82 // when it has to also reallocate the vector's internal buffer.
87 std::vector
<copycounter
> a(10, c
);
88 copycounter::copycount
= 0;
89 a
.insert(a
.begin(), 20, c
);
90 VERIFY(copycounter::copycount
== 20);
91 a
.insert(a
.end(), 50, c
);
92 VERIFY(copycounter::copycount
== 70);
93 a
.insert(a
.begin() + 50, 100, c
);
94 VERIFY(copycounter::copycount
== 170);
97 // Test vector::insert(iterator, count, value) makes no unneeded copies
98 // when it doesn't have to reallocate the vector's internal buffer.
103 std::vector
<copycounter
> a(10, c
);
104 copycounter::copycount
= 0;
106 a
.insert(a
.begin(), 20, c
);
107 // NOTE : These values are each one higher than might be expected, as
108 // vector::insert(iterator, count, value) copies the value it is given
109 // when it doesn't reallocate the buffer.
110 VERIFY(copycounter::copycount
== 20 + 1);
111 a
.insert(a
.end(), 50, c
);
112 VERIFY(copycounter::copycount
== 70 + 2);
113 a
.insert(a
.begin() + 50, 100, c
);
114 VERIFY(copycounter::copycount
== 170 + 3);