]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/vector/capacity/2.cc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[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, 2009 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 bool test __attribute__((unused)) = true;
40 typedef copy_tracker T;
41 typedef std::vector<T, tracker_allocator<T> > X;
42
43 tracker_allocator_counter::reset();
44 {
45 X a(3);
46 const X::size_type old_size = a.size();
47 const X::size_type old_capacity = a.capacity();
48 const X::size_type new_capacity = old_capacity + 10;
49 T::reset();
50
51 a.reserve(new_capacity);
52
53 // [23.2.4.1 (2)]
54 VERIFY(new_capacity <= a.capacity());
55 // [23.2.4.1 (3)]
56 VERIFY(old_size == a.size());
57 VERIFY(copy_constructor::count() <= old_size);
58 VERIFY(destructor::count() <= old_size);
59 }
60 // check for memory leaks
61 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
62 }
63
64 // Verifies that reserve() with reallocation offers the strong
65 // exception guarantee.
66 void
67 test_reserve_exception_guarantee()
68 {
69 bool test __attribute__((unused)) = true;
70 typedef copy_tracker T;
71 typedef std::vector<T, tracker_allocator<T> > X;
72
73 tracker_allocator_counter::reset();
74 {
75 X a(7);
76 const X::size_type old_size __attribute__((unused)) = a.size();
77 const X::size_type old_capacity = a.capacity();
78 const X::size_type new_capacity = old_capacity + 10;
79 T::reset();
80 copy_constructor::throw_on(3);
81
82 try
83 {
84 a.reserve(new_capacity);
85 VERIFY(false);
86 }
87 catch (...)
88 {
89 }
90
91 VERIFY(old_capacity == a.capacity());
92 VERIFY(copy_constructor::count() == destructor::count()+1);
93 }
94 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
95 }
96
97 int main()
98 {
99 test_reserve();
100 test_reserve_exception_guarantee();
101 return 0;
102 }