Refactor ix86_expand_vecop_qihi2.
[gcc.git] / libstdc++-v3 / testsuite / 23_containers / vector / capacity / 2.cc
bloba515851e00991b52b688ca69293d2c6bd4614404
1 // 1999-05-07
2 // bkoz
4 // Copyright (C) 1999-2025 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.
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
23 #include <vector>
24 #include <stdexcept>
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.
36 void
37 test_reserve()
39 typedef copy_tracker T;
40 typedef std::vector<T, tracker_allocator<T> > X;
42 tracker_allocator_counter::reset();
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();
50 a.reserve(new_capacity);
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);
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.
65 void
66 test_reserve_exception_guarantee()
68 typedef copy_tracker T;
69 typedef std::vector<T, tracker_allocator<T> > X;
71 tracker_allocator_counter::reset();
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);
80 try
82 a.reserve(new_capacity);
83 VERIFY(false);
85 catch (...)
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());
95 int main()
97 test_reserve();
98 test_reserve_exception_guarantee();
99 return 0;