xref: /llvm-project/libcxx/test/std/strings/basic.string/string.capacity/reserve.pass.cpp (revision e85018b7dd3a89dda6b22319b03b5906f8a85aed)
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 TEST_CONSTEXPR_CXX20 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 bool test() {
41     {
42     typedef std::string S;
43     {
44     test<S>(0, 0);
45     test<S>(10, 5);
46     test<S>(100, 50);
47     }
48     }
49 #if TEST_STD_VER >= 11
50     {
51     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
52     {
53     test<S>(0, 0);
54     test<S>(10, 5);
55     test<S>(100, 50);
56     }
57     }
58 #endif
59 
60   return true;
61 }
62 
63 int main(int, char**)
64 {
65   test();
66 #if TEST_STD_VER > 17
67   // static_assert(test());
68 #endif
69 
70   return 0;
71 }
72