OSDN Git Service

2007-04-02 Matthew Levine <gcc@severeweblint.org>
[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 inline void
28 check_cap_ge_size(const std::vector<bool>& x)
29 {
30   if (x.capacity() < x.size())
31     throw std::logic_error("");
32 }
33
34 inline void
35 check_cap_eq_maxsize(const std::vector<bool>& x)
36 {
37   if (x.capacity() != x.max_size())
38     throw std::logic_error("");
39 }
40
41 // libstdc++/31370
42 void test01()
43 {
44   bool test __attribute__((unused)) = true;
45   int myexit = 0;
46
47   try
48     {
49       std::vector<bool> x;
50       x.reserve(x.max_size());
51       check_cap_eq_maxsize(x);
52     }
53   catch(std::bad_alloc&)
54     { }
55   catch(std::exception&)
56     { ++myexit; }
57   
58   // When doubling is too big, but smaller is sufficient, the resize
59   // should do smaller and be happy.  It certainly shouldn't throw
60   // other exceptions or crash.
61   try
62     {
63       std::vector<bool> x;
64       x.resize(x.max_size() / 2 + 1, false); 
65       for(int i = 0; i < std::_S_word_bit; ++i)
66         x.push_back(false);
67       check_cap_ge_size(x);
68     }
69   catch(std::bad_alloc&)
70     { }
71   catch(std::exception&)
72     { ++myexit; }
73   
74   try
75     {
76       std::vector<bool> x;
77       x.resize(x.max_size() / 2 + 1, false); 
78       x.insert(x.end(), std::_S_word_bit, false);
79       check_cap_ge_size(x);
80     }
81   catch(std::bad_alloc&)
82     { }
83   catch(std::exception&)
84     { ++myexit; }
85   
86   try
87     {
88       std::vector<bool> x;
89       x.resize(x.max_size() / 2 + 1, false); 
90       std::vector<bool> y(std::_S_word_bit, false);
91       x.insert(x.end(), y.begin(), y.end());
92       check_cap_ge_size(x);
93     }
94   catch(std::bad_alloc&)
95     { }
96   catch(std::exception&)
97     { ++myexit; }
98
99   // These tests are currently only relevant to bool: don't get burned
100   // by the attempt to round up when near the max size.
101   try
102     {
103       std::vector<bool> x;
104       x.resize(x.max_size() - std::_S_word_bit, false); 
105       for(int i = 0; i < std::_S_word_bit; ++i)
106         x.push_back(false);
107       check_cap_ge_size(x);
108     }
109   catch(std::bad_alloc&)
110     { }
111   catch(std::exception&)
112     { ++myexit; }
113   
114   try
115     {
116       std::vector<bool> x;
117       x.resize(x.max_size() - std::_S_word_bit, false); 
118       x.insert(x.end(), std::_S_word_bit, false);
119       check_cap_ge_size(x);
120     }
121   catch(std::bad_alloc&)
122     { }
123   catch(std::exception&)
124     { ++myexit; }
125
126   try
127     {
128       std::vector<bool> x;
129       x.resize(x.max_size() - std::_S_word_bit, false); 
130       std::vector<bool> y(std::_S_word_bit, false);
131       x.insert(x.end(), y.begin(), y.end());
132       check_cap_ge_size(x);
133     }
134   catch(std::bad_alloc&)
135     { }
136   catch(std::exception&)
137     { ++myexit; }
138   
139   // Attempts to put in more than max_size() items should result in a
140   // length error.
141   try
142     {
143       std::vector<bool> x;
144       x.resize(x.max_size() - std::_S_word_bit, false); 
145       for(int i = 0; i < std::_S_word_bit + 1; ++i)
146         x.push_back(false);
147       ++myexit;
148     }
149   catch(std::bad_alloc)
150     { }
151   catch(std::length_error)
152     { }
153   catch(std::exception)
154     { ++myexit; }
155   
156   try
157     {
158       std::vector<bool> x;
159       x.resize(x.max_size() - std::_S_word_bit, false); 
160       x.insert(x.end(), std::_S_word_bit + 1, false);
161       ++myexit;
162     }
163   catch(std::bad_alloc)
164     { }
165   catch(std::length_error)
166     { }
167   catch(std::exception)
168     { ++myexit; }
169
170   try
171     {
172       std::vector<bool> x;
173       x.resize(x.max_size() - std::_S_word_bit, false); 
174       std::vector<bool> y(std::_S_word_bit + 1, false);
175       x.insert(x.end(), y.begin(), y.end());
176       ++myexit;
177     }
178   catch(std::bad_alloc)
179     { }
180   catch(std::length_error)
181     { }
182   catch(std::exception)
183     { ++myexit; }
184
185   VERIFY( !myexit );
186 }
187
188 int main()
189 {
190   test01();
191   return 0;
192 }