1 // Copyright (C) 2001, 2003 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>
24 typedef __gnu_test::copy_tracker T;
29 // This test verifies the following.
31 // 23.2.2.3 void push_front(const T& x)
32 // 23.2.2.3 void push_back(const T& x)
33 // 23.2.2.3 (1) iterator and reference non-invalidation
34 // 23.2.2.3 (1) exception effects
35 // 23.2.2.3 (2) complexity requirements
37 // 23.2.2.3 void pop_front()
38 // 23.2.2.3 void pop_back()
39 // 23.2.2.3 (3) iterator and reference non-invalidation
40 // 23.2.2.3 (5) complexity requirements
42 // 23.2.2 const_iterator begin() const
43 // 23.2.2 iterator end()
44 // 23.2.2 const_reverse_iterator rbegin() const
45 // 23.2.2 _reference front()
46 // 23.2.2 const_reference front() const
47 // 23.2.2 reference back()
48 // 23.2.2 const_reference back() const
53 std::list<T> list0101;
54 std::list<T>::const_iterator i;
55 std::list<T>::const_reverse_iterator j;
56 std::list<T>::iterator k;
59 list0101.push_back(T(1)); // list should be [1]
60 VERIFY(list0101.size() == 1);
61 VERIFY(T::copyCount() == 1);
66 VERIFY(k->id() == list0101.front().id());
67 VERIFY(k->id() == list0101.back().id());
69 list0101.push_front(T(2)); // list should be [2 1]
70 VERIFY(list0101.size() == 2);
71 VERIFY(T::copyCount() == 2);
74 list0101.push_back(T(3)); // list should be [2 1 3]
75 VERIFY(list0101.size() == 3);
76 VERIFY(T::copyCount() == 3);
81 list0101.push_back(T(4, true));
82 VERIFY(("no exception thrown", false));
86 VERIFY(list0101.size() == 3);
87 VERIFY(T::copyCount() == 4);
92 VERIFY(i->id() == list0101.front().id());
94 j = list0101.rbegin();
96 VERIFY(j->id() == list0101.back().id());
102 VERIFY(j->id() == 1);
106 list0101.pop_back(); // list should be [2 1]
107 VERIFY(list0101.size() == 2);
108 VERIFY(T::dtorCount() == 1);
109 VERIFY(i->id() == 1);
110 VERIFY(k->id() == 1);
112 list0101.pop_front(); // list should be [1]
113 VERIFY(list0101.size() == 1);
114 VERIFY(T::dtorCount() == 2);
115 VERIFY(i->id() == 1);
116 VERIFY(k->id() == 1);
119 // general single insert/erase + swap
123 std::list<T> list0201;
126 list0201.insert(list0201.begin(), T(1)); // list should be [1]
127 VERIFY(list0201.size() == 1);
128 VERIFY(T::copyCount() == 1);
130 list0201.insert(list0201.end(), T(2)); // list should be [1 2]
131 VERIFY(list0201.size() == 2);
132 VERIFY(T::copyCount() == 2);
134 std::list<T>::iterator i = list0201.begin();
135 std::list<T>::const_iterator j = i;
136 VERIFY(i->id() == 1); ++i;
137 VERIFY(i->id() == 2);
139 list0201.insert(i, T(3)); // list should be [1 3 2]
140 VERIFY(list0201.size() == 3);
141 VERIFY(T::copyCount() == 3);
143 std::list<T>::const_iterator k = i;
144 VERIFY(i->id() == 2); --i;
145 VERIFY(i->id() == 3); --i;
146 VERIFY(i->id() == 1);
147 VERIFY(j->id() == 1);
149 ++i; // will point to '3'
151 list0201.erase(i); // should be [1 2]
152 VERIFY(list0201.size() == 2);
153 VERIFY(T::dtorCount() == 1);
154 VERIFY(k->id() == 2);
155 VERIFY(j->id() == 1);
157 std::list<T> list0202;
159 VERIFY(list0202.size() == 0);
160 VERIFY(T::copyCount() == 0);
161 VERIFY(T::dtorCount() == 0);
164 list0202.swap(list0201);
165 VERIFY(list0201.size() == 0);
166 VERIFY(list0202.size() == 2);
167 VERIFY(T::copyCount() == 0);
168 VERIFY(T::dtorCount() == 0);
171 swap(list0201, list0202);
172 VERIFY(list0201.size() == 2);
173 VERIFY(list0202.size() == 0);
174 VERIFY(T::copyCount() == 0);
175 VERIFY(T::dtorCount() == 0);
178 // range and fill insert/erase + clear
179 // missing: o fill insert disguised as a range insert in all its variants
180 // o exception effects
184 std::list<T> list0301;
187 // fill insert at beginning of list / empty list
188 list0301.insert(list0301.begin(), 3, T(11)); // should be [11 11 11]
189 VERIFY(list0301.size() == 3);
190 VERIFY(T::copyCount() == 3);
192 // save iterators to verify post-insert validity
193 std::list<T>::iterator b = list0301.begin();
194 std::list<T>::iterator m = list0301.end(); --m;
195 std::list<T>::iterator e = list0301.end();
197 // fill insert at end of list
199 list0301.insert(list0301.end(), 3, T(13)); // should be [11 11 11 13 13 13]
200 VERIFY(list0301.size() == 6);
201 VERIFY(T::copyCount() == 3);
202 VERIFY(b == list0301.begin() && b->id() == 11);
203 VERIFY(e == list0301.end());
204 VERIFY(m->id() == 11);
206 // fill insert in the middle of list
209 list0301.insert(m, 3, T(12)); // should be [11 11 11 12 12 12 13 13 13]
210 VERIFY(list0301.size() == 9);
211 VERIFY(T::copyCount() == 3);
212 VERIFY(b == list0301.begin() && b->id() == 11);
213 VERIFY(e == list0301.end());
214 VERIFY(m->id() == 13);
218 m = list0301.erase(m); // should be [11 11 11 12 12 12 13 13]
219 VERIFY(list0301.size() == 8);
220 VERIFY(T::dtorCount() == 1);
221 VERIFY(b == list0301.begin() && b->id() == 11);
222 VERIFY(e == list0301.end());
223 VERIFY(m->id() == 13);
227 m = list0301.erase(list0301.begin(), m); // should be [13 13]
228 VERIFY(list0301.size() == 2);
229 VERIFY(T::dtorCount() == 6);
230 VERIFY(m->id() == 13);
232 // range fill at beginning
233 const int A[] = {321, 322, 333};
234 const int N = sizeof(A) / sizeof(int);
236 b = list0301.begin();
237 list0301.insert(b, A, A + N); // should be [321 322 333 13 13]
238 VERIFY(list0301.size() == 5);
239 VERIFY(T::copyCount() == 3);
240 VERIFY(m->id() == 13);
244 list0301.insert(e, A, A + N); // should be [321 322 333 13 13 321 322 333]
245 VERIFY(list0301.size() == 8);
246 VERIFY(T::copyCount() == 3);
247 VERIFY(e == list0301.end());
248 VERIFY(m->id() == 13);
250 // range fill in middle
252 list0301.insert(m, A, A + N);
253 VERIFY(list0301.size() == 11);
254 VERIFY(T::copyCount() == 3);
255 VERIFY(e == list0301.end());
256 VERIFY(m->id() == 13);
260 VERIFY(list0301.size() == 0);
261 VERIFY(T::dtorCount() == 11);
262 VERIFY(e == list0301.end());