1 // Copyright (C) 2001 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without Pred the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // 23.2.2.3 list modifiers [lib.list.modifiers]
22 #include <testsuite_hooks.h>
26 // Here's a class with nontrivial ctor/dtor that provides
27 // the ability to track the number of copy ctors and dtors
28 // and will throw on demand during copy.
32 // default constructor
33 T(int anId, bool throwOnDemand = false)
34 : itsId(anId), willThrow(throwOnDemand)
39 : itsId(rhs.id()), willThrow(rhs.willThrow)
60 { itsCopyCount = 0; itsDtorCount = 0; }
64 { return itsCopyCount; }
68 { return itsDtorCount; }
71 static int itsCopyCount;
72 static int itsDtorCount;
75 int T::itsCopyCount = 0;
76 int T::itsDtorCount = 0;
79 // This test verifies the following.
81 // 23.2.2.3 void push_front(const T& x)
82 // 23.2.2.3 void push_back(const T& x)
83 // 23.2.2.3 (1) iterator and reference non-invalidation
84 // 23.2.2.3 (1) exception effects
85 // 23.2.2.3 (2) complexity requirements
87 // 23.2.2.3 void pop_front()
88 // 23.2.2.3 void pop_back()
89 // 23.2.2.3 (3) iterator and reference non-invalidation
90 // 23.2.2.3 (5) complexity requirements
92 // 23.2.2 const_iterator begin() const
93 // 23.2.2 iterator end()
94 // 23.2.2 const_reverse_iterator rbegin() const
95 // 23.2.2 _reference front()
96 // 23.2.2 const_reference front() const
97 // 23.2.2 reference back()
98 // 23.2.2 const_reference back() const
103 std::list<T> list0101;
104 std::list<T>::const_iterator i;
105 std::list<T>::const_reverse_iterator j;
106 std::list<T>::iterator k;
109 list0101.push_back(T(1)); // list should be [1]
110 VERIFY(list0101.size() == 1);
111 VERIFY(T::copyCount() == 1);
115 VERIFY(k->id() == 1);
116 VERIFY(k->id() == list0101.front().id());
117 VERIFY(k->id() == list0101.back().id());
119 list0101.push_front(T(2)); // list should be [2 1]
120 VERIFY(list0101.size() == 2);
121 VERIFY(T::copyCount() == 2);
122 VERIFY(k->id() == 1);
124 list0101.push_back(T(3)); // list should be [2 1 3]
125 VERIFY(list0101.size() == 3);
126 VERIFY(T::copyCount() == 3);
127 VERIFY(k->id() == 1);
131 list0101.push_back(T(4, true));
132 VERIFY(("no exception thrown", false));
136 VERIFY(list0101.size() == 3);
137 VERIFY(T::copyCount() == 4);
140 i = list0101.begin();
141 VERIFY(i->id() == 2);
142 VERIFY(i->id() == list0101.front().id());
144 j = list0101.rbegin();
145 VERIFY(j->id() == 3);
146 VERIFY(j->id() == list0101.back().id());
149 VERIFY(i->id() == 1);
152 VERIFY(j->id() == 1);
156 list0101.pop_back(); // list should be [2 1]
157 VERIFY(list0101.size() == 2);
158 VERIFY(T::dtorCount() == 1);
159 VERIFY(i->id() == 1);
160 VERIFY(j->id() == 1);
161 VERIFY(k->id() == 1);
163 list0101.pop_front(); // list should be [1]
164 VERIFY(list0101.size() == 1);
165 VERIFY(T::dtorCount() == 2);
166 VERIFY(i->id() == 1);
167 VERIFY(j->id() == 1);
168 VERIFY(k->id() == 1);
171 // general single insert/erase + swap
175 std::list<T> list0201;
178 list0201.insert(list0201.begin(), T(1)); // list should be [1]
179 VERIFY(list0201.size() == 1);
180 VERIFY(T::copyCount() == 1);
182 list0201.insert(list0201.end(), T(2)); // list should be [1 2]
183 VERIFY(list0201.size() == 2);
184 VERIFY(T::copyCount() == 2);
186 std::list<T>::iterator i = list0201.begin();
187 std::list<T>::const_iterator j = i;
188 VERIFY(i->id() == 1); ++i;
189 VERIFY(i->id() == 2);
191 list0201.insert(i, T(3)); // list should be [1 3 2]
192 VERIFY(list0201.size() == 3);
193 VERIFY(T::copyCount() == 3);
195 std::list<T>::const_iterator k = i;
196 VERIFY(i->id() == 2); --i;
197 VERIFY(i->id() == 3); --i;
198 VERIFY(i->id() == 1);
199 VERIFY(j->id() == 1);
201 ++i; // will point to '3'
203 list0201.erase(i); // should be [1 2]
204 VERIFY(list0201.size() == 2);
205 VERIFY(T::dtorCount() == 1);
206 VERIFY(k->id() == 2);
207 VERIFY(j->id() == 1);
209 std::list<T> list0202;
211 VERIFY(list0202.size() == 0);
212 VERIFY(T::copyCount() == 0);
213 VERIFY(T::dtorCount() == 0);
216 list0202.swap(list0201);
217 VERIFY(list0201.size() == 0);
218 VERIFY(list0202.size() == 2);
219 VERIFY(T::copyCount() == 0);
220 VERIFY(T::dtorCount() == 0);
223 swap(list0201, list0202);
224 VERIFY(list0201.size() == 2);
225 VERIFY(list0202.size() == 0);
226 VERIFY(T::copyCount() == 0);
227 VERIFY(T::dtorCount() == 0);
230 // range and fill insert/erase + clear
231 // missing: o fill insert disguised as a range insert in all its variants
232 // o exception effects
236 std::list<T> list0301;
239 // fill insert at beginning of list / empty list
240 list0301.insert(list0301.begin(), 3, T(11)); // should be [11 11 11]
241 VERIFY(list0301.size() == 3);
242 VERIFY(T::copyCount() == 3);
244 // save iterators to verify post-insert validity
245 std::list<T>::iterator b = list0301.begin();
246 std::list<T>::iterator m = list0301.end(); --m;
247 std::list<T>::iterator e = list0301.end();
249 // fill insert at end of list
251 list0301.insert(list0301.end(), 3, T(13)); // should be [11 11 11 13 13 13]
252 VERIFY(list0301.size() == 6);
253 VERIFY(T::copyCount() == 3);
254 VERIFY(b == list0301.begin() && b->id() == 11);
255 VERIFY(e == list0301.end());
256 VERIFY(m->id() == 11);
258 // fill insert in the middle of list
261 list0301.insert(m, 3, T(12)); // should be [11 11 11 12 12 12 13 13 13]
262 VERIFY(list0301.size() == 9);
263 VERIFY(T::copyCount() == 3);
264 VERIFY(b == list0301.begin() && b->id() == 11);
265 VERIFY(e == list0301.end());
266 VERIFY(m->id() == 13);
270 m = list0301.erase(m); // should be [11 11 11 12 12 12 13 13]
271 VERIFY(list0301.size() == 8);
272 VERIFY(T::dtorCount() == 1);
273 VERIFY(b == list0301.begin() && b->id() == 11);
274 VERIFY(e == list0301.end());
275 VERIFY(m->id() == 13);
279 m = list0301.erase(list0301.begin(), m); // should be [13 13]
280 VERIFY(list0301.size() == 2);
281 VERIFY(T::dtorCount() == 6);
282 VERIFY(m->id() == 13);
284 // range fill at beginning
285 const int A[] = {321, 322, 333};
286 const int N = sizeof(A) / sizeof(int);
288 b = list0301.begin();
289 list0301.insert(b, A, A + N); // should be [321 322 333 13 13]
290 VERIFY(list0301.size() == 5);
291 VERIFY(T::copyCount() == 3);
292 VERIFY(m->id() == 13);
296 list0301.insert(e, A, A + N); // should be [321 322 333 13 13 321 322 333]
297 VERIFY(list0301.size() == 8);
298 VERIFY(T::copyCount() == 3);
299 VERIFY(e == list0301.end());
300 VERIFY(m->id() == 13);
302 // range fill in middle
304 list0301.insert(m, A, A + N);
305 VERIFY(list0301.size() == 11);
306 VERIFY(T::copyCount() == 3);
307 VERIFY(e == list0301.end());
308 VERIFY(m->id() == 13);
312 VERIFY(list0301.size() == 0);
313 VERIFY(T::dtorCount() == 11);
314 VERIFY(e == list0301.end());
317 main(int argc, char* argv[])