2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / vector / capacity / 2.cc
blob293ca5945bfb5ec6c4cb10790b0087ba4ab025fd
1 // 1999-05-07
2 // bkoz
4 // Copyright (C) 1999, 2002, 2003 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 2, 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 COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
22 // 23.2.4.2 vector capacity
24 #include <vector>
25 #include <stdexcept>
26 #include <testsuite_allocator.h>
27 #include <testsuite_hooks.h>
29 using __gnu_test::copy_tracker;
30 using __gnu_test::allocation_tracker;
31 using __gnu_test::tracker_alloc;
32 using __gnu_test::copy_constructor;
33 using __gnu_test::assignment_operator;
34 using __gnu_test::destructor;
36 // Verifies basic functionality of reserve() with forced reallocation.
37 void
38 test_reserve()
40 bool test __attribute__((unused)) = true;
41 typedef copy_tracker T;
42 typedef std::vector<T, tracker_alloc<T> > X;
44 allocation_tracker::resetCounts();
46 X a(3);
47 const X::size_type old_size = a.size();
48 const X::size_type old_capacity = a.capacity();
49 const X::size_type new_capacity = old_capacity + 10;
50 T::reset();
52 a.reserve(new_capacity);
54 // [23.2.4.1 (2)]
55 VERIFY(new_capacity <= a.capacity());
56 // [23.2.4.1 (3)]
57 VERIFY(old_size == a.size());
58 VERIFY(copy_constructor::count() <= old_size);
59 VERIFY(destructor::count() <= old_size);
61 // check for memory leaks
62 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
65 // Verifies that reserve() with reallocation offers the strong
66 // exception guarantee.
67 void
68 test_reserve_exception_guarantee()
70 bool test __attribute__((unused)) = true;
71 typedef copy_tracker T;
72 typedef std::vector<T, tracker_alloc<T> > X;
74 allocation_tracker::resetCounts();
76 X a(7);
77 const X::size_type old_size __attribute__((unused)) = a.size();
78 const X::size_type old_capacity = a.capacity();
79 const X::size_type new_capacity = old_capacity + 10;
80 T::reset();
81 copy_constructor::throw_on(3);
83 try
85 a.reserve(new_capacity);
86 VERIFY(false);
88 catch (...)
92 VERIFY(old_capacity == a.capacity());
93 VERIFY(copy_constructor::count() == destructor::count()+1);
95 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
98 int main()
100 test_reserve();
101 test_reserve_exception_guarantee();
102 return 0;