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

source code of libcxx/test/std/strings/basic.string/string.capacity/reserve.pass.cpp