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