diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h index 569cc5ea898b..bc99470f126f 100644 --- a/libcxx/include/__vector/vector_bool.h +++ b/libcxx/include/__vector/vector_bool.h @@ -549,7 +549,7 @@ vector::__recommend(size_type __new_size) const { const size_type __cap = capacity(); if (__cap >= __ms / 2) return __ms; - return std::max(2 * __cap, __align_it(__new_size)); + return std::max(2 * __cap, __align_it(__new_size)); } // Default constructs __n objects starting at __end_ diff --git a/libcxx/test/std/containers/sequences/vector.bool/small_allocator_size.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/small_allocator_size.pass.cpp new file mode 100644 index 000000000000..95e4c18cc798 --- /dev/null +++ b/libcxx/test/std/containers/sequences/vector.bool/small_allocator_size.pass.cpp @@ -0,0 +1,102 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// +// vector + +// XFAIL: FROZEN-CXX03-HEADERS-FIXME + +// This test ensures that std::vector handles allocator types with small size types +// properly. Related issue: https://github.com/llvm/llvm-project/issues/121713. + +#include +#include +#include +#include +#include +#include +#include + +#include "sized_allocator.h" +#include "test_macros.h" + +TEST_CONSTEXPR_CXX20 bool tests() { + { + using Alloc = sized_allocator; + std::vector c(Alloc(1)); + c.resize(10); + assert(c.size() == 10); + assert(c.capacity() >= 10); + } + { + using Alloc = sized_allocator; + std::vector c(Alloc(1)); + c.assign(10, true); + assert(c.size() == 10); + assert(c.capacity() >= 10); + } + { + using Alloc = sized_allocator; + std::vector c(Alloc(1)); + c.insert(c.end(), true); + assert(c.size() == 1); + assert(c.capacity() >= 1); + } + { + using Alloc = sized_allocator; + std::vector c(Alloc(1)); + c.insert(c.end(), 10, true); + assert(c.size() == 10); + assert(c.capacity() >= 10); + } + { + using Alloc = sized_allocator; + std::vector c(Alloc(1)); + c.push_back(true); + assert(c.size() == 1); + assert(c.capacity() >= 1); + } + { + using Alloc = sized_allocator; + std::vector c(Alloc(1)); + c.resize(10, true); + assert(c.size() == 10); + assert(c.capacity() >= 10); + } + { + using Alloc = sized_allocator; + std::vector c(Alloc(1)); + c.resize(10); + assert(c.size() == 10); + assert(c.capacity() >= 10); + } + { + using Alloc = sized_allocator; + std::vector c(Alloc(1)); + c.resize(10); + assert(c.size() == 10); + assert(c.capacity() >= 10); + } + { + using Alloc = sized_allocator; + std::vector c(Alloc(1)); + c.resize(10); + assert(c.size() == 10); + assert(c.capacity() >= 10); + } + + return true; +} + +int main(int, char**) { + tests(); +#if TEST_STD_VER >= 20 + static_assert(tests()); +#endif + return 0; +}