xref: /llvm-project/libcxx/test/std/strings/basic.string/string.capacity/reserve.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
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 test(typename S::size_type min_cap, typename S::size_type erased_index) {
24   S s(min_cap, 'a');
25   s.erase(erased_index);
26   assert(s.size() == erased_index);
27   assert(s.capacity() >= min_cap); // Check that we really have at least this capacity.
28 
29   typename S::size_type old_cap = s.capacity();
30   S s0                          = s;
31   s.reserve();
32   LIBCPP_ASSERT(s.__invariants());
33   assert(s == s0);
34   assert(s.capacity() <= old_cap);
35   assert(s.capacity() >= s.size());
36 }
37 
38 template <class S>
39 void test_string() {
40   test<S>(0, 0);
41   test<S>(10, 5);
42   test<S>(100, 50);
43 }
44 
45 bool test() {
46   test_string<std::string>();
47 #if TEST_STD_VER >= 11
48   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
49 #endif
50 
51   return true;
52 }
53 
54 int main(int, char**) {
55   test();
56 
57   return 0;
58 }
59