xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/testsuite/23_containers/list_ctor.cc (revision 03a78d155d6fff5698289342b62759a75b20d130)
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 even 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.1 list constructors, copy, and assignment
20 
21 #include <list>
22 #include <testsuite_hooks.h>
23 
24 bool test = true;
25 
26 // A nontrivial type.
27 template<typename T>
28   struct A { };
29 
30 // Another nontrivial type
31 struct B { };
32 
33 // A nontrivial type convertible from an int
34 struct C {
CC35   C(int i) : i_(i) { }
operator ==C36   bool operator==(const C& rhs) { return i_ == rhs.i_; }
37   int i_;
38 };
39 
40 // Default constructor, basic properties
41 //
42 // This test verifies the following.
43 // 23.2.2.1     explicit list(const a& = Allocator())
44 // 23.1 (7)     iterator behaviour of empty containers
45 // 23.2.2       iterator begin()
46 // 23.2.2       iterator end()
47 // 23.2.2       size_type size() const
48 // 23.2.2	existence of required typedefs
49 //
50 void
test01()51 test01()
52 {
53   std::list< A<B> > list0101;
54   VERIFY(list0101.begin() == list0101.end());
55   VERIFY(list0101.size() == 0);
56 
57   // check type definitions -- will fail compile if missing
58   typedef std::list< A<B> >::reference              reference;
59   typedef std::list< A<B> >::const_reference        const_reference;
60   typedef std::list< A<B> >::iterator               iterator;
61   typedef std::list< A<B> >::const_iterator         const_iterator;
62   typedef std::list< A<B> >::size_type              size_type;
63   typedef std::list< A<B> >::difference_type        difference_type;
64   typedef std::list< A<B> >::value_type             value_type;
65   typedef std::list< A<B> >::allocator_type         allocator_type;
66   typedef std::list< A<B> >::pointer                pointer;
67   typedef std::list< A<B> >::const_pointer          const_pointer;
68   typedef std::list< A<B> >::reverse_iterator       reverse_iterator;
69   typedef std::list< A<B> >::const_reverse_iterator const_reverse_iterator;
70 
71   // allocator checks?
72 }
73 
74 // Fill constructor
75 //
76 // This test verifies the following.
77 // 23.2.2.1     explicit list(size_type n, const T& v = T(), const a& = Allocator())
78 // 23.2.2       const_iterator begin() const
79 // 23.2.2       const_iterator end() const
80 // 23.2.2       size_type size() const
81 //
82 void
test02()83 test02()
84 {
85   const int LIST_SIZE = 5;
86   const int INIT_VALUE = 7;
87   int count;
88   std::list<int>::const_iterator i;
89 
90   // nontrivial value_type
91   std::list< A<B> > list0201(LIST_SIZE);
92 
93   // default value
94   std::list<int> list0202(LIST_SIZE);
95   for (i = list0202.begin(), count = 0;
96        i != list0202.end();
97        ++i, ++count)
98     VERIFY(*i == 0);
99   VERIFY(count == LIST_SIZE);
100   VERIFY(list0202.size() == LIST_SIZE);
101 
102   // explicit value
103   std::list<int> list0203(LIST_SIZE, INIT_VALUE);
104   for (i = list0203.begin(), count = 0;
105        i != list0203.end();
106        ++i, ++count)
107     VERIFY(*i == INIT_VALUE);
108   VERIFY(count == LIST_SIZE);
109   VERIFY(list0203.size() == LIST_SIZE);
110 }
111 
112 // Fill constructor disguised as a range constructor
113 void
test02D()114 test02D()
115 {
116   const int LIST_SIZE = 5;
117   const int INIT_VALUE = 7;
118   int count = 0;
119   std::list<C> list0204(LIST_SIZE, INIT_VALUE);
120   std::list<C>::iterator i = list0204.begin();
121   for (; i != list0204.end(); ++i, ++count)
122     VERIFY(*i == INIT_VALUE);
123   VERIFY(count == LIST_SIZE);
124   VERIFY(list0204.size() == LIST_SIZE);
125 }
126 
127 // Range constructor
128 //
129 // This test verifies the following.
130 // 23.2.2.1     template list(InputIterator f, InputIterator l, const Allocator& a = Allocator())
131 // 23.2.2       const_iterator begin() const
132 // 23.2.2       const_iterator end() const
133 // 23.2.2       size_type size() const
134 //
135 void
test03()136 test03()
137 {
138   const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
139   const int N = sizeof(A) / sizeof(int);
140   int count;
141   std::list<int>::const_iterator i;
142 
143   // construct from a dissimilar range
144   std::list<int> list0301(A, A + N);
145   for (i = list0301.begin(), count = 0;
146        i != list0301.end();
147        ++i, ++count)
148     VERIFY(*i == A[count]);
149   VERIFY(count == N);
150   VERIFY(list0301.size() == N);
151 
152   // construct from a similar range
153   std::list<int> list0302(list0301.begin(), list0301.end());
154   for (i = list0302.begin(), count = 0;
155        i != list0302.end();
156        ++i, ++count)
157     VERIFY(*i == A[count]);
158   VERIFY(count == N);
159   VERIFY(list0302.size() == N);
160 }
161 
162 // Copy constructor
163 //
164 // This test verifies the following.
165 // 23.2.2.1     list(const list& x)
166 // 23.2.2       reverse_iterator rbegin()
167 // 23.2.2       reverse_iterator rend()
168 // 23.2.2       size_type size() const
169 //
170 void
test04()171 test04()
172 {
173   const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
174   const int N = sizeof(A) / sizeof(int);
175   int count;
176   std::list<int>::reverse_iterator i;
177   std::list<int> list0401(A, A + N);
178 
179   std::list<int> list0402(list0401);
180   for (i = list0401.rbegin(), count = N - 1;
181        i != list0401.rend();
182        ++i, --count)
183     VERIFY(*i == A[count]);
184   VERIFY(count == -1);
185   VERIFY(list0401.size() == N);
186 }
187 
188 // Range assign
189 //
190 // This test verifies the following.
191 // 23.2.2.1     void assign(InputIterator f, InputIterator l)
192 // 23.2.2       const_iterator begin() const
193 // 23.2.2       const_iterator end() const
194 // 23.2.2       size_type size() const
195 //
196 void
test05()197 test05()
198 {
199   const int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
200   const int B[] = {101, 102, 103, 104, 105};
201   const int N = sizeof(A) / sizeof(int);
202   const int M = sizeof(B) / sizeof(int);
203   int count;
204   std::list<int>::const_iterator i;
205 
206   std::list<int> list0501;
207 
208   // make it bigger
209   list0501.assign(A, A + N);
210   for (i = list0501.begin(), count = 0;
211        i != list0501.end();
212        ++i, ++count)
213     VERIFY(*i == A[count]);
214   VERIFY(count == N);
215   VERIFY(list0501.size() == N);
216 
217   // make it smaller
218   list0501.assign(B, B + M);
219   for (i = list0501.begin(), count = 0;
220        i != list0501.end();
221        ++i, ++count)
222     VERIFY(*i == B[count]);
223   VERIFY(count == M);
224   VERIFY(list0501.size() == M);
225 }
226 
227 // Fill assign
228 //
229 // This test verifies the following.
230 // 23.2.2.1     void assign(size_type n, const T& v)
231 // 23.2.2       const_iterator begin() const
232 // 23.2.2       const_iterator end() const
233 // 23.2.2       size_type size() const
234 //
235 void
test06()236 test06()
237 {
238   const int BIG_LIST_SIZE = 11;
239   const int BIG_INIT_VALUE = 7;
240   const int SMALL_LIST_SIZE = 5;
241   const int SMALL_INIT_VALUE = 17;
242   int count;
243   std::list<int>::const_iterator i;
244 
245   std::list<int> list0601;
246   VERIFY(list0601.size() == 0);
247 
248   // make it bigger
249   list0601.assign(BIG_LIST_SIZE, BIG_INIT_VALUE);
250   for (i = list0601.begin(), count = 0;
251        i != list0601.end();
252        ++i, ++count)
253     VERIFY(*i == BIG_INIT_VALUE);
254   VERIFY(count == BIG_LIST_SIZE);
255   VERIFY(list0601.size() == BIG_LIST_SIZE);
256 
257   // make it shrink
258   list0601.assign(SMALL_LIST_SIZE, SMALL_INIT_VALUE);
259   for (i = list0601.begin(), count = 0;
260        i != list0601.end();
261        ++i, ++count)
262     VERIFY(*i == SMALL_INIT_VALUE);
263   VERIFY(count == SMALL_LIST_SIZE);
264   VERIFY(list0601.size() == SMALL_LIST_SIZE);
265 }
266 
267 // Fill Assignment disguised as a Range Assignment
268 void
test06D()269 test06D()
270 {
271   const int LIST_SIZE = 5;
272   const int INIT_VALUE = 7;
273   int count = 0;
274   std::list<C> list0604;
275   VERIFY(list0604.size() == 0);
276 
277   list0604.assign(LIST_SIZE, INIT_VALUE);
278   std::list<C>::iterator i = list0604.begin();
279   for (; i != list0604.end(); ++i, ++count)
280     VERIFY(*i == INIT_VALUE);
281   VERIFY(count == LIST_SIZE);
282   VERIFY(list0604.size() == LIST_SIZE);
283 }
284 
285 // Assignment operator
286 //
287 // This test verifies the following.
288 // 23.2.2       operator=(const list& x)
289 // 23.2.2       iterator begin()
290 // 23.2.2       iterator end()
291 // 23.2.2       size_type size() const
292 // 23.2.2       bool operator==(const list& x, const list& y)
293 //
294 void
test07()295 test07()
296 {
297   const int A[] = {701, 702, 703, 704, 705};
298   const int N = sizeof(A) / sizeof(int);
299   int count;
300   std::list<int>::iterator i;
301 
302   std::list<int> list0701(A, A + N);
303   VERIFY(list0701.size() == N);
304 
305   std::list<int> list0702;
306   VERIFY(list0702.size() == 0);
307 
308   list0702 = list0701;
309   VERIFY(list0702.size() == N);
310   for (i = list0702.begin(), count = 0;
311        i != list0702.end();
312        ++i, ++count)
313     VERIFY(*i == A[count]);
314   VERIFY(count == N);
315   VERIFY(list0702 == list0701);
316 }
317 
main()318 int main()
319 {
320   test01();
321   test02();
322   test02D();
323   test03();
324   test04();
325   test05();
326   test06();
327   test06D();
328   test07();
329 
330   return !test;
331 }
332 // vi:set sw=2 ts=2:
333