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