xref: /llvm-project/libcxx/test/std/strings/basic.string/string.capacity/shrink_to_fit.pass.cpp (revision 425620ccdd47e56b59512913cdc71e116f951e4e)
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 // XFAIL: LIBCXX-AIX-FIXME
10 
11 // <string>
12 
13 // void shrink_to_fit(); // constexpr since C++20
14 
15 #include <string>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 #include "min_allocator.h"
20 
21 template <class S>
22 TEST_CONSTEXPR_CXX20 void
23 test(S s)
24 {
25     typename S::size_type old_cap = s.capacity();
26     S s0 = s;
27     s.shrink_to_fit();
28     LIBCPP_ASSERT(s.__invariants());
29     assert(s == s0);
30     assert(s.capacity() <= old_cap);
31     assert(s.capacity() >= s.size());
32 }
33 
34 TEST_CONSTEXPR_CXX20 bool test() {
35   {
36     typedef std::string S;
37     S s;
38     test(s);
39 
40     s.assign(10, 'a');
41     s.erase(5);
42     test(s);
43 
44     s.assign(100, 'a');
45     s.erase(50);
46     test(s);
47   }
48 #if TEST_STD_VER >= 11
49   {
50     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
51     S s;
52     test(s);
53 
54     s.assign(10, 'a');
55     s.erase(5);
56     test(s);
57 
58     s.assign(100, 'a');
59     s.erase(50);
60     test(s);
61   }
62 #endif
63 
64   return true;
65 }
66 
67 int main(int, char**)
68 {
69   test();
70 #if TEST_STD_VER > 17
71   static_assert(test());
72 #endif
73 
74   return 0;
75 }
76