blob: 0689a8b406ee1bc7382c35ed61c1326388adad48 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00004//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <string>
11
12// void reserve(size_type res_arg=0);
13
14#include <string>
15#include <stdexcept>
16#include <cassert>
17
18template <class S>
19void
20test(S s)
21{
22 typename S::size_type old_cap = s.capacity();
23 S s0 = s;
24 s.reserve();
25 assert(s.__invariants());
26 assert(s == s0);
27 assert(s.capacity() <= old_cap);
28 assert(s.capacity() >= s.size());
29}
30
31template <class S>
32void
33test(S s, typename S::size_type res_arg)
34{
35 typename S::size_type old_cap = s.capacity();
36 S s0 = s;
37 try
38 {
39 s.reserve(res_arg);
40 assert(res_arg <= s.max_size());
41 assert(s == s0);
42 assert(s.capacity() >= res_arg);
43 assert(s.capacity() >= s.size());
44 }
45 catch (std::length_error&)
46 {
47 assert(res_arg > s.max_size());
48 }
49}
50
51int main()
52{
53 typedef std::string S;
54 {
55 S s;
56 test(s);
57
58 s.assign(10, 'a');
59 s.erase(5);
60 test(s);
61
62 s.assign(100, 'a');
63 s.erase(50);
64 test(s);
65 }
66 {
67 S s;
68 test(s, 5);
69 test(s, 10);
70 test(s, 50);
71 }
72 {
73 S s(100, 'a');
74 s.erase(50);
75 test(s, 5);
76 test(s, 10);
77 test(s, 50);
78 test(s, 100);
79 test(s, S::npos);
80 }
81}