OSDN Git Service

2001-11-22 Stephen M. Webb <stephen@bregmasoft.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 23_containers / list_modifiers.cc
1 // Copyright (C) 2001 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
8
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.
13
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,
17 // USA.
18
19 // 23.2.2.3 list modifiers [lib.list.modifiers]
20
21 #include <list>
22 #include <testsuite_hooks.h>
23
24 bool test = true;
25
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.
29 class T
30 {
31 public:
32   // default constructor
33   T(int anId, bool throwOnDemand = false)
34   : itsId(anId), willThrow(throwOnDemand)
35   { }
36
37   // copy constructor
38   T(const T& rhs)
39   : itsId(rhs.id()), willThrow(rhs.willThrow)
40   {
41     ++itsCopyCount;
42     if (willThrow) 
43       throw "exception";
44   }
45
46   ~T()
47   { ++itsDtorCount; }
48
49   int
50   id() const
51   { return itsId; }
52
53 private:
54   const int  itsId;
55   const bool willThrow;
56
57 public:
58   static void
59   reset()
60   { itsCopyCount = 0; itsDtorCount = 0; }
61
62   static int
63   copyCount() 
64   { return itsCopyCount; }
65
66   static int
67   dtorCount() 
68   { return itsDtorCount; }
69
70 private:
71   static int itsCopyCount;
72   static int itsDtorCount;
73 };
74
75 int T::itsCopyCount = 0;
76 int T::itsDtorCount = 0;
77
78
79 // This test verifies the following.
80 //
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
86 //
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
91 //
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
99 //
100 void
101 test01()
102 {
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;
107   T::reset();
108
109   list0101.push_back(T(1));     // list should be [1]
110   VERIFY(list0101.size() == 1);
111   VERIFY(T::copyCount() == 1);
112
113   k = list0101.end();
114   --k;
115   VERIFY(k->id() == 1);
116   VERIFY(k->id() == list0101.front().id());
117   VERIFY(k->id() == list0101.back().id());
118
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);
123
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);
128
129   try
130   {
131     list0101.push_back(T(4, true));
132     VERIFY(("no exception thrown", false));
133   }
134   catch (...)
135   {
136     VERIFY(list0101.size() == 3);
137     VERIFY(T::copyCount() == 4);
138   }
139
140   i = list0101.begin();
141   VERIFY(i->id() == 2);
142   VERIFY(i->id() == list0101.front().id());
143
144   j = list0101.rbegin();
145   VERIFY(j->id() == 3);
146   VERIFY(j->id() == list0101.back().id());
147
148   ++i;
149   VERIFY(i->id() == 1);
150
151   ++j;
152   VERIFY(j->id() == 1);
153
154   T::reset();
155
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);
162
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);
169 }
170
171 // general single insert/erase + swap
172 void
173 test02()
174 {
175   std::list<T> list0201;
176   T::reset();
177
178   list0201.insert(list0201.begin(), T(1));     // list should be [1]
179   VERIFY(list0201.size() == 1);
180   VERIFY(T::copyCount() == 1);
181
182   list0201.insert(list0201.end(), T(2));     // list should be [1 2]
183   VERIFY(list0201.size() == 2);
184   VERIFY(T::copyCount() == 2);
185
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);
190
191   list0201.insert(i, T(3));     // list should be [1 3 2]
192   VERIFY(list0201.size() == 3);
193   VERIFY(T::copyCount() == 3);
194
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); 
200
201   ++i; // will point to '3'
202   T::reset();
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); 
208
209   std::list<T> list0202;
210   T::reset();
211   VERIFY(list0202.size() == 0);
212   VERIFY(T::copyCount() == 0);
213   VERIFY(T::dtorCount() == 0);
214
215   // member swap
216   list0202.swap(list0201);
217   VERIFY(list0201.size() == 0);
218   VERIFY(list0202.size() == 2);
219   VERIFY(T::copyCount() == 0);
220   VERIFY(T::dtorCount() == 0);
221
222   // specialized swap
223   swap(list0201, list0202);
224   VERIFY(list0201.size() == 2);
225   VERIFY(list0202.size() == 0);
226   VERIFY(T::copyCount() == 0);
227   VERIFY(T::dtorCount() == 0);
228 }
229
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
233 void
234 test03()
235 {
236   std::list<T> list0301;
237   T::reset();
238
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);
243
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();
248
249   // fill insert at end of list
250   T::reset();
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);
257
258   // fill insert in the middle of list
259   ++m;
260   T::reset();
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);
267
268   // single erase
269   T::reset();
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);
276
277   // range erase
278   T::reset();
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);
283
284   // range fill at beginning
285   const int A[] = {321, 322, 333};
286   const int N = sizeof(A) / sizeof(int);
287   T::reset();
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);
293   
294   // range fill at end
295   T::reset();
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);
301   
302   // range fill in middle
303   T::reset();
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);
309
310   T::reset();
311   list0301.clear();
312   VERIFY(list0301.size() == 0);
313   VERIFY(T::dtorCount() == 11);
314   VERIFY(e == list0301.end());
315 }
316
317 main(int argc, char* argv[])
318 {
319     test01();
320     test02();
321     test03();
322
323     return !test;
324 }
325 // vi:set sw=2 ts=2: