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 // void reserve(size_type res_arg=0);
13 
14 #include <string>
15 #include <stdexcept>
16 #include <cassert>
17 
18 #include "../min_allocator.h"
19 
20 template <class S>
21 void
22 test(S s)
23 {
24     typename S::size_type old_cap = s.capacity();
25     S s0 = s;
26     s.reserve();
27     assert(s.__invariants());
28     assert(s == s0);
29     assert(s.capacity() <= old_cap);
30     assert(s.capacity() >= s.size());
31 }
32 
33 template <class S>
34 void
35 test(S s, typename S::size_type res_arg)
36 {
37     typename S::size_type old_cap = s.capacity();
38     S s0 = s;
39     try
40     {
41         s.reserve(res_arg);
42         assert(res_arg <= s.max_size());
43         assert(s == s0);
44         assert(s.capacity() >= res_arg);
45         assert(s.capacity() >= s.size());
46     }
47     catch (std::length_error&)
48     {
49         assert(res_arg > s.max_size());
50     }
51 }
52 
53 int main()
54 {
55     {
56     typedef std::string S;
57     {
58     S s;
59     test(s);
60 
61     s.assign(10, 'a');
62     s.erase(5);
63     test(s);
64 
65     s.assign(100, 'a');
66     s.erase(50);
67     test(s);
68     }
69     {
70     S s;
71     test(s, 5);
72     test(s, 10);
73     test(s, 50);
74     }
75     {
76     S s(100, 'a');
77     s.erase(50);
78     test(s, 5);
79     test(s, 10);
80     test(s, 50);
81     test(s, 100);
82     test(s, S::npos);
83     }
84     }
85 #if __cplusplus >= 201103L
86     {
87     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
88     {
89     S s;
90     test(s);
91 
92     s.assign(10, 'a');
93     s.erase(5);
94     test(s);
95 
96     s.assign(100, 'a');
97     s.erase(50);
98     test(s);
99     }
100     {
101     S s;
102     test(s, 5);
103     test(s, 10);
104     test(s, 50);
105     }
106     {
107     S s(100, 'a');
108     s.erase(50);
109     test(s, 5);
110     test(s, 10);
111     test(s, 50);
112     test(s, 100);
113     test(s, S::npos);
114     }
115     }
116 #endif
117 }
118