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