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 deque::push_back makes no unneeded copies.
31 std::deque
<copycounter
> a
;
33 copycounter::copycount
= 0;
34 for(int i
= 0; i
< 1000; ++i
)
36 VERIFY(copycounter::copycount
== 1000);
39 // Test deque::push_front makes no unneeded copies.
43 std::deque
<copycounter
> a
;
45 copycounter::copycount
= 0;
46 for(int i
= 0; i
< 1000; ++i
)
48 VERIFY(copycounter::copycount
== 1000);
51 // Test deque::insert makes no unneeded copies.
55 std::deque
<copycounter
> a(1000);
57 copycounter::copycount
= 0;
58 a
.insert(a
.begin(),c
);
60 for(int i
= 0; i
< 500; ++i
)
61 a
.insert(a
.begin() + i
, c
);
62 VERIFY(copycounter::copycount
== 502);
65 // Test deque::insert(iterator, count, value) makes no unneeded copies
66 // when it has to also reallocate the deque's internal buffer.
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
83 a
.insert(a
.begin() + 10, 100, c
);
84 VERIFY(copycounter::copycount
== 170 + 1);
86 a
.insert(a
.end() - 10, 1000, c
);
87 VERIFY(copycounter::copycount
== 1170 + 2);
90 // Test deque::insert(iterator, count, value) makes no unneeded copies
91 // when it doesn't have to reallocate the deque's internal buffer.
96 std::deque
<copycounter
> a(10, c
);
97 copycounter::copycount
= 0;
99 a
.insert(a
.begin(), 20, c
);
100 VERIFY(copycounter::copycount
== 20 );
101 a
.insert(a
.end(), 50, c
);
102 VERIFY(copycounter::copycount
== 70 );
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
109 a
.insert(a
.begin() + 10, 100, c
);
110 VERIFY(copycounter::copycount
== 170 + 1);
112 a
.insert(a
.end() - 10, 200, c
);
113 VERIFY(copycounter::copycount
== 370 + 2);