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 // void reserve(size_type res_arg);
12 
13 // This test relies on https://llvm.org/PR45368 being fixed, which isn't in
14 // older Apple dylibs
15 //
16 // XFAIL: with_system_cxx_lib=macosx10.15
17 // XFAIL: with_system_cxx_lib=macosx10.14
18 // XFAIL: with_system_cxx_lib=macosx10.13
19 // XFAIL: with_system_cxx_lib=macosx10.12
20 // XFAIL: with_system_cxx_lib=macosx10.11
21 // XFAIL: with_system_cxx_lib=macosx10.10
22 // XFAIL: with_system_cxx_lib=macosx10.9
23 
24 #include <string>
25 #include <stdexcept>
26 #include <cassert>
27 
28 #include "test_macros.h"
29 #include "min_allocator.h"
30 
31 template <class S>
32 void
33 test(typename S::size_type min_cap, typename S::size_type erased_index, typename S::size_type res_arg)
34 {
35     S s(min_cap, 'a');
36     s.erase(erased_index);
37     assert(s.size() == erased_index);
38     assert(s.capacity() >= min_cap); // Check that we really have at least this capacity.
39 
40 #if TEST_STD_VER > 17
41     typename S::size_type old_cap = s.capacity();
42 #endif
43     S s0 = s;
44     if (res_arg <= s.max_size())
45     {
46         s.reserve(res_arg);
47         LIBCPP_ASSERT(s.__invariants());
48         assert(s == s0);
49         assert(s.capacity() >= res_arg);
50         assert(s.capacity() >= s.size());
51 #if TEST_STD_VER > 17
52         assert(s.capacity() >= old_cap); // reserve never shrinks as of P0966 (C++20)
53 #endif
54     }
55 #ifndef TEST_HAS_NO_EXCEPTIONS
56     else
57     {
58         try
59         {
60             s.reserve(res_arg);
61             LIBCPP_ASSERT(s.__invariants());
62             assert(false);
63         }
64         catch (std::length_error&)
65         {
66             assert(res_arg > s.max_size());
67         }
68     }
69 #endif
70 }
71 
72 int main(int, char**)
73 {
74     {
75     typedef std::string S;
76     {
77     test<S>(0, 0, 5);
78     test<S>(0, 0, 10);
79     test<S>(0, 0, 50);
80     }
81     {
82     test<S>(100, 50, 5);
83     test<S>(100, 50, 10);
84     test<S>(100, 50, 50);
85     test<S>(100, 50, 100);
86     test<S>(100, 50, 1000);
87     test<S>(100, 50, S::npos);
88     }
89     }
90 #if TEST_STD_VER >= 11
91     {
92     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
93     {
94     test<S>(0, 0, 5);
95     test<S>(0, 0, 10);
96     test<S>(0, 0, 50);
97     }
98     {
99     test<S>(100, 50, 5);
100     test<S>(100, 50, 10);
101     test<S>(100, 50, 50);
102     test<S>(100, 50, 100);
103     test<S>(100, 50, 1000);
104     test<S>(100, 50, S::npos);
105     }
106     }
107 #endif
108 
109   return 0;
110 }
111