Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.capacity / reserve.pass.cpp
blob03e3cf2f9d7d38e7b6e4c687a1052542d7385ffb
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 reserve(); // Deprecated in C++20.
13 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
15 #include <string>
16 #include <stdexcept>
17 #include <cassert>
19 #include "test_macros.h"
20 #include "min_allocator.h"
22 template <class S>
23 void test(typename S::size_type min_cap, typename S::size_type erased_index) {
24 S s(min_cap, 'a');
25 s.erase(erased_index);
26 assert(s.size() == erased_index);
27 assert(s.capacity() >= min_cap); // Check that we really have at least this capacity.
29 typename S::size_type old_cap = s.capacity();
30 S s0 = s;
31 s.reserve();
32 LIBCPP_ASSERT(s.__invariants());
33 assert(s == s0);
34 assert(s.capacity() <= old_cap);
35 assert(s.capacity() >= s.size());
38 template <class S>
39 void test_string() {
40 test<S>(0, 0);
41 test<S>(10, 5);
42 test<S>(100, 50);
45 bool test() {
46 test_string<std::string>();
47 #if TEST_STD_VER >= 11
48 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
49 #endif
51 return true;
54 int main(int, char**) {
55 test();
57 return 0;