diff --git a/libcxx/include/__split_buffer b/libcxx/include/__split_buffer index bab724d1b8963..381e3d2bc7b7c 100644 --- a/libcxx/include/__split_buffer +++ b/libcxx/include/__split_buffer @@ -430,7 +430,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::swap(__split template _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::reserve(size_type __n) { - if (__n < capacity()) { + if (__n > capacity()) { __split_buffer __t(__n, 0, __alloc()); __t.__construct_at_end(move_iterator(__begin_), move_iterator(__end_)); std::swap(__first_, __t.__first_); diff --git a/libcxx/test/libcxx/utilities/split_buffer.pass.cpp b/libcxx/test/libcxx/utilities/split_buffer.pass.cpp new file mode 100644 index 0000000000000..6ce361508fd2d --- /dev/null +++ b/libcxx/test/libcxx/utilities/split_buffer.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include <__split_buffer> +#include <__config> +#include +#include +#include +#include + +#include "test_macros.h" + +struct simple_test_type { + int value; +}; + +struct complex_test_type { + std::string value; + complex_test_type(const std::string& v) : value(v) {} +}; + +template +_LIBCPP_CONSTEXPR_SINCE_CXX20 int capacity_after_initialization() { + std::__split_buffer sb; + return sb.capacity(); +} + +template +_LIBCPP_CONSTEXPR_SINCE_CXX20 int capacity_after_reserve(const std::size_t n) { + std::__split_buffer sb; + sb.reserve(n); + return sb.capacity(); +} + +int main() { + { // check test_types' features +#if _LIBCPP_STD_VER >= 17 + static_assert(std::is_trivially_copyable_v); + static_assert(not std::is_trivially_copyable_v); +#endif + } + + { // test simple_test_type at run-time + assert(0 == capacity_after_initialization()); + assert(42 == capacity_after_reserve(42)); + } + + { // test complex_test_type at run-time + assert(0 == capacity_after_initialization()); + assert(42 == capacity_after_reserve(42)); + } + +#if _LIBCPP_STD_VER >= 20 + { // test simple_test_type at compile-time + static_assert(0 == capacity_after_initialization()); + static_assert(42 == capacity_after_reserve(42)); + } + + { // test complex_test_type at compile-time + static_assert(0 == capacity_after_initialization()); + static_assert(42 == capacity_after_reserve(42)); + } +#endif +}