OSDN Git Service

2003-09-23 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 23_containers / vector / cons / 4.cc
1 // 1999-06-29 bkoz
2
3 // Copyright (C) 1999-2001, 2002, 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // 23.2.4.1 vector constructors, copy, and assignment
22
23 #include <vector>
24 #include <string>
25 #include <testsuite_allocator.h>
26 #include <testsuite_hooks.h>
27
28 using __gnu_test::copy_tracker;
29 using __gnu_test::allocation_tracker;
30 using __gnu_test::tracker_alloc;
31 using __gnu_test::copy_constructor;
32 using __gnu_test::assignment_operator;
33
34 // @fn test_default_ctor_exception_gurantee This test verifies that if
35 // one of the vector's contained objects throws an exception from its
36 // constructor while the vector is being constructed and filled with
37 // default values, all memory is returned to the allocator whence it
38 // came.
39 void
40 test_default_ctor_exception_gurantee()
41 {
42   // setup
43   bool test __attribute__((unused)) = true;
44   typedef copy_tracker T;
45   typedef std::vector<T, tracker_alloc<T> > X;
46
47   copy_tracker::reset();
48   copy_constructor::throw_on(3);
49   allocation_tracker::resetCounts();
50
51   // run test
52   try
53   {
54     X a(7);
55     VERIFY(false);
56   }
57   catch (...)
58   {
59   }
60
61   // assert postconditions
62   VERIFY( allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal() );
63
64   // teardown
65 }
66
67 // @fn test_copy_ctor_exception_gurantee This test verifies that if
68 // one of the vector's contained objects throws an exception from its
69 // constructor while the vector is being copy constructed, all memory
70 // is returned to the allocator whence it came.
71 void
72 test_copy_ctor_exception_gurantee()
73 {
74   // setup
75   bool test __attribute__((unused)) = true;
76   typedef copy_tracker T;
77   typedef std::vector<T, tracker_alloc<T> > X;
78
79   allocation_tracker::resetCounts();
80   {
81     X a(7);
82     copy_tracker::reset();
83     copy_constructor::throw_on(3);
84
85     // run test
86     try
87     {
88       X u(a);
89       VERIFY(false);
90     }
91     catch (...)
92     {
93     }
94   }
95
96   // assert postconditions
97   VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
98
99   // teardown
100   copy_tracker::reset();
101   allocation_tracker::resetCounts();
102 }
103
104 // operator=()
105 //
106 // case 1: lhs.size() > rhs.size()
107 // case 2: lhs.size() < rhs.size() < lhs.capacity()
108 // case 3: lhs.capacity() < rhs.size()
109 //
110 void
111 test_assignment_operator_1()
112 {
113   // setup
114   bool test __attribute__((unused)) = true;
115   typedef copy_tracker T;
116   typedef std::vector<T, tracker_alloc<T> > X;
117
118   X r(9);
119   X a(r.size() - 2);
120   copy_tracker::reset();
121   allocation_tracker::resetCounts();
122
123   // preconditions
124   VERIFY(r.size() > a.size());
125
126   // run test
127   r = a;
128
129   // assert postconditions
130   VERIFY(r == a);
131   VERIFY(allocation_tracker::allocationTotal() == 0);
132
133   // teardown
134   copy_tracker::reset();
135   allocation_tracker::resetCounts();
136 }
137
138 void
139 test_assignment_operator_2()
140 {
141   // setup
142   bool test __attribute__((unused)) = true;
143   typedef copy_tracker T;
144   typedef std::vector<T, tracker_alloc<T> > X;
145
146   X r(1);
147   r.reserve(17);
148   X a(r.size() + 7);
149   copy_tracker::reset();
150   allocation_tracker::resetCounts();
151
152   // preconditions
153   VERIFY(r.size() < a.size());
154   VERIFY(a.size() < r.capacity());
155
156   // run test
157   r = a;
158
159   // assert postconditions
160   VERIFY(r == a);
161   VERIFY(allocation_tracker::allocationTotal() == 0);
162
163   // teardown
164   copy_tracker::reset();
165   allocation_tracker::resetCounts();
166 }
167
168 void
169 test_assignment_operator_3()
170 {
171   // setup
172   bool test __attribute__((unused)) = true;
173   typedef copy_tracker T;
174   typedef std::vector<T, tracker_alloc<T> > X;
175
176   allocation_tracker::resetCounts();
177   {
178     X r(1);
179     X a(r.capacity() + 7);
180     copy_tracker::reset();
181
182     // preconditions
183     VERIFY(r.capacity() < a.size());
184
185     // run test
186     r = a;
187
188     // assert postconditions
189     VERIFY(r == a);
190   }
191   VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
192
193   // teardown
194   copy_tracker::reset();
195   allocation_tracker::resetCounts();
196 }
197
198 void
199 test_assignment_operator_3_exception_guarantee()
200 {
201   // setup
202   bool test __attribute__((unused)) = true;
203   typedef copy_tracker T;
204   typedef std::vector<T, tracker_alloc<T> > X;
205
206   allocation_tracker::resetCounts();
207   {
208     X r(1);
209     X a(r.capacity() + 7);
210     copy_tracker::reset();
211     copy_constructor::throw_on(3);
212
213     // preconditions
214     VERIFY(r.capacity() < a.size());
215
216     // run test
217     try
218     {
219       r = a;
220       VERIFY(false);
221     }
222     catch (...)
223     {
224     }
225   }
226
227   // assert postconditions
228   VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
229
230   // teardown
231   copy_tracker::reset();
232   allocation_tracker::resetCounts();
233 }
234
235 // fill assign()
236 //
237 // case 1: [23.2.4.1 (3)] n <= size()
238 // case 2: [23.2.4.1 (3)] size() < n <= capacity()
239 // case 3: [23.2.4.1 (3)] n > capacity()
240 // case 4: [23.2.4.1 (3)] n > capacity(), exception guarantees
241 // case 5: [23.1.1 (9)] fill assign disguised as a range assign
242 //
243 void
244 test_fill_assign_1()
245 {
246   // setup
247   bool test __attribute__((unused)) = true;
248   typedef copy_tracker T;
249   typedef std::vector<T, tracker_alloc<T> > X;
250
251   X a(7);
252   X::size_type old_size = a.size();
253   X::size_type new_size = old_size - 2;
254   const T t;
255
256   copy_tracker::reset();
257   allocation_tracker::resetCounts();
258
259   // run test
260   a.assign(new_size, t);
261
262   // assert postconditions
263   VERIFY(a.size() == new_size);
264   VERIFY(allocation_tracker::allocationTotal() == 0);
265
266   // teardown
267   copy_tracker::reset();
268   allocation_tracker::resetCounts();
269 }
270
271 void
272 test_fill_assign_2()
273 {
274   // setup
275   bool test __attribute__((unused)) = true;
276   typedef copy_tracker T;
277   typedef std::vector<T, tracker_alloc<T> > X;
278
279   X a(7);
280   a.reserve(11);
281   X::size_type old_size     = a.size();
282   X::size_type old_capacity = a.capacity();
283   X::size_type new_size     = old_size + 2;
284   const T t;
285
286   copy_tracker::reset();
287   allocation_tracker::resetCounts();
288
289   // assert preconditions
290   VERIFY(old_size < new_size);
291   VERIFY(new_size <= old_capacity);
292
293   // run test
294   a.assign(new_size, t);
295
296   // assert postconditions
297   VERIFY(a.size() == new_size);
298   VERIFY(allocation_tracker::allocationTotal() == 0);
299
300   // teardown
301   copy_tracker::reset();
302   allocation_tracker::resetCounts();
303 }
304
305 void
306 test_fill_assign_3()
307 {
308   // setup
309   bool test __attribute__((unused)) = true;
310   typedef copy_tracker T;
311   typedef std::vector<T, tracker_alloc<T> > X;
312
313   allocation_tracker::resetCounts();
314   {
315     X a(7);
316     X::size_type old_capacity = a.capacity();
317     X::size_type new_size     = old_capacity + 4;
318     const T t;
319
320     copy_tracker::reset();
321
322     // assert preconditions
323     VERIFY(new_size > old_capacity);
324
325     // run test
326     a.assign(new_size, t);
327
328     // assert postconditions
329     VERIFY(a.size() == new_size);
330   }
331
332   VERIFY(allocation_tracker::allocationTotal() > 0);
333   VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
334
335   // teardown
336   copy_tracker::reset();
337   allocation_tracker::resetCounts();
338 }
339
340 void
341 test_fill_assign_3_exception_guarantee()
342 {
343   // setup
344   bool test __attribute__((unused)) = true;
345   typedef copy_tracker T;
346   typedef std::vector<T, tracker_alloc<T> > X;
347
348   allocation_tracker::resetCounts();
349   {
350     X a(7);
351     X::size_type old_size     = a.size();
352     X::size_type old_capacity = a.capacity();
353     X::size_type new_size     = old_capacity + 4;
354     const T t;
355
356     copy_tracker::reset();
357     copy_constructor::throw_on(3);
358
359     // assert preconditions
360     VERIFY(new_size > old_capacity);
361
362     // run test
363     try
364     {
365       a.assign(new_size, t);
366       VERIFY(false);
367     }
368     catch (...)
369     {
370     }
371
372     // assert postconditions
373     VERIFY(a.size() == old_size);
374     VERIFY(a.capacity() == old_capacity);
375   }
376
377   VERIFY(allocation_tracker::allocationTotal() > 0);
378   VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
379
380   // teardown
381   copy_tracker::reset();
382   allocation_tracker::resetCounts();
383 }
384
385 void
386 test_fill_assign_4()
387 {
388   // setup
389   bool test __attribute__((unused)) = true;
390   typedef copy_tracker T;
391   typedef std::vector<T, tracker_alloc<T> > X;
392
393   X a(7);
394   X::size_type old_size  = a.size();
395   X::size_type new_size  = old_size - 2;
396   X::size_type new_value = 117;
397
398   copy_tracker::reset();
399   allocation_tracker::resetCounts();
400
401   // run test
402   a.assign(new_size, new_value);
403
404   // assert postconditions
405   VERIFY(a.size() == new_size);
406   VERIFY(allocation_tracker::allocationTotal() == 0);
407
408   // teardown
409   copy_tracker::reset();
410   allocation_tracker::resetCounts();
411 }
412
413 // range assign()
414 //
415 // case 1: [23.2.4.1 (2)] input iterator
416 // case 2: [23.2.4.1 (2)] forward iterator, distance(first, last) <= size()
417 // case 3: [23.2.4.1 (2)] 
418 //         forward iterator, size() < distance(first, last) <= capacity()
419 // case 4: [23.2.4.1 (2)] forward iterator, distance(first, last) > capacity()
420 // case 5: [23.2.4.1 (2)] 
421 //         forward iterator, distance(first, last) > capacity(), 
422 //         exception guarantees
423 void
424 test_range_assign_1()
425 {
426   // @TODO
427 }
428
429 void
430 test_range_assign_2()
431 {
432   // setup
433   bool test __attribute__((unused)) = true;
434   typedef copy_tracker T;
435   typedef std::vector<T, tracker_alloc<T> > X;
436
437   X a(7);
438   X b(3);
439
440   copy_tracker::reset();
441   allocation_tracker::resetCounts();
442
443   // assert preconditions
444   VERIFY(b.size() < a.capacity());
445
446   // run test
447   a.assign(b.begin(), b.end());
448
449   // assert postconditions
450   VERIFY(a.size() == b.size());
451   VERIFY(a == b);
452   VERIFY(allocation_tracker::allocationTotal() == 0);
453
454   // teardown
455   copy_tracker::reset();
456   allocation_tracker::resetCounts();
457 }
458
459 void
460 test_range_assign_3()
461 {
462   // setup
463   bool test __attribute__((unused)) = true;
464   typedef copy_tracker T;
465   typedef std::vector<T, tracker_alloc<T> > X;
466
467   X a(7);
468   a.reserve(a.size() + 7);
469   X b(a.size() + 3);
470
471   copy_tracker::reset();
472   allocation_tracker::resetCounts();
473
474   // assert preconditions
475   VERIFY(a.size() < b.size());
476   VERIFY(b.size() < a.capacity());
477
478   // run test
479   a.assign(b.begin(), b.end());
480
481   // assert postconditions
482   VERIFY(a.size() == b.size());
483   VERIFY(a == b);
484   VERIFY(allocation_tracker::allocationTotal() == 0);
485
486   // teardown
487   copy_tracker::reset();
488   allocation_tracker::resetCounts();
489 }
490
491 void
492 test_range_assign_4()
493 {
494   // setup
495   bool test __attribute__((unused)) = true;
496   typedef copy_tracker T;
497   typedef std::vector<T, tracker_alloc<T> > X;
498
499   allocation_tracker::resetCounts();
500   {
501     X a(7);
502     X b(a.capacity() + 7);
503
504     copy_tracker::reset();
505
506     // assert preconditions
507     VERIFY(b.size() > a.capacity());
508
509     // run test
510     a.assign(b.begin(), b.end());
511
512     // assert postconditions
513     VERIFY(a.size() == b.size());
514     VERIFY(a == b);
515   }
516   VERIFY(allocation_tracker::allocationTotal() > 0);
517   VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
518
519   // teardown
520   copy_tracker::reset();
521   allocation_tracker::resetCounts();
522 }
523
524 void
525 test_range_assign_4_exception_guarantee()
526 {
527   // setup
528   bool test __attribute__((unused)) = true;
529   typedef copy_tracker T;
530   typedef std::vector<T, tracker_alloc<T> > X;
531
532   allocation_tracker::resetCounts();
533   {
534     X a(7);
535     X b(a.capacity() + 7);
536
537     copy_tracker::reset();
538     copy_constructor::throw_on(3);
539
540     // assert preconditions
541     VERIFY(b.size() > a.capacity());
542
543     // run test
544     try
545     {
546       a.assign(b.begin(), b.end());
547       VERIFY(false);
548     }
549     catch (...)
550     {
551     }
552   }
553
554   // assert postconditions
555   VERIFY(allocation_tracker::allocationTotal() > 0);
556   VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
557
558   // teardown
559   copy_tracker::reset();
560   allocation_tracker::resetCounts();
561 }
562
563
564 int main()
565 {
566   test_default_ctor_exception_gurantee();
567   test_copy_ctor_exception_gurantee();
568   test_assignment_operator_1();
569   test_assignment_operator_2();
570   test_assignment_operator_3();
571   test_assignment_operator_3_exception_guarantee();
572   test_fill_assign_1();
573   test_fill_assign_2();
574   test_fill_assign_3();
575   test_fill_assign_3_exception_guarantee();
576   test_fill_assign_4();
577   test_range_assign_1();
578   test_range_assign_2();
579   test_range_assign_3();
580   test_range_assign_4();
581   test_range_assign_4_exception_guarantee();
582
583   return 0;
584 }