| 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 <deque> |
| 22 |
#include <testsuite_hooks.h> |
| 23 |
#include <testsuite_rvalref.h> |
| 24 |
|
| 25 |
using namespace __gnu_test; |
| 26 |
|
| 27 |
// Test deque::push_back makes no unneeded copies. |
| 28 |
void |
| 29 |
test01() |
| 30 |
{ |
| 31 |
std::deque<copycounter> a; |
| 32 |
copycounter c(1); |
| 33 |
copycounter::copycount = 0; |
| 34 |
for(int i = 0; i < 1000; ++i) |
| 35 |
a.push_back(c); |
| 36 |
VERIFY(copycounter::copycount == 1000); |
| 37 |
} |
| 38 |
|
| 39 |
// Test deque::push_front makes no unneeded copies. |
| 40 |
void |
| 41 |
test02() |
| 42 |
{ |
| 43 |
std::deque<copycounter> a; |
| 44 |
copycounter c(1); |
| 45 |
copycounter::copycount = 0; |
| 46 |
for(int i = 0; i < 1000; ++i) |
| 47 |
a.push_front(c); |
| 48 |
VERIFY(copycounter::copycount == 1000); |
| 49 |
} |
| 50 |
|
| 51 |
// Test deque::insert makes no unneeded copies. |
| 52 |
void |
| 53 |
test03() |
| 54 |
{ |
| 55 |
std::deque<copycounter> a(1000); |
| 56 |
copycounter c(1); |
| 57 |
copycounter::copycount = 0; |
| 58 |
a.insert(a.begin(),c); |
| 59 |
a.insert(a.end(),c); |
| 60 |
for(int i = 0; i < 500; ++i) |
| 61 |
a.insert(a.begin() + i, c); |
| 62 |
VERIFY(copycounter::copycount == 502); |
| 63 |
} |
| 64 |
|
| 65 |
// Test deque::insert(iterator, count, value) makes no unneeded copies |
| 66 |
// when it has to also reallocate the deque's internal buffer. |
| 67 |
void |
| 68 |
test04() |
| 69 |
{ |
| 70 |
copycounter c(1); |
| 71 |
std::deque<copycounter> a(10, c); |
| 72 |
copycounter::copycount = 0; |
| 73 |
a.insert(a.begin(), 20, c); |
| 74 |
VERIFY(copycounter::copycount == 20); |
| 75 |
a.insert(a.end(), 50, c); |
| 76 |
VERIFY(copycounter::copycount == 70); |
| 77 |
// NOTE : These values are each one higher than might be expected, as |
| 78 |
// deque::insert(iterator, count, value) copies the value it is given |
| 79 |
// when it has to move elements in the deque in case that value is |
| 80 |
// in the deque. |
| 81 |
|
| 82 |
// Near the start |
| 83 |
a.insert(a.begin() + 10, 100, c); |
| 84 |
VERIFY(copycounter::copycount == 170 + 1); |
| 85 |
// Near the end |
| 86 |
a.insert(a.end() - 10, 1000, c); |
| 87 |
VERIFY(copycounter::copycount == 1170 + 2); |
| 88 |
} |
| 89 |
|
| 90 |
// Test deque::insert(iterator, count, value) makes no unneeded copies |
| 91 |
// when it doesn't have to reallocate the deque's internal buffer. |
| 92 |
void |
| 93 |
test05() |
| 94 |
{ |
| 95 |
copycounter c(1); |
| 96 |
std::deque<copycounter> a(10, c); |
| 97 |
copycounter::copycount = 0; |
| 98 |
//a.reserve(1000); |
| 99 |
a.insert(a.begin(), 20, c); |
| 100 |
VERIFY(copycounter::copycount == 20 ); |
| 101 |
a.insert(a.end(), 50, c); |
| 102 |
VERIFY(copycounter::copycount == 70 ); |
| 103 |
|
| 104 |
// NOTE : These values are each one higher than might be expected, as |
| 105 |
// deque::insert(iterator, count, value) copies the value it is given |
| 106 |
// when it has to move elements in the deque in case that value is |
| 107 |
// in the deque. |
| 108 |
// Near the start |
| 109 |
a.insert(a.begin() + 10, 100, c); |
| 110 |
VERIFY(copycounter::copycount == 170 + 1); |
| 111 |
// Near the end |
| 112 |
a.insert(a.end() - 10, 200, c); |
| 113 |
VERIFY(copycounter::copycount == 370 + 2); |
| 114 |
} |
| 115 |
|
| 116 |
int main() |
| 117 |
{ |
| 118 |
test01(); |
| 119 |
test02(); |
| 120 |
test03(); |
| 121 |
test04(); |
| 122 |
test05(); |
| 123 |
return 0; |
| 124 |
} |