3 // Copyright (C) 1999-2001, 2002, 2003 Free Software Foundation, Inc.
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)
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.
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,
21 // 23.2.4.1 vector constructors, copy, and assignment
25 #include <testsuite_allocator.h>
26 #include <testsuite_hooks.h>
28 using __gnu_cxx_test::copy_tracker
;
29 using __gnu_cxx_test::allocation_tracker
;
30 using __gnu_cxx_test::tracker_alloc
;
31 using __gnu_cxx_test::copy_constructor
;
32 using __gnu_cxx_test::assignment_operator
;
43 std::vector
< A
<B
> > vec01
;
44 std::vector
< A
<B
> > vec02(5);
45 typedef std::vector
< A
<B
> >::size_type size_type
;
55 template class std::vector
<double>;
56 template class std::vector
< A
<B
> >;
63 std::vector
<int> v2 (v1
);
66 // test range constructors and range-fill constructor
71 const int A
[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
72 const int B
[] = {7, 7, 7, 7, 7};
73 const int N
= sizeof(A
) / sizeof(int);
74 const int M
= sizeof(B
) / sizeof(int);
76 std::vector
<int> v3(A
, A
+ N
);
77 VERIFY(std::equal(v3
.begin(), v3
.end(), A
));
79 std::vector
<int> v4(v3
.begin(), v3
.end());
80 VERIFY(std::equal(v4
.begin(), v4
.end(), A
));
82 std::vector
<int> v5(M
, 7);
83 VERIFY(std::equal(v5
.begin(), v5
.end(), B
));
84 VERIFY(std::equal(B
, B
+ M
, v5
.begin()));
95 const char* c_strings
[5] = { "1", "2", "3", "4", "5" };
96 std::vector
<std::string
> strings(c_strings
, c_strings
+ 5);
104 // @fn test_default_ctor_exception_gurantee This test verifies that if
105 // one of the vector's contained objects throws an exception from its
106 // constructor while the vector is being constructed and filled with
107 // default values, all memory is returned to the allocator whence it
110 test_default_ctor_exception_gurantee()
114 typedef copy_tracker T
;
115 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
117 copy_tracker::reset();
118 copy_constructor::throw_on(3);
119 allocation_tracker::resetCounts();
125 VERIFY(("no exception thrown", false));
131 // assert postconditions
132 VERIFY(("memory leak detected:",
133 allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal()));
138 // @fn test_copy_ctor_exception_gurantee This test verifies that if
139 // one of the vector's contained objects throws an exception from its
140 // constructor while the vector is being copy constructed, all memory
141 // is returned to the allocator whence it came.
143 test_copy_ctor_exception_gurantee()
147 typedef copy_tracker T
;
148 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
150 allocation_tracker::resetCounts();
153 copy_tracker::reset();
154 copy_constructor::throw_on(3);
160 VERIFY(("no exception thrown", false));
167 // assert postconditions
168 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
171 copy_tracker::reset();
172 allocation_tracker::resetCounts();
177 // case 1: lhs.size() > rhs.size()
178 // case 2: lhs.size() < rhs.size() < lhs.capacity()
179 // case 3: lhs.capacity() < rhs.size()
182 test_assignment_operator_1()
186 typedef copy_tracker T
;
187 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
191 copy_tracker::reset();
192 allocation_tracker::resetCounts();
195 VERIFY(r
.size() > a
.size());
200 // assert postconditions
202 VERIFY(allocation_tracker::allocationTotal() == 0);
205 copy_tracker::reset();
206 allocation_tracker::resetCounts();
210 test_assignment_operator_2()
214 typedef copy_tracker T
;
215 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
220 copy_tracker::reset();
221 allocation_tracker::resetCounts();
224 VERIFY(r
.size() < a
.size());
225 VERIFY(a
.size() < r
.capacity());
230 // assert postconditions
232 VERIFY(allocation_tracker::allocationTotal() == 0);
235 copy_tracker::reset();
236 allocation_tracker::resetCounts();
240 test_assignment_operator_3()
244 typedef copy_tracker T
;
245 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
247 allocation_tracker::resetCounts();
250 X
a(r
.capacity() + 7);
251 copy_tracker::reset();
254 VERIFY(r
.capacity() < a
.size());
259 // assert postconditions
262 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
265 copy_tracker::reset();
266 allocation_tracker::resetCounts();
270 test_assignment_operator_3_exception_guarantee()
274 typedef copy_tracker T
;
275 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
277 allocation_tracker::resetCounts();
280 X
a(r
.capacity() + 7);
281 copy_tracker::reset();
282 copy_constructor::throw_on(3);
285 VERIFY(r
.capacity() < a
.size());
291 VERIFY(("no exception thrown", false));
298 // assert postconditions
299 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
302 copy_tracker::reset();
303 allocation_tracker::resetCounts();
308 // case 1: [23.2.4.1 (3)] n <= size()
309 // case 2: [23.2.4.1 (3)] size() < n <= capacity()
310 // case 3: [23.2.4.1 (3)] n > capacity()
311 // case 4: [23.2.4.1 (3)] n > capacity(), exception guarantees
312 // case 5: [23.1.1 (9)] fill assign disguised as a range assign
319 typedef copy_tracker T
;
320 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
323 X::size_type old_size
= a
.size();
324 X::size_type new_size
= old_size
- 2;
327 copy_tracker::reset();
328 allocation_tracker::resetCounts();
331 a
.assign(new_size
, t
);
333 // assert postconditions
334 VERIFY(a
.size() == new_size
);
335 VERIFY(allocation_tracker::allocationTotal() == 0);
338 copy_tracker::reset();
339 allocation_tracker::resetCounts();
347 typedef copy_tracker T
;
348 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
352 X::size_type old_size
= a
.size();
353 X::size_type old_capacity
= a
.capacity();
354 X::size_type new_size
= old_size
+ 2;
357 copy_tracker::reset();
358 allocation_tracker::resetCounts();
360 // assert preconditions
361 VERIFY(old_size
< new_size
);
362 VERIFY(new_size
<= old_capacity
);
365 a
.assign(new_size
, t
);
367 // assert postconditions
368 VERIFY(a
.size() == new_size
);
369 VERIFY(allocation_tracker::allocationTotal() == 0);
372 copy_tracker::reset();
373 allocation_tracker::resetCounts();
381 typedef copy_tracker T
;
382 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
384 allocation_tracker::resetCounts();
387 X::size_type old_size
= a
.size();
388 X::size_type old_capacity
= a
.capacity();
389 X::size_type new_size
= old_capacity
+ 4;
392 copy_tracker::reset();
394 // assert preconditions
395 VERIFY(new_size
> old_capacity
);
398 a
.assign(new_size
, t
);
400 // assert postconditions
401 VERIFY(a
.size() == new_size
);
404 VERIFY(allocation_tracker::allocationTotal() > 0);
405 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
408 copy_tracker::reset();
409 allocation_tracker::resetCounts();
413 test_fill_assign_3_exception_guarantee()
417 typedef copy_tracker T
;
418 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
420 allocation_tracker::resetCounts();
423 X::size_type old_size
= a
.size();
424 X::size_type old_capacity
= a
.capacity();
425 X::size_type new_size
= old_capacity
+ 4;
428 copy_tracker::reset();
429 copy_constructor::throw_on(3);
431 // assert preconditions
432 VERIFY(new_size
> old_capacity
);
437 a
.assign(new_size
, t
);
438 VERIFY(("no exception thrown", false));
444 // assert postconditions
445 VERIFY(a
.size() == old_size
);
446 VERIFY(a
.capacity() == old_capacity
);
449 VERIFY(allocation_tracker::allocationTotal() > 0);
450 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
453 copy_tracker::reset();
454 allocation_tracker::resetCounts();
462 typedef copy_tracker T
;
463 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
466 X::size_type old_size
= a
.size();
467 X::size_type new_size
= old_size
- 2;
468 X::size_type new_value
= 117;
470 copy_tracker::reset();
471 allocation_tracker::resetCounts();
474 a
.assign(new_size
, new_value
);
476 // assert postconditions
477 VERIFY(a
.size() == new_size
);
478 VERIFY(allocation_tracker::allocationTotal() == 0);
481 copy_tracker::reset();
482 allocation_tracker::resetCounts();
487 // case 1: [23.2.4.1 (2)] input iterator
488 // case 2: [23.2.4.1 (2)] forward iterator, distance(first, last) <= size()
489 // case 3: [23.2.4.1 (2)]
490 // forward iterator, size() < distance(first, last) <= capacity()
491 // case 4: [23.2.4.1 (2)] forward iterator, distance(first, last) > capacity()
492 // case 5: [23.2.4.1 (2)]
493 // forward iterator, distance(first, last) > capacity(),
494 // exception guarantees
496 test_range_assign_1()
502 test_range_assign_2()
506 typedef copy_tracker T
;
507 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
511 X::size_type old_size
= a
.size();
513 copy_tracker::reset();
514 allocation_tracker::resetCounts();
516 // assert preconditions
517 VERIFY(b
.size() < a
.capacity());
520 a
.assign(b
.begin(), b
.end());
522 // assert postconditions
523 VERIFY(a
.size() == b
.size());
525 VERIFY(allocation_tracker::allocationTotal() == 0);
528 copy_tracker::reset();
529 allocation_tracker::resetCounts();
533 test_range_assign_3()
537 typedef copy_tracker T
;
538 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
541 a
.reserve(a
.size() + 7);
543 X::size_type old_size
= a
.size();
545 copy_tracker::reset();
546 allocation_tracker::resetCounts();
548 // assert preconditions
549 VERIFY(a
.size() < b
.size());
550 VERIFY(b
.size() < a
.capacity());
553 a
.assign(b
.begin(), b
.end());
555 // assert postconditions
556 VERIFY(a
.size() == b
.size());
558 VERIFY(allocation_tracker::allocationTotal() == 0);
561 copy_tracker::reset();
562 allocation_tracker::resetCounts();
566 test_range_assign_4()
570 typedef copy_tracker T
;
571 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
573 allocation_tracker::resetCounts();
576 X
b(a
.capacity() + 7);
577 X::size_type old_size
= a
.size();
579 copy_tracker::reset();
581 // assert preconditions
582 VERIFY(b
.size() > a
.capacity());
585 a
.assign(b
.begin(), b
.end());
587 // assert postconditions
588 VERIFY(a
.size() == b
.size());
591 VERIFY(allocation_tracker::allocationTotal() > 0);
592 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
595 copy_tracker::reset();
596 allocation_tracker::resetCounts();
600 test_range_assign_4_exception_guarantee()
604 typedef copy_tracker T
;
605 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
607 allocation_tracker::resetCounts();
610 X
b(a
.capacity() + 7);
611 X::size_type old_size
= a
.size();
613 copy_tracker::reset();
614 copy_constructor::throw_on(3);
616 // assert preconditions
617 VERIFY(b
.size() > a
.capacity());
622 a
.assign(b
.begin(), b
.end());
623 VERIFY(("no exception thrown", false));
630 // assert postconditions
631 VERIFY(allocation_tracker::allocationTotal() > 0);
632 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
635 copy_tracker::reset();
636 allocation_tracker::resetCounts();
646 test_default_ctor_exception_gurantee();
647 test_copy_ctor_exception_gurantee();
648 test_assignment_operator_1();
649 test_assignment_operator_2();
650 test_assignment_operator_3();
651 test_assignment_operator_3_exception_guarantee();
652 test_fill_assign_1();
653 test_fill_assign_2();
654 test_fill_assign_3();
655 test_fill_assign_3_exception_guarantee();
656 test_fill_assign_4();
657 test_range_assign_1();
658 test_range_assign_2();
659 test_range_assign_3();
660 test_range_assign_4();
661 test_range_assign_4_exception_guarantee();