]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/vector/capacity/2.cc
Reshuffle 23_containers testsuite.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / vector / capacity / 2.cc
1 // 1999-05-07
2 // bkoz
3
4 // Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // 23.2.4.2 vector capacity
23
24 #include <vector>
25 #include <stdexcept>
26 #include <testsuite_allocator.h>
27 #include <testsuite_hooks.h>
28
29 using __gnu_test::copy_tracker;
30 using __gnu_test::allocation_tracker;
31 using __gnu_test::tracker_alloc;
32 using __gnu_test::copy_constructor;
33 using __gnu_test::assignment_operator;
34 using __gnu_test::destructor;
35
36 // Verifies basic functionality of reserve() with forced reallocation.
37 void
38 test_reserve()
39 {
40 bool test = true;
41 typedef copy_tracker T;
42 typedef std::vector<T, tracker_alloc<T> > X;
43
44 allocation_tracker::resetCounts();
45 {
46 X a(3);
47 const X::size_type old_size = a.size();
48 const X::size_type old_capacity = a.capacity();
49 const X::size_type new_capacity = old_capacity + 10;
50 T::reset();
51
52 a.reserve(new_capacity);
53
54 // [23.2.4.1 (2)]
55 VERIFY(new_capacity <= a.capacity());
56 // [23.2.4.1 (3)]
57 VERIFY(old_size == a.size());
58 VERIFY(copy_constructor::count() <= old_size);
59 VERIFY(destructor::count() <= old_size);
60 }
61 // check for memory leaks
62 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
63 }
64
65 // Verifies that reserve() with reallocation offers the strong
66 // exception guarantee.
67 void
68 test_reserve_exception_guarantee()
69 {
70 bool test = true;
71 typedef copy_tracker T;
72 typedef std::vector<T, tracker_alloc<T> > X;
73
74 allocation_tracker::resetCounts();
75 {
76 X a(7);
77 const X::size_type old_size = a.size();
78 const X::size_type old_capacity = a.capacity();
79 const X::size_type new_capacity = old_capacity + 10;
80 T::reset();
81 copy_constructor::throw_on(3);
82
83 try
84 {
85 a.reserve(new_capacity);
86 VERIFY(("no exception thrown", false));
87 }
88 catch (...)
89 {
90 }
91
92 VERIFY(old_capacity == a.capacity());
93 VERIFY(copy_constructor::count() == destructor::count()+1);
94 }
95 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
96 }
97
98 int main()
99 {
100 test_reserve();
101 test_reserve_exception_guarantee();
102 return 0;
103 }