| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // Copyright (C) 2005-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 | |
| 21 | #include <vector> |
| 22 | #include <testsuite_hooks.h> |
| 23 | #include <testsuite_rvalref.h> |
| 24 | |
| 25 | using namespace __gnu_test; |
| 26 | |
| 27 | // Test vector::push_back makes no unneeded copies. |
| 28 | void |
| 29 | test01() |
| 30 | { |
| 31 | std::vector<copycounter> a; |
| 32 | copycounter c(1); |
| 33 | copycounter::copycount = 0; |
| 34 | for(int i = 0; i < 10; ++i) |
| 35 | a.push_back(c); |
| 36 | VERIFY(copycounter::copycount == 10); |
| 37 | |
| 38 | for(int i = 0; i < 100; ++i) |
| 39 | a.insert(a.begin() + i, c); |
| 40 | VERIFY(copycounter::copycount == 110); |
| 41 | |
| 42 | for(int i = 0; i < 1000; ++i) |
| 43 | a.insert(a.end(), c); |
| 44 | VERIFY(copycounter::copycount == 1110); |
| 45 | } |
| 46 | |
| 47 | // Test vector::insert(iterator, iterator, iterator) makes no unneeded copies |
| 48 | // when it has to also reallocate the vector's internal buffer. |
| 49 | void |
| 50 | test02() |
| 51 | { |
| 52 | copycounter c(1); |
| 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); |
| 61 | } |
| 62 | |
| 63 | // Test vector::insert(iterator, iterator, iterator) makes no unneeded copies |
| 64 | // when it doesn't have to reallocate the vector's internal buffer. |
| 65 | void |
| 66 | test03() |
| 67 | { |
| 68 | copycounter c(1); |
| 69 | std::vector<copycounter> a(10, c), b(100, c); |
| 70 | copycounter::copycount = 0; |
| 71 | a.reserve(1000); |
| 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); |
| 79 | } |
| 80 | |
| 81 | // Test vector::insert(iterator, count, value) makes no unneeded copies |
| 82 | // when it has to also reallocate the vector's internal buffer. |
| 83 | void |
| 84 | test04() |
| 85 | { |
| 86 | copycounter c(1); |
| 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); |
| 95 | } |
| 96 | |
| 97 | // Test vector::insert(iterator, count, value) makes no unneeded copies |
| 98 | // when it doesn't have to reallocate the vector's internal buffer. |
| 99 | void |
| 100 | test05() |
| 101 | { |
| 102 | copycounter c(1); |
| 103 | std::vector<copycounter> a(10, c); |
| 104 | copycounter::copycount = 0; |
| 105 | a.reserve(1000); |
| 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 | // expect when inserting at the end (appending), where existing |
| 113 | // elements are not modified |
| 114 | VERIFY(copycounter::copycount == 70 + 1); |
| 115 | a.insert(a.begin() + 50, 100, c); |
| 116 | VERIFY(copycounter::copycount == 170 + 2); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | int main() |
| 122 | { |
| 123 | test01(); |
| 124 | test02(); |
| 125 | test03(); |
| 126 | test04(); |
| 127 | test05(); |
| 128 | return 0; |
| 129 | } |