xref: /llvm-project/libcxx/test/libcxx/strings/basic.string/string.modifiers/clear_and_shrink.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 // Call __clear_and_shrink() and ensure string invariants hold
12 
13 #include <string>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 
test()18 TEST_CONSTEXPR_CXX20 bool test() {
19   std::string l = "Long string so that allocation definitely, for sure, absolutely happens. Probably.";
20   std::string s = "short";
21 
22   assert(l.__invariants());
23   assert(s.__invariants());
24 
25   s.__clear_and_shrink();
26   assert(s.__invariants());
27   assert(s.size() == 0);
28 
29   std::string::size_type cap = l.capacity();
30   l.__clear_and_shrink();
31   assert(l.__invariants());
32   assert(l.size() == 0);
33   assert(l.capacity() < cap);
34 
35   return true;
36 }
37 
main(int,char **)38 int main(int, char**) {
39   test();
40 #if TEST_STD_VER > 17
41   static_assert(test());
42 #endif
43   return 0;
44 }
45