OSDN Git Service

fbbe7b4f586098c5ede1aad1943a7a77d06324b4
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 23_containers / vector / bool / modifiers / insert / 31370.cc
1 // Copyright (C) 2007 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 2, 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 COPYING.  If not, write to the Free
16 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 // USA.
18
19 // 23.2.5 class vector<bool> [lib.vector.bool]
20
21 // { dg-do run { xfail *-*-darwin8.[0-4].* } }
22
23 #include <vector>
24 #include <stdexcept>
25 #include <testsuite_hooks.h>
26
27 #ifndef _GLIBCXX_DEBUG
28   using std::_S_word_bit;
29 #else
30   using std::_GLIBCXX_STD_D::_S_word_bit;
31 #endif
32
33 inline void
34 check_cap_ge_size(const std::vector<bool>& x)
35 {
36   if (x.capacity() < x.size())
37     throw std::logic_error("");
38 }
39
40 inline void
41 check_cap_eq_maxsize(const std::vector<bool>& x)
42 {
43   if (x.capacity() != x.max_size())
44     throw std::logic_error("");
45 }
46
47 // libstdc++/31370
48 void test01()
49 {
50   bool test __attribute__((unused)) = true;
51   int myexit = 0;
52
53   try
54     {
55       std::vector<bool> x;
56       x.reserve(x.max_size());
57       check_cap_eq_maxsize(x);
58     }
59   catch(std::bad_alloc&)
60     { }
61   catch(std::exception&)
62     { ++myexit; }
63   
64   // When doubling is too big, but smaller is sufficient, the resize
65   // should do smaller and be happy.  It certainly shouldn't throw
66   // other exceptions or crash.
67   try
68     {
69       std::vector<bool> x;
70       x.resize(x.max_size() / 2 + 1, false); 
71       for(int i = 0; i < _S_word_bit; ++i)
72         x.push_back(false);
73       check_cap_ge_size(x);
74     }
75   catch(std::bad_alloc&)
76     { }
77   catch(std::exception&)
78     { ++myexit; }
79   
80   try
81     {
82       std::vector<bool> x;
83       x.resize(x.max_size() / 2 + 1, false); 
84       x.insert(x.end(), _S_word_bit, false);
85       check_cap_ge_size(x);
86     }
87   catch(std::bad_alloc&)
88     { }
89   catch(std::exception&)
90     { ++myexit; }
91   
92   try
93     {
94       std::vector<bool> x;
95       x.resize(x.max_size() / 2 + 1, false); 
96       std::vector<bool> y(_S_word_bit, false);
97       x.insert(x.end(), y.begin(), y.end());
98       check_cap_ge_size(x);
99     }
100   catch(std::bad_alloc&)
101     { }
102   catch(std::exception&)
103     { ++myexit; }
104
105   // These tests are currently only relevant to bool: don't get burned
106   // by the attempt to round up when near the max size.
107   try
108     {
109       std::vector<bool> x;
110       x.resize(x.max_size() - _S_word_bit, false); 
111       for(int i = 0; i < _S_word_bit; ++i)
112         x.push_back(false);
113       check_cap_ge_size(x);
114     }
115   catch(std::bad_alloc&)
116     { }
117   catch(std::exception&)
118     { ++myexit; }
119   
120   try
121     {
122       std::vector<bool> x;
123       x.resize(x.max_size() - _S_word_bit, false); 
124       x.insert(x.end(), _S_word_bit, false);
125       check_cap_ge_size(x);
126     }
127   catch(std::bad_alloc&)
128     { }
129   catch(std::exception&)
130     { ++myexit; }
131
132   try
133     {
134       std::vector<bool> x;
135       x.resize(x.max_size() - _S_word_bit, false); 
136       std::vector<bool> y(_S_word_bit, false);
137       x.insert(x.end(), y.begin(), y.end());
138       check_cap_ge_size(x);
139     }
140   catch(std::bad_alloc&)
141     { }
142   catch(std::exception&)
143     { ++myexit; }
144   
145   // Attempts to put in more than max_size() items should result in a
146   // length error.
147   try
148     {
149       std::vector<bool> x;
150       x.resize(x.max_size() - _S_word_bit, false); 
151       for(int i = 0; i < _S_word_bit + 1; ++i)
152         x.push_back(false);
153       ++myexit;
154     }
155   catch(std::bad_alloc)
156     { }
157   catch(std::length_error)
158     { }
159   catch(std::exception)
160     { ++myexit; }
161   
162   try
163     {
164       std::vector<bool> x;
165       x.resize(x.max_size() - _S_word_bit, false); 
166       x.insert(x.end(), _S_word_bit + 1, false);
167       ++myexit;
168     }
169   catch(std::bad_alloc)
170     { }
171   catch(std::length_error)
172     { }
173   catch(std::exception)
174     { ++myexit; }
175
176   try
177     {
178       std::vector<bool> x;
179       x.resize(x.max_size() - _S_word_bit, false); 
180       std::vector<bool> y(_S_word_bit + 1, false);
181       x.insert(x.end(), y.begin(), y.end());
182       ++myexit;
183     }
184   catch(std::bad_alloc)
185     { }
186   catch(std::length_error)
187     { }
188   catch(std::exception)
189     { ++myexit; }
190
191   VERIFY( !myexit );
192 }
193
194 int main()
195 {
196   test01();
197   return 0;
198 }