xref: /llvm-project/libcxx/test/std/strings/basic.string/string.capacity/reserve.pass.cpp (revision 786366b18fadc3d7c4f150e89ef49c165767a668)
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 template <class S>
41 void test_string() {
42   test<S>(0, 0);
43   test<S>(10, 5);
44   test<S>(100, 50);
45 }
46 
47 bool test() {
48   test_string<std::string>();
49 #if TEST_STD_VER >= 11
50   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
51 #endif
52 
53   return true;
54 }
55 
56 int main(int, char**)
57 {
58   test();
59 
60   return 0;
61 }
62