| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <vector> |
| 10 | // vector<bool> |
| 11 | |
| 12 | // void reserve(size_type n); |
| 13 | |
| 14 | #include <vector> |
| 15 | #include <cassert> |
| 16 | #include <stdexcept> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | #include "min_allocator.h" |
| 20 | #include "test_allocator.h" |
| 21 | |
| 22 | TEST_CONSTEXPR_CXX20 bool tests() { |
| 23 | { |
| 24 | std::vector<bool> v; |
| 25 | v.reserve(n: 10); |
| 26 | assert(v.capacity() >= 10); |
| 27 | } |
| 28 | { |
| 29 | std::vector<bool> v(100); |
| 30 | assert(v.capacity() >= 100); |
| 31 | v.reserve(n: 50); |
| 32 | assert(v.size() == 100); |
| 33 | assert(v.capacity() >= 100); |
| 34 | v.reserve(n: 150); |
| 35 | assert(v.size() == 100); |
| 36 | assert(v.capacity() >= 150); |
| 37 | } |
| 38 | #if TEST_STD_VER >= 11 |
| 39 | { |
| 40 | std::vector<bool, min_allocator<bool>> v; |
| 41 | v.reserve(10); |
| 42 | assert(v.capacity() >= 10); |
| 43 | } |
| 44 | { |
| 45 | std::vector<bool, explicit_allocator<bool>> v; |
| 46 | v.reserve(10); |
| 47 | assert(v.capacity() >= 10); |
| 48 | } |
| 49 | { |
| 50 | std::vector<bool, min_allocator<bool>> v(100); |
| 51 | assert(v.capacity() >= 100); |
| 52 | v.reserve(50); |
| 53 | assert(v.size() == 100); |
| 54 | assert(v.capacity() >= 100); |
| 55 | v.reserve(150); |
| 56 | assert(v.size() == 100); |
| 57 | assert(v.capacity() >= 150); |
| 58 | } |
| 59 | #endif |
| 60 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 61 | if (!TEST_IS_CONSTANT_EVALUATED) { |
| 62 | std::vector<bool, limited_allocator<bool, 10> > v; |
| 63 | v.reserve(5); |
| 64 | try { |
| 65 | // A typical implementation would allocate chunks of bits. |
| 66 | // In libc++ the chunk has the same size as the machine word. It is |
| 67 | // reasonable to assume that in practice no implementation would use |
| 68 | // 64 kB or larger chunks. |
| 69 | v.reserve(10 * 65536); |
| 70 | assert(false); |
| 71 | } catch (const std::length_error&) { |
| 72 | // no-op |
| 73 | } |
| 74 | assert(v.capacity() >= 5); |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | int main(int, char**) { |
| 82 | tests(); |
| 83 | #if TEST_STD_VER > 17 |
| 84 | static_assert(tests()); |
| 85 | #endif |
| 86 | return 0; |
| 87 | } |
| 88 | |