]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 1999-05-07 |
2 | // bkoz | |
3 | ||
83ffe9cd | 4 | // Copyright (C) 1999-2023 Free Software Foundation, Inc. |
b2dad0e3 BK |
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 | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
b2dad0e3 BK |
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 | |
748086b7 JJ |
18 | // with this library; see the file COPYING3. If not see |
19 | // <http://www.gnu.org/licenses/>. | |
b2dad0e3 BK |
20 | |
21 | // 23.2.4.2 vector capacity | |
22 | ||
23 | #include <vector> | |
48d1c3c5 | 24 | #include <stdexcept> |
162c7cd9 | 25 | #include <testsuite_allocator.h> |
fe413112 | 26 | #include <testsuite_hooks.h> |
b2dad0e3 | 27 | |
aecf642c | 28 | using __gnu_test::copy_tracker; |
9f9900db BK |
29 | using __gnu_test::tracker_allocator_counter; |
30 | using __gnu_test::tracker_allocator; | |
aecf642c BK |
31 | using __gnu_test::copy_constructor; |
32 | using __gnu_test::assignment_operator; | |
33 | using __gnu_test::destructor; | |
b2dad0e3 | 34 | |
162c7cd9 SW |
35 | // Verifies basic functionality of reserve() with forced reallocation. |
36 | void | |
37 | test_reserve() | |
38 | { | |
8d59b230 | 39 | typedef copy_tracker T; |
9f9900db | 40 | typedef std::vector<T, tracker_allocator<T> > X; |
162c7cd9 | 41 | |
9f9900db | 42 | tracker_allocator_counter::reset(); |
162c7cd9 SW |
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()); | |
8d59b230 BK |
56 | VERIFY(copy_constructor::count() <= old_size); |
57 | VERIFY(destructor::count() <= old_size); | |
162c7cd9 SW |
58 | } |
59 | // check for memory leaks | |
9f9900db | 60 | VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count()); |
162c7cd9 SW |
61 | } |
62 | ||
63 | // Verifies that reserve() with reallocation offers the strong | |
64 | // exception guarantee. | |
65 | void | |
66 | test_reserve_exception_guarantee() | |
67 | { | |
8d59b230 | 68 | typedef copy_tracker T; |
9f9900db | 69 | typedef std::vector<T, tracker_allocator<T> > X; |
162c7cd9 | 70 | |
9f9900db | 71 | tracker_allocator_counter::reset(); |
162c7cd9 SW |
72 | { |
73 | X a(7); | |
11f10e6b | 74 | const X::size_type old_size __attribute__((unused)) = a.size(); |
162c7cd9 SW |
75 | const X::size_type old_capacity = a.capacity(); |
76 | const X::size_type new_capacity = old_capacity + 10; | |
77 | T::reset(); | |
8d59b230 | 78 | copy_constructor::throw_on(3); |
162c7cd9 SW |
79 | |
80 | try | |
81 | { | |
82 | a.reserve(new_capacity); | |
11f10e6b | 83 | VERIFY(false); |
162c7cd9 SW |
84 | } |
85 | catch (...) | |
86 | { | |
87 | } | |
88 | ||
89 | VERIFY(old_capacity == a.capacity()); | |
8d59b230 | 90 | VERIFY(copy_constructor::count() == destructor::count()+1); |
162c7cd9 | 91 | } |
9f9900db | 92 | VERIFY(tracker_allocator_counter::get_allocation_count() == tracker_allocator_counter::get_deallocation_count()); |
162c7cd9 SW |
93 | } |
94 | ||
b2dad0e3 BK |
95 | int main() |
96 | { | |
162c7cd9 SW |
97 | test_reserve(); |
98 | test_reserve_exception_guarantee(); | |
b2dad0e3 BK |
99 | return 0; |
100 | } |