4 // Copyright (C) 1999, 2002, 2003 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 2, 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 COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // 23.2.4.2 vector capacity
26 #include <testsuite_allocator.h>
27 #include <testsuite_hooks.h>
29 using __gnu_cxx_test::copy_tracker
;
30 using __gnu_cxx_test::allocation_tracker
;
31 using __gnu_cxx_test::tracker_alloc
;
32 using __gnu_cxx_test::copy_constructor
;
33 using __gnu_cxx_test::assignment_operator
;
34 using __gnu_cxx_test::destructor
;
45 std::vector
< A
<B
> > vec01
;
46 typedef std::vector
< A
<B
> >::size_type size_type
;
48 size_type sz01
= vec01
.capacity();
50 size_type sz02
= vec01
.capacity();
51 VERIFY( sz02
>= sz01
);
53 sz01
= vec01
.size() + 5;
56 VERIFY( sz01
== sz02
);
58 sz01
= vec01
.size() - 5;
61 VERIFY( sz01
== sz02
);
69 std::vector
<int> array
;
70 const std::size_t size
= array
.max_size();
75 catch (const std::length_error
& error
)
79 catch (const std::bad_alloc
& error
)
91 std::vector
<int> array
;
92 const std::size_t size
= array
.max_size() + 1;
97 catch (const std::length_error
& error
)
109 // Verifies basic functionality of reserve() with forced reallocation.
114 typedef copy_tracker T
;
115 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
117 allocation_tracker::resetCounts();
120 const X::size_type old_size
= a
.size();
121 const X::size_type old_capacity
= a
.capacity();
122 const X::size_type new_capacity
= old_capacity
+ 10;
125 a
.reserve(new_capacity
);
128 VERIFY(new_capacity
<= a
.capacity());
130 VERIFY(old_size
== a
.size());
131 VERIFY(copy_constructor::count() <= old_size
);
132 VERIFY(destructor::count() <= old_size
);
134 // check for memory leaks
135 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
138 // Verifies that reserve() with reallocation offers the strong
139 // exception guarantee.
141 test_reserve_exception_guarantee()
144 typedef copy_tracker T
;
145 typedef std::vector
<T
, tracker_alloc
<T
> > X
;
147 allocation_tracker::resetCounts();
150 const X::size_type old_size
= a
.size();
151 const X::size_type old_capacity
= a
.capacity();
152 const X::size_type new_capacity
= old_capacity
+ 10;
154 copy_constructor::throw_on(3);
158 a
.reserve(new_capacity
);
159 VERIFY(("no exception thrown", false));
165 VERIFY(old_capacity
== a
.capacity());
166 VERIFY(copy_constructor::count() == destructor::count()+1);
168 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
176 test_reserve_exception_guarantee();