blob: a155825be752d25706d2da60a82a418571a290dc [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
Marshall Clowe34f6f62013-11-26 20:58:02 +000018#include "min_allocator.h"
Howard Hinnanteec72182013-06-28 16:59:19 +000019
Howard Hinnant3e519522010-05-11 19:42:16 +000020template <class S>
21void
22test(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
33template <class S>
34void
35test(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
53int main()
54{
Howard Hinnanteec72182013-06-28 16:59:19 +000055 {
Howard Hinnant3e519522010-05-11 19:42:16 +000056 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 }
Howard Hinnanteec72182013-06-28 16:59:19 +000084 }
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
Howard Hinnant3e519522010-05-11 19:42:16 +0000117}