Add an UNSPEC_PROLOGUE_USE to prevent the link register from being considered dead.
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / vector_capacity.cc
blobb6e347de3099450fd63df7641dc59e0c62f84a8e
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_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;
36 template<typename T>
37 struct A { };
39 struct B { };
41 void test01()
43 // non POD types
44 bool test = true;
45 std::vector< A<B> > vec01;
46 typedef std::vector< A<B> >::size_type size_type;
48 size_type sz01 = vec01.capacity();
49 vec01.reserve(100);
50 size_type sz02 = vec01.capacity();
51 VERIFY( sz02 >= sz01 );
53 sz01 = vec01.size() + 5;
54 vec01.resize(sz01);
55 sz02 = vec01.size();
56 VERIFY( sz01 == sz02 );
58 sz01 = vec01.size() - 5;
59 vec01.resize(sz01);
60 sz02 = vec01.size();
61 VERIFY( sz01 == sz02 );
64 // libstdc++/8230
65 void test02()
67 bool test = true;
69 std::vector<int> array;
70 const std::size_t size = array.max_size();
71 try
73 array.reserve(size);
75 catch (const std::length_error& error)
77 test &= false;
79 catch (const std::bad_alloc& error)
81 test &= true;
83 catch (...)
85 test &= false;
87 VERIFY( test );
91 std::vector<int> array;
92 const std::size_t size = array.max_size() + 1;
93 try
95 array.reserve(size);
97 catch (const std::length_error& error)
99 test &= true;
101 catch (...)
103 test &= false;
105 VERIFY( test );
109 // Verifies basic functionality of reserve() with forced reallocation.
110 void
111 test_reserve()
113 bool test = true;
114 typedef copy_tracker T;
115 typedef std::vector<T, tracker_alloc<T> > X;
117 allocation_tracker::resetCounts();
119 X a(3);
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;
123 T::reset();
125 a.reserve(new_capacity);
127 // [23.2.4.1 (2)]
128 VERIFY(new_capacity <= a.capacity());
129 // [23.2.4.1 (3)]
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.
140 void
141 test_reserve_exception_guarantee()
143 bool test = true;
144 typedef copy_tracker T;
145 typedef std::vector<T, tracker_alloc<T> > X;
147 allocation_tracker::resetCounts();
149 X a(7);
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;
153 T::reset();
154 copy_constructor::throw_on(3);
158 a.reserve(new_capacity);
159 VERIFY(("no exception thrown", false));
161 catch (...)
165 VERIFY(old_capacity == a.capacity());
166 VERIFY(copy_constructor::count() == destructor::count()+1);
168 VERIFY(allocation_tracker::allocationTotal() == allocation_tracker::deallocationTotal());
171 int main()
173 test01();
174 test02();
175 test_reserve();
176 test_reserve_exception_guarantee();
177 return 0;