Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.capacity / shrink_to_fit.pass.cpp
blob1efebfc9cea561b14a8998d83cac36696077ab8f
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 //===----------------------------------------------------------------------===//
9 // <string>
11 // void shrink_to_fit(); // constexpr since C++20
13 #include <string>
14 #include <cassert>
16 #include "test_macros.h"
17 #include "min_allocator.h"
19 template <class S>
20 TEST_CONSTEXPR_CXX20 void test(S s) {
21 typename S::size_type old_cap = s.capacity();
22 S s0 = s;
23 s.shrink_to_fit();
24 LIBCPP_ASSERT(s.__invariants());
25 assert(s == s0);
26 assert(s.capacity() <= old_cap);
27 assert(s.capacity() >= s.size());
30 template <class S>
31 TEST_CONSTEXPR_CXX20 void test_string() {
32 S s;
33 test(s);
35 s.assign(10, 'a');
36 s.erase(5);
37 test(s);
39 s.assign(100, 'a');
40 s.erase(50);
41 test(s);
44 TEST_CONSTEXPR_CXX20 bool test() {
45 test_string<std::string>();
46 #if TEST_STD_VER >= 11
47 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
48 #endif
50 return true;
53 int main(int, char**) {
54 test();
55 #if TEST_STD_VER > 17
56 static_assert(test());
57 #endif
59 return 0;