]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/strings/basic.string/string.capacity/reserve.pass.cpp
Vendor import of libc++ trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / std / strings / basic.string / string.capacity / reserve.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <string>
11
12 // Split into two calls for C++20
13 // void reserve();
14 // void reserve(size_type res_arg);
15
16 #include <string>
17 #include <stdexcept>
18 #include <cassert>
19
20 #include "test_macros.h"
21 #include "min_allocator.h"
22
23 template <class S>
24 void
25 test(S s)
26 {
27     typename S::size_type old_cap = s.capacity();
28     S s0 = s;
29     s.reserve();
30     LIBCPP_ASSERT(s.__invariants());
31     assert(s == s0);
32     assert(s.capacity() <= old_cap);
33     assert(s.capacity() >= s.size());
34 }
35
36 template <class S>
37 void
38 test(S s, typename S::size_type res_arg)
39 {
40     typename S::size_type old_cap = s.capacity();
41     ((void)old_cap); // Prevent unused warning
42     S s0 = s;
43     if (res_arg <= s.max_size())
44     {
45         s.reserve(res_arg);
46         assert(s == s0);
47         assert(s.capacity() >= res_arg);
48         assert(s.capacity() >= s.size());
49 #if TEST_STD_VER > 17
50         assert(s.capacity() >= old_cap); // resize never shrinks as of P0966
51 #endif
52     }
53 #ifndef TEST_HAS_NO_EXCEPTIONS
54     else
55     {
56         try
57         {
58             s.reserve(res_arg);
59             assert(false);
60         }
61         catch (std::length_error&)
62         {
63             assert(res_arg > s.max_size());
64         }
65     }
66 #endif
67 }
68
69 int main()
70 {
71     {
72     typedef std::string S;
73     {
74     S s;
75     test(s);
76
77     s.assign(10, 'a');
78     s.erase(5);
79     test(s);
80
81     s.assign(100, 'a');
82     s.erase(50);
83     test(s);
84     }
85     {
86     S s;
87     test(s, 5);
88     test(s, 10);
89     test(s, 50);
90     }
91     {
92     S s(100, 'a');
93     s.erase(50);
94     test(s, 5);
95     test(s, 10);
96     test(s, 50);
97     test(s, 100);
98     test(s, 1000);
99     test(s, S::npos);
100     }
101     }
102 #if TEST_STD_VER >= 11
103     {
104     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
105     {
106     S s;
107     test(s);
108
109     s.assign(10, 'a');
110     s.erase(5);
111     test(s);
112
113     s.assign(100, 'a');
114     s.erase(50);
115     test(s);
116     }
117     {
118     S s;
119     test(s, 5);
120     test(s, 10);
121     test(s, 50);
122     }
123     {
124     S s(100, 'a');
125     s.erase(50);
126     test(s, 5);
127     test(s, 10);
128     test(s, 50);
129     test(s, 100);
130     test(s, 1000);
131     test(s, S::npos);
132     }
133     }
134 #endif
135 }