4 // Copyright (C) 1999-2025 Free Software Foundation, Inc.
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)
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.
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/>.
21 // 23.2.4.2 vector capacity
25 #include <testsuite_allocator.h>
26 #include <testsuite_hooks.h>
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
;
35 // Verifies basic functionality of reserve() with forced reallocation.
39 typedef copy_tracker T
;
40 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
42 tracker_allocator_counter::reset();
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;
50 a
.reserve(new_capacity
);
53 VERIFY(new_capacity
<= a
.capacity());
55 VERIFY(old_size
== a
.size());
56 VERIFY(copy_constructor::count() <= old_size
);
57 VERIFY(destructor::count() <= old_size
);
59 // check for memory leaks
60 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
63 // Verifies that reserve() with reallocation offers the strong
64 // exception guarantee.
66 test_reserve_exception_guarantee()
68 typedef copy_tracker T
;
69 typedef std::vector
<T
, tracker_allocator
<T
> > X
;
71 tracker_allocator_counter::reset();
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;
78 copy_constructor::throw_on(3);
82 a
.reserve(new_capacity
);
89 VERIFY(old_capacity
== a
.capacity());
90 VERIFY(copy_constructor::count() == destructor::count()+1);
92 VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count());
98 test_reserve_exception_guarantee();