OSDN Git Service

571c6ecc1a6ff549e49a46b21eb02d682b8240d4
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 23_containers / vector / bool / modifiers / insert / 31370.cc
1 // Copyright (C) 2007, 2009 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without Pred the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3.  If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // 23.2.5 class vector<bool> [lib.vector.bool]
19
20 // { dg-do run { xfail *-*-darwin8.[0-4].* } }
21
22 #include <vector>
23 #include <stdexcept>
24 #include <testsuite_hooks.h>
25
26 #ifndef _GLIBCXX_DEBUG
27   using std::_S_word_bit;
28 #else
29   using std::_GLIBCXX_STD_D::_S_word_bit;
30 #endif
31
32 inline void
33 check_cap_ge_size(const std::vector<bool>& x)
34 {
35   if (x.capacity() < x.size())
36     throw std::logic_error("");
37 }
38
39 inline void
40 check_cap_eq_maxsize(const std::vector<bool>& x)
41 {
42   if (x.capacity() != x.max_size())
43     throw std::logic_error("");
44 }
45
46 // libstdc++/31370
47 void test01()
48 {
49   bool test __attribute__((unused)) = true;
50   int myexit = 0;
51
52   try
53     {
54       std::vector<bool> x;
55       x.reserve(x.max_size());
56       check_cap_eq_maxsize(x);
57     }
58   catch(std::bad_alloc&)
59     { }
60   catch(std::exception&)
61     { ++myexit; }
62   
63   // When doubling is too big, but smaller is sufficient, the resize
64   // should do smaller and be happy.  It certainly shouldn't throw
65   // other exceptions or crash.
66   try
67     {
68       std::vector<bool> x;
69       x.resize(x.max_size() / 2 + 1, false); 
70       for(int i = 0; i < _S_word_bit; ++i)
71         x.push_back(false);
72       check_cap_ge_size(x);
73     }
74   catch(std::bad_alloc&)
75     { }
76   catch(std::exception&)
77     { ++myexit; }
78   
79   try
80     {
81       std::vector<bool> x;
82       x.resize(x.max_size() / 2 + 1, false); 
83       x.insert(x.end(), _S_word_bit, false);
84       check_cap_ge_size(x);
85     }
86   catch(std::bad_alloc&)
87     { }
88   catch(std::exception&)
89     { ++myexit; }
90   
91   try
92     {
93       std::vector<bool> x;
94       x.resize(x.max_size() / 2 + 1, false); 
95       std::vector<bool> y(_S_word_bit, false);
96       x.insert(x.end(), y.begin(), y.end());
97       check_cap_ge_size(x);
98     }
99   catch(std::bad_alloc&)
100     { }
101   catch(std::exception&)
102     { ++myexit; }
103
104   // These tests are currently only relevant to bool: don't get burned
105   // by the attempt to round up when near the max size.
106   try
107     {
108       std::vector<bool> x;
109       x.resize(x.max_size() - _S_word_bit, false); 
110       for(int i = 0; i < _S_word_bit; ++i)
111         x.push_back(false);
112       check_cap_ge_size(x);
113     }
114   catch(std::bad_alloc&)
115     { }
116   catch(std::exception&)
117     { ++myexit; }
118   
119   try
120     {
121       std::vector<bool> x;
122       x.resize(x.max_size() - _S_word_bit, false); 
123       x.insert(x.end(), _S_word_bit, false);
124       check_cap_ge_size(x);
125     }
126   catch(std::bad_alloc&)
127     { }
128   catch(std::exception&)
129     { ++myexit; }
130
131   try
132     {
133       std::vector<bool> x;
134       x.resize(x.max_size() - _S_word_bit, false); 
135       std::vector<bool> y(_S_word_bit, false);
136       x.insert(x.end(), y.begin(), y.end());
137       check_cap_ge_size(x);
138     }
139   catch(std::bad_alloc&)
140     { }
141   catch(std::exception&)
142     { ++myexit; }
143   
144   // Attempts to put in more than max_size() items should result in a
145   // length error.
146   try
147     {
148       std::vector<bool> x;
149       x.resize(x.max_size() - _S_word_bit, false); 
150       for(int i = 0; i < _S_word_bit + 1; ++i)
151         x.push_back(false);
152       ++myexit;
153     }
154   catch(std::bad_alloc)
155     { }
156   catch(std::length_error)
157     { }
158   catch(std::exception)
159     { ++myexit; }
160   
161   try
162     {
163       std::vector<bool> x;
164       x.resize(x.max_size() - _S_word_bit, false); 
165       x.insert(x.end(), _S_word_bit + 1, false);
166       ++myexit;
167     }
168   catch(std::bad_alloc)
169     { }
170   catch(std::length_error)
171     { }
172   catch(std::exception)
173     { ++myexit; }
174
175   try
176     {
177       std::vector<bool> x;
178       x.resize(x.max_size() - _S_word_bit, false); 
179       std::vector<bool> y(_S_word_bit + 1, false);
180       x.insert(x.end(), y.begin(), y.end());
181       ++myexit;
182     }
183   catch(std::bad_alloc)
184     { }
185   catch(std::length_error)
186     { }
187   catch(std::exception)
188     { ++myexit; }
189
190   VERIFY( !myexit );
191 }
192
193 int main()
194 {
195   test01();
196   return 0;
197 }