OSDN Git Service

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