xref: /llvm-project/libcxx/test/std/strings/basic.string/string.capacity/reserve.pass.cpp (revision 57b08b0944046a6a57ee9b7b479181f548a5b9b4)
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 // Split into two calls for C++20
12 // void reserve();
13 // void reserve(size_type res_arg);
14 
15 #include <string>
16 #include <stdexcept>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "min_allocator.h"
21 
22 template <class S>
23 void
24 test(S s)
25 {
26     typename S::size_type old_cap = s.capacity();
27     S s0 = s;
28     s.reserve();
29     LIBCPP_ASSERT(s.__invariants());
30     assert(s == s0);
31     assert(s.capacity() <= old_cap);
32     assert(s.capacity() >= s.size());
33 }
34 
35 template <class S>
36 void
37 test(S s, typename S::size_type res_arg)
38 {
39     typename S::size_type old_cap = s.capacity();
40     ((void)old_cap); // Prevent unused warning
41     S s0 = s;
42     if (res_arg <= s.max_size())
43     {
44         s.reserve(res_arg);
45         assert(s == s0);
46         assert(s.capacity() >= res_arg);
47         assert(s.capacity() >= s.size());
48 #if TEST_STD_VER > 17
49         assert(s.capacity() >= old_cap); // resize never shrinks as of P0966
50 #endif
51     }
52 #ifndef TEST_HAS_NO_EXCEPTIONS
53     else
54     {
55         try
56         {
57             s.reserve(res_arg);
58             assert(false);
59         }
60         catch (std::length_error&)
61         {
62             assert(res_arg > s.max_size());
63         }
64     }
65 #endif
66 }
67 
68 int main()
69 {
70     {
71     typedef std::string S;
72     {
73     S s;
74     test(s);
75 
76     s.assign(10, 'a');
77     s.erase(5);
78     test(s);
79 
80     s.assign(100, 'a');
81     s.erase(50);
82     test(s);
83     }
84     {
85     S s;
86     test(s, 5);
87     test(s, 10);
88     test(s, 50);
89     }
90     {
91     S s(100, 'a');
92     s.erase(50);
93     test(s, 5);
94     test(s, 10);
95     test(s, 50);
96     test(s, 100);
97     test(s, 1000);
98     test(s, S::npos);
99     }
100     }
101 #if TEST_STD_VER >= 11
102     {
103     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
104     {
105     S s;
106     test(s);
107 
108     s.assign(10, 'a');
109     s.erase(5);
110     test(s);
111 
112     s.assign(100, 'a');
113     s.erase(50);
114     test(s);
115     }
116     {
117     S s;
118     test(s, 5);
119     test(s, 10);
120     test(s, 50);
121     }
122     {
123     S s(100, 'a');
124     s.erase(50);
125     test(s, 5);
126     test(s, 10);
127     test(s, 50);
128     test(s, 100);
129     test(s, 1000);
130     test(s, S::npos);
131     }
132     }
133 #endif
134 }
135