| 1 |
// Copyright (C) 2007-2025 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-do run { xfail *-*-darwin8.[0-4].* } } |
| 21 |
// { dg-skip-if "" { powerpc64-*-freebsd* } } |
| 22 |
|
| 23 |
#include <vector> |
| 24 |
#include <stdexcept> |
| 25 |
#include <testsuite_hooks.h> |
| 26 |
|
| 27 |
using std::_S_word_bit; |
| 28 |
|
| 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 |
{ |
| 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; } |
| 58 |
|
| 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; |
| 65 |
x.resize(x.max_size() / 2 + 1, false); |
| 66 |
for(int i = 0; i < _S_word_bit; ++i) |
| 67 |
x.push_back(false); |
| 68 |
check_cap_ge_size(x); |
| 69 |
} |
| 70 |
catch(std::bad_alloc&) |
| 71 |
{ } |
| 72 |
catch(std::exception&) |
| 73 |
{ ++myexit; } |
| 74 |
|
| 75 |
try |
| 76 |
{ |
| 77 |
std::vector<bool> x; |
| 78 |
x.resize(x.max_size() / 2 + 1, false); |
| 79 |
x.insert(x.end(), _S_word_bit, false); |
| 80 |
check_cap_ge_size(x); |
| 81 |
} |
| 82 |
catch(std::bad_alloc&) |
| 83 |
{ } |
| 84 |
catch(std::exception&) |
| 85 |
{ ++myexit; } |
| 86 |
|
| 87 |
try |
| 88 |
{ |
| 89 |
std::vector<bool> x; |
| 90 |
x.resize(x.max_size() / 2 + 1, false); |
| 91 |
std::vector<bool> y(_S_word_bit, false); |
| 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; |
| 105 |
x.resize(x.max_size() - _S_word_bit, false); |
| 106 |
for(int i = 0; i < _S_word_bit; ++i) |
| 107 |
x.push_back(false); |
| 108 |
check_cap_ge_size(x); |
| 109 |
} |
| 110 |
catch(std::bad_alloc&) |
| 111 |
{ } |
| 112 |
catch(std::exception&) |
| 113 |
{ ++myexit; } |
| 114 |
|
| 115 |
try |
| 116 |
{ |
| 117 |
std::vector<bool> x; |
| 118 |
x.resize(x.max_size() - _S_word_bit, false); |
| 119 |
x.insert(x.end(), _S_word_bit, false); |
| 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; |
| 130 |
x.resize(x.max_size() - _S_word_bit, false); |
| 131 |
std::vector<bool> y(_S_word_bit, false); |
| 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; } |
| 139 |
|
| 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; |
| 145 |
x.resize(x.max_size() - _S_word_bit, false); |
| 146 |
for(int i = 0; i < _S_word_bit + 1; ++i) |
| 147 |
x.push_back(false); |
| 148 |
++myexit; |
| 149 |
} |
| 150 |
catch(std::bad_alloc&) |
| 151 |
{ } |
| 152 |
catch(std::length_error&) |
| 153 |
{ } |
| 154 |
catch(std::exception&) |
| 155 |
{ ++myexit; } |
| 156 |
|
| 157 |
try |
| 158 |
{ |
| 159 |
std::vector<bool> x; |
| 160 |
x.resize(x.max_size() - _S_word_bit, false); |
| 161 |
x.insert(x.end(), _S_word_bit + 1, false); |
| 162 |
++myexit; |
| 163 |
} |
| 164 |
catch(std::bad_alloc&) |
| 165 |
{ } |
| 166 |
catch(std::length_error&) |
| 167 |
{ } |
| 168 |
catch(std::exception&) |
| 169 |
{ ++myexit; } |
| 170 |
|
| 171 |
try |
| 172 |
{ |
| 173 |
std::vector<bool> x; |
| 174 |
x.resize(x.max_size() - _S_word_bit, false); |
| 175 |
std::vector<bool> y(_S_word_bit + 1, false); |
| 176 |
x.insert(x.end(), y.begin(), y.end()); |
| 177 |
++myexit; |
| 178 |
} |
| 179 |
catch(std::bad_alloc&) |
| 180 |
{ } |
| 181 |
catch(std::length_error&) |
| 182 |
{ } |
| 183 |
catch(std::exception&) |
| 184 |
{ ++myexit; } |
| 185 |
|
| 186 |
VERIFY( !myexit ); |
| 187 |
} |
| 188 |
|
| 189 |
int main() |
| 190 |
{ |
| 191 |
test01(); |
| 192 |
return 0; |
| 193 |
} |