]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/vector/capacity/2.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / vector / capacity / 2.cc
1 // 1999-05-07
2 // bkoz
3
4 // Copyright (C) 1999-2020 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 3, 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 COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 // 23.2.4.2 vector capacity
22
23 #include <vector>
24 #include <stdexcept>
25 #include <testsuite_allocator.h>
26 #include <testsuite_hooks.h>
27
28 using __gnu_test::copy_tracker;
29 using __gnu_test::tracker_allocator_counter;
30 using __gnu_test::tracker_allocator;
31 using __gnu_test::copy_constructor;
32 using __gnu_test::assignment_operator;
33 using __gnu_test::destructor;
34
35 // Verifies basic functionality of reserve() with forced reallocation.
36 void
37 test_reserve()
38 {
39 typedef copy_tracker T;
40 typedef std::vector<T, tracker_allocator<T> > X;
41
42 tracker_allocator_counter::reset();
43 {
44 X a(3);
45 const X::size_type old_size = a.size();
46 const X::size_type old_capacity = a.capacity();
47 const X::size_type new_capacity = old_capacity + 10;
48 T::reset();
49
50 a.reserve(new_capacity);
51
52 // [23.2.4.1 (2)]
53 VERIFY(new_capacity <= a.capacity());
54 // [23.2.4.1 (3)]
55 VERIFY(old_size == a.size());
56 VERIFY(copy_constructor::count() <= old_size);
57 VERIFY(destructor::count() <= old_size);
58 }
59 // check for memory leaks
60 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
61 }
62
63 // Verifies that reserve() with reallocation offers the strong
64 // exception guarantee.
65 void
66 test_reserve_exception_guarantee()
67 {
68 typedef copy_tracker T;
69 typedef std::vector<T, tracker_allocator<T> > X;
70
71 tracker_allocator_counter::reset();
72 {
73 X a(7);
74 const X::size_type old_size __attribute__((unused)) = a.size();
75 const X::size_type old_capacity = a.capacity();
76 const X::size_type new_capacity = old_capacity + 10;
77 T::reset();
78 copy_constructor::throw_on(3);
79
80 try
81 {
82 a.reserve(new_capacity);
83 VERIFY(false);
84 }
85 catch (...)
86 {
87 }
88
89 VERIFY(old_capacity == a.capacity());
90 VERIFY(copy_constructor::count() == destructor::count()+1);
91 }
92 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
93 }
94
95 int main()
96 {
97 test_reserve();
98 test_reserve_exception_guarantee();
99 return 0;
100 }