• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test_macros.h"
19 #include "min_allocator.h"
20 
21 template <class S>
22 void
test(S s)23 test(S s)
24 {
25     typename S::size_type old_cap = s.capacity();
26     S s0 = s;
27     s.reserve();
28     LIBCPP_ASSERT(s.__invariants());
29     assert(s == s0);
30     assert(s.capacity() <= old_cap);
31     assert(s.capacity() >= s.size());
32 }
33 
34 template <class S>
35 void
test(S s,typename S::size_type res_arg)36 test(S s, typename S::size_type res_arg)
37 {
38     typename S::size_type old_cap = s.capacity();
39     ((void)old_cap); // Prevent unused warning
40     S s0 = s;
41     if (res_arg <= s.max_size())
42     {
43         s.reserve(res_arg);
44         assert(s == s0);
45         assert(s.capacity() >= res_arg);
46         assert(s.capacity() >= s.size());
47     }
48 #ifndef TEST_HAS_NO_EXCEPTIONS
49     else
50     {
51         try
52         {
53             s.reserve(res_arg);
54             assert(false);
55         }
56         catch (std::length_error&)
57         {
58             assert(res_arg > s.max_size());
59         }
60     }
61 #endif
62 }
63 
main()64 int main()
65 {
66     {
67     typedef std::string S;
68     {
69     S s;
70     test(s);
71 
72     s.assign(10, 'a');
73     s.erase(5);
74     test(s);
75 
76     s.assign(100, 'a');
77     s.erase(50);
78     test(s);
79     }
80     {
81     S s;
82     test(s, 5);
83     test(s, 10);
84     test(s, 50);
85     }
86     {
87     S s(100, 'a');
88     s.erase(50);
89     test(s, 5);
90     test(s, 10);
91     test(s, 50);
92     test(s, 100);
93     test(s, S::npos);
94     }
95     }
96 #if TEST_STD_VER >= 11
97     {
98     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
99     {
100     S s;
101     test(s);
102 
103     s.assign(10, 'a');
104     s.erase(5);
105     test(s);
106 
107     s.assign(100, 'a');
108     s.erase(50);
109     test(s);
110     }
111     {
112     S s;
113     test(s, 5);
114     test(s, 10);
115     test(s, 50);
116     }
117     {
118     S s(100, 'a');
119     s.erase(50);
120     test(s, 5);
121     test(s, 10);
122     test(s, 50);
123     test(s, 100);
124     test(s, S::npos);
125     }
126     }
127 #endif
128 }
129