xref: /llvm-project/libcxx/test/std/strings/basic.string/string.capacity/shrink_to_fit.pass.cpp (revision 57b08b0944046a6a57ee9b7b479181f548a5b9b4)
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 shrink_to_fit();
12 
13 #include <string>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 #include "min_allocator.h"
18 
19 template <class S>
20 void
21 test(S s)
22 {
23     typename S::size_type old_cap = s.capacity();
24     S s0 = s;
25     s.shrink_to_fit();
26     LIBCPP_ASSERT(s.__invariants());
27     assert(s == s0);
28     assert(s.capacity() <= old_cap);
29     assert(s.capacity() >= s.size());
30 }
31 
32 int main()
33 {
34     {
35     typedef std::string S;
36     S s;
37     test(s);
38 
39     s.assign(10, 'a');
40     s.erase(5);
41     test(s);
42 
43     s.assign(100, 'a');
44     s.erase(50);
45     test(s);
46     }
47 #if TEST_STD_VER >= 11
48     {
49     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
50     S s;
51     test(s);
52 
53     s.assign(10, 'a');
54     s.erase(5);
55     test(s);
56 
57     s.assign(100, 'a');
58     s.erase(50);
59     test(s);
60     }
61 #endif
62 }
63