OSDN Git Service

2003-07-24 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 23_containers / list_modifiers.cc
1 // Copyright (C) 2001, 2003 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 typedef __gnu_test::copy_tracker  T;
25
26 bool test = true;
27
28
29 // This test verifies the following.
30 //
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
36 //
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
41 //
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
49 //
50 void
51 test01()
52 {
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;
57   T::reset();
58
59   list0101.push_back(T(1));     // list should be [1]
60   VERIFY(list0101.size() == 1);
61   VERIFY(T::copyCount() == 1);
62
63   k = list0101.end();
64   --k;
65   VERIFY(k->id() == 1);
66   VERIFY(k->id() == list0101.front().id());
67   VERIFY(k->id() == list0101.back().id());
68
69   list0101.push_front(T(2));    // list should be [2 1]
70   VERIFY(list0101.size() == 2);
71   VERIFY(T::copyCount() == 2);
72   VERIFY(k->id() == 1);
73
74   list0101.push_back(T(3));     // list should be [2 1 3]
75   VERIFY(list0101.size() == 3);
76   VERIFY(T::copyCount() == 3);
77   VERIFY(k->id() == 1);
78
79   try
80   {
81     list0101.push_back(T(4, true));
82     VERIFY(("no exception thrown", false));
83   }
84   catch (...)
85   {
86     VERIFY(list0101.size() == 3);
87     VERIFY(T::copyCount() == 4);
88   }
89
90   i = list0101.begin();
91   VERIFY(i->id() == 2);
92   VERIFY(i->id() == list0101.front().id());
93
94   j = list0101.rbegin();
95   VERIFY(j->id() == 3);
96   VERIFY(j->id() == list0101.back().id());
97
98   ++i;
99   VERIFY(i->id() == 1);
100
101   ++j;
102   VERIFY(j->id() == 1);
103
104   T::reset();
105
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);
111
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);
117 }
118
119 // general single insert/erase + swap
120 void
121 test02()
122 {
123   std::list<T> list0201;
124   T::reset();
125
126   list0201.insert(list0201.begin(), T(1));     // list should be [1]
127   VERIFY(list0201.size() == 1);
128   VERIFY(T::copyCount() == 1);
129
130   list0201.insert(list0201.end(), T(2));     // list should be [1 2]
131   VERIFY(list0201.size() == 2);
132   VERIFY(T::copyCount() == 2);
133
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);
138
139   list0201.insert(i, T(3));     // list should be [1 3 2]
140   VERIFY(list0201.size() == 3);
141   VERIFY(T::copyCount() == 3);
142
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); 
148
149   ++i; // will point to '3'
150   T::reset();
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); 
156
157   std::list<T> list0202;
158   T::reset();
159   VERIFY(list0202.size() == 0);
160   VERIFY(T::copyCount() == 0);
161   VERIFY(T::dtorCount() == 0);
162
163   // member swap
164   list0202.swap(list0201);
165   VERIFY(list0201.size() == 0);
166   VERIFY(list0202.size() == 2);
167   VERIFY(T::copyCount() == 0);
168   VERIFY(T::dtorCount() == 0);
169
170   // specialized swap
171   swap(list0201, list0202);
172   VERIFY(list0201.size() == 2);
173   VERIFY(list0202.size() == 0);
174   VERIFY(T::copyCount() == 0);
175   VERIFY(T::dtorCount() == 0);
176 }
177
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
181 void
182 test03()
183 {
184   std::list<T> list0301;
185   T::reset();
186
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);
191
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();
196
197   // fill insert at end of list
198   T::reset();
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);
205
206   // fill insert in the middle of list
207   ++m;
208   T::reset();
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);
215
216   // single erase
217   T::reset();
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);
224
225   // range erase
226   T::reset();
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);
231
232   // range fill at beginning
233   const int A[] = {321, 322, 333};
234   const int N = sizeof(A) / sizeof(int);
235   T::reset();
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);
241   
242   // range fill at end
243   T::reset();
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);
249   
250   // range fill in middle
251   T::reset();
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);
257
258   T::reset();
259   list0301.clear();
260   VERIFY(list0301.size() == 0);
261   VERIFY(T::dtorCount() == 11);
262   VERIFY(e == list0301.end());
263 }
264
265 int main()
266 {
267     test01();
268     test02();
269     test03();
270
271     return !test;
272 }
273 // vi:set sw=2 ts=2: