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 // <string> 10 11 // void reserve(); // Deprecated in C++20. 12 13 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS 14 15 #include <string> 16 #include <stdexcept> 17 #include <cassert> 18 19 #include "test_macros.h" 20 #include "min_allocator.h" 21 22 template <class S> 23 void 24 test(typename S::size_type min_cap, typename S::size_type erased_index) 25 { 26 S s(min_cap, 'a'); 27 s.erase(erased_index); 28 assert(s.size() == erased_index); 29 assert(s.capacity() >= min_cap); // Check that we really have at least this capacity. 30 31 typename S::size_type old_cap = s.capacity(); 32 S s0 = s; 33 s.reserve(); 34 LIBCPP_ASSERT(s.__invariants()); 35 assert(s == s0); 36 assert(s.capacity() <= old_cap); 37 assert(s.capacity() >= s.size()); 38 } 39 40 int main(int, char**) 41 { 42 { 43 typedef std::string S; 44 { 45 test<S>(0, 0); 46 test<S>(10, 5); 47 test<S>(100, 50); 48 } 49 } 50 #if TEST_STD_VER >= 11 51 { 52 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 53 { 54 test<S>(0, 0); 55 test<S>(10, 5); 56 test<S>(100, 50); 57 } 58 } 59 #endif 60 61 return 0; 62 } 63