Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.capacity / reserve_size.pass.cpp
blobbb804bf328b3b8d3ece34c9dc58b7145a87e06aa
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(size_type res_arg); // constexpr since C++20
13 // This test relies on https://llvm.org/PR45368 being fixed, which isn't in
14 // older Apple dylibs
15 // XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0}}
17 #include <string>
18 #include <stdexcept>
19 #include <cassert>
21 #include "test_macros.h"
22 #include "min_allocator.h"
24 template <class S>
25 TEST_CONSTEXPR_CXX20 void
26 test(typename S::size_type min_cap, typename S::size_type erased_index, typename S::size_type res_arg) {
27 S s(min_cap, 'a');
28 s.erase(erased_index);
29 assert(s.size() == erased_index);
30 assert(s.capacity() >= min_cap); // Check that we really have at least this capacity.
32 #if TEST_STD_VER > 17
33 typename S::size_type old_cap = s.capacity();
34 #endif
35 S s0 = s;
36 if (res_arg <= s.max_size()) {
37 s.reserve(res_arg);
38 LIBCPP_ASSERT(s.__invariants());
39 assert(s == s0);
40 assert(s.capacity() >= res_arg);
41 assert(s.capacity() >= s.size());
42 #if TEST_STD_VER > 17
43 assert(s.capacity() >= old_cap); // reserve never shrinks as of P0966 (C++20)
44 #endif
46 #ifndef TEST_HAS_NO_EXCEPTIONS
47 else if (!TEST_IS_CONSTANT_EVALUATED) {
48 try {
49 s.reserve(res_arg);
50 LIBCPP_ASSERT(s.__invariants());
51 assert(false);
52 } catch (std::length_error&) {
53 assert(res_arg > s.max_size());
56 #endif
59 template <class S>
60 TEST_CONSTEXPR_CXX20 void test_string() {
62 test<S>(0, 0, 5);
63 test<S>(0, 0, 10);
64 test<S>(0, 0, 50);
67 test<S>(100, 50, 5);
68 test<S>(100, 50, 10);
69 test<S>(100, 50, 50);
70 test<S>(100, 50, 100);
71 test<S>(100, 50, 1000);
72 test<S>(100, 50, S::npos);
76 TEST_CONSTEXPR_CXX20 bool test() {
77 test_string<std::string>();
78 #if TEST_STD_VER >= 11
79 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
80 #endif
82 return true;
85 int main(int, char**) {
86 test();
87 #if TEST_STD_VER > 17
88 static_assert(test());
89 #endif
91 return 0;