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