]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/vector/modifiers/moveable.cc
stl_vector.h (vector<>::push_back<>(_Args...), [...]): Add.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / vector / modifiers / moveable.cc
1 // { dg-options "-std=gnu++0x" }
2
3 // Copyright (C) 2005, 2007 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 2, 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 COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 #include <vector>
31 #include <testsuite_hooks.h>
32 #include <testsuite_rvalref.h>
33
34 using namespace __gnu_test;
35
36 // Test vector::push_back makes no unneeded copies.
37 void
38 test01()
39 {
40 bool test __attribute__((unused)) = true;
41
42 std::vector<copycounter> a;
43 copycounter c(1);
44 copycounter::copycount = 0;
45 for(int i = 0; i < 10; ++i)
46 a.push_back(c);
47 VERIFY(copycounter::copycount == 10);
48
49 for(int i = 0; i < 100; ++i)
50 a.insert(a.begin() + i, c);
51 VERIFY(copycounter::copycount == 110);
52
53 for(int i = 0; i < 1000; ++i)
54 a.insert(a.end(), c);
55 VERIFY(copycounter::copycount == 1110);
56 }
57
58 // Test vector::insert(iterator, iterator, iterator) makes no unneeded copies
59 // when it has to also reallocate the vector's internal buffer.
60 void
61 test02()
62 {
63 bool test __attribute__((unused)) = true;
64
65 copycounter c(1);
66 std::vector<copycounter> a(10, c), b(100, c);
67 copycounter::copycount = 0;
68 a.insert(a.begin(), b.begin(), b.begin() + 20);
69 VERIFY(copycounter::copycount == 20);
70 a.insert(a.end(), b.begin(), b.begin() + 50);
71 VERIFY(copycounter::copycount == 70);
72 a.insert(a.begin() + 50, b.begin(), b.end());
73 VERIFY(copycounter::copycount == 170);
74 }
75
76 // Test vector::insert(iterator, iterator, iterator) makes no unneeded copies
77 // when it doesn't have to reallocate the vector's internal buffer.
78 void
79 test03()
80 {
81 bool test __attribute__((unused)) = true;
82
83 copycounter c(1);
84 std::vector<copycounter> a(10, c), b(100, c);
85 copycounter::copycount = 0;
86 a.reserve(1000);
87 VERIFY(copycounter::copycount == 0);
88 a.insert(a.begin(), b.begin(), b.begin() + 20);
89 VERIFY(copycounter::copycount == 20);
90 a.insert(a.end(), b.begin(), b.begin() + 50);
91 VERIFY(copycounter::copycount == 70);
92 a.insert(a.begin() + 50, b.begin(), b.end());
93 VERIFY(copycounter::copycount == 170);
94 }
95
96 // Test vector::insert(iterator, count, value) makes no unneeded copies
97 // when it has to also reallocate the vector's internal buffer.
98 void
99 test04()
100 {
101 bool test __attribute__((unused)) = true;
102
103 copycounter c(1);
104 std::vector<copycounter> a(10, c);
105 copycounter::copycount = 0;
106 a.insert(a.begin(), 20, c);
107 VERIFY(copycounter::copycount == 20 + 1);
108 a.insert(a.end(), 50, c);
109 VERIFY(copycounter::copycount == 70 + 2);
110 a.insert(a.begin() + 50, 100, c);
111 VERIFY(copycounter::copycount == 170 + 3);
112 }
113
114 // Test vector::insert(iterator, count, value) makes no unneeded copies
115 // when it doesn't have to reallocate the vector's internal buffer.
116 void
117 test05()
118 {
119 bool test __attribute__((unused)) = true;
120
121 copycounter c(1);
122 std::vector<copycounter> a(10, c);
123 copycounter::copycount = 0;
124 a.reserve(1000);
125 a.insert(a.begin(), 20, c);
126 // NOTE : These values are each one higher than might be expected, as
127 // vector::insert(iterator, count, value) copies the value it is given
128 // when it doesn't reallocate the buffer.
129 VERIFY(copycounter::copycount == 20 + 1);
130 a.insert(a.end(), 50, c);
131 VERIFY(copycounter::copycount == 70 + 2);
132 a.insert(a.begin() + 50, 100, c);
133 VERIFY(copycounter::copycount == 170 + 3);
134 }
135
136
137
138 int main()
139 {
140 test01();
141 test02();
142 test03();
143 test04();
144 test05();
145 return 0;
146 }