| 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 shrink_to_fit(); |
| 13 | |
| 14 | // XFAIL: FROZEN-CXX03-HEADERS-FIXME |
| 15 | |
| 16 | #include <cassert> |
| 17 | #include <climits> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "increasing_allocator.h" |
| 21 | #include "min_allocator.h" |
| 22 | #include "test_macros.h" |
| 23 | |
| 24 | TEST_CONSTEXPR_CXX20 bool tests() { |
| 25 | { |
| 26 | using C = std::vector<bool>; |
| 27 | C v(100); |
| 28 | v.push_back(x: 1); |
| 29 | C::size_type before_cap = v.capacity(); |
| 30 | v.clear(); |
| 31 | v.shrink_to_fit(); |
| 32 | assert(v.capacity() <= before_cap); |
| 33 | LIBCPP_ASSERT(v.capacity() == 0); // libc++ honors the shrink_to_fit request as a QOI matter |
| 34 | assert(v.size() == 0); |
| 35 | } |
| 36 | { |
| 37 | using C = std::vector<bool, min_allocator<bool> >; |
| 38 | C v(100); |
| 39 | v.push_back(1); |
| 40 | C::size_type before_cap = v.capacity(); |
| 41 | v.shrink_to_fit(); |
| 42 | assert(v.capacity() >= 101); |
| 43 | assert(v.capacity() <= before_cap); |
| 44 | assert(v.size() == 101); |
| 45 | v.erase(v.begin() + 1, v.end()); |
| 46 | v.shrink_to_fit(); |
| 47 | assert(v.capacity() <= before_cap); |
| 48 | LIBCPP_ASSERT(v.capacity() == C(1).capacity()); // libc++ honors the shrink_to_fit request as a QOI matter. |
| 49 | assert(v.size() == 1); |
| 50 | } |
| 51 | |
| 52 | #if defined(_LIBCPP_VERSION) |
| 53 | { |
| 54 | using C = std::vector<bool>; |
| 55 | unsigned bits_per_word = static_cast<unsigned>(sizeof(C::__storage_type) * CHAR_BIT); |
| 56 | C v(bits_per_word); |
| 57 | v.push_back(1); |
| 58 | assert(v.capacity() == bits_per_word * 2); |
| 59 | assert(v.size() == bits_per_word + 1); |
| 60 | v.pop_back(); |
| 61 | v.shrink_to_fit(); |
| 62 | assert(v.capacity() == bits_per_word); |
| 63 | assert(v.size() == bits_per_word); |
| 64 | } |
| 65 | { |
| 66 | using C = std::vector<bool>; |
| 67 | unsigned bits_per_word = static_cast<unsigned>(sizeof(C::__storage_type) * CHAR_BIT); |
| 68 | C v; |
| 69 | v.reserve(bits_per_word * 2); |
| 70 | v.push_back(1); |
| 71 | assert(v.capacity() == bits_per_word * 2); |
| 72 | assert(v.size() == 1); |
| 73 | v.shrink_to_fit(); |
| 74 | assert(v.capacity() == bits_per_word); |
| 75 | assert(v.size() == 1); |
| 76 | } |
| 77 | #endif |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | #if TEST_STD_VER >= 23 |
| 83 | // https://github.com/llvm/llvm-project/issues/95161 |
| 84 | constexpr bool test_increasing_allocator() { |
| 85 | std::vector<bool, increasing_allocator<bool>> v; |
| 86 | v.push_back(1); |
| 87 | std::size_t capacity = v.capacity(); |
| 88 | v.shrink_to_fit(); |
| 89 | assert(v.capacity() <= capacity); |
| 90 | assert(v.size() == 1); |
| 91 | |
| 92 | return true; |
| 93 | } |
| 94 | #endif // TEST_STD_VER >= 23 |
| 95 | |
| 96 | int main(int, char**) { |
| 97 | tests(); |
| 98 | #if TEST_STD_VER > 17 |
| 99 | static_assert(tests()); |
| 100 | #endif |
| 101 | #if TEST_STD_VER >= 23 |
| 102 | test_increasing_allocator(); |
| 103 | static_assert(test_increasing_allocator()); |
| 104 | #endif // TEST_STD_VER >= 23 |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |