blob: b2c254d1fb2537bf042777ef7dec8c5fa28cc1ac [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
Eric Fiselier1f4231f2016-04-28 22:28:23 +000019#include "test_macros.h"
Marshall Clowe34f6f62013-11-26 20:58:02 +000020#include "min_allocator.h"
Howard Hinnanteec72182013-06-28 16:59:19 +000021
Howard Hinnant3e519522010-05-11 19:42:16 +000022template <class S>
23void
24test(S s)
25{
26 typename S::size_type old_cap = s.capacity();
27 S s0 = s;
28 s.reserve();
Eric Fiselier1f4231f2016-04-28 22:28:23 +000029 LIBCPP_ASSERT(s.__invariants());
Howard Hinnant3e519522010-05-11 19:42:16 +000030 assert(s == s0);
31 assert(s.capacity() <= old_cap);
32 assert(s.capacity() >= s.size());
33}
34
35template <class S>
36void
37test(S s, typename S::size_type res_arg)
38{
39 typename S::size_type old_cap = s.capacity();
40 S s0 = s;
41 try
42 {
43 s.reserve(res_arg);
44 assert(res_arg <= s.max_size());
45 assert(s == s0);
46 assert(s.capacity() >= res_arg);
47 assert(s.capacity() >= s.size());
48 }
49 catch (std::length_error&)
50 {
51 assert(res_arg > s.max_size());
52 }
53}
54
55int main()
56{
Howard Hinnanteec72182013-06-28 16:59:19 +000057 {
Howard Hinnant3e519522010-05-11 19:42:16 +000058 typedef std::string S;
59 {
60 S s;
61 test(s);
62
63 s.assign(10, 'a');
64 s.erase(5);
65 test(s);
66
67 s.assign(100, 'a');
68 s.erase(50);
69 test(s);
70 }
71 {
72 S s;
73 test(s, 5);
74 test(s, 10);
75 test(s, 50);
76 }
77 {
78 S s(100, 'a');
79 s.erase(50);
80 test(s, 5);
81 test(s, 10);
82 test(s, 50);
83 test(s, 100);
84 test(s, S::npos);
85 }
Howard Hinnanteec72182013-06-28 16:59:19 +000086 }
Eric Fiselier1f4231f2016-04-28 22:28:23 +000087#if TEST_STD_VER >= 11
Howard Hinnanteec72182013-06-28 16:59:19 +000088 {
89 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
90 {
91 S s;
92 test(s);
93
94 s.assign(10, 'a');
95 s.erase(5);
96 test(s);
97
98 s.assign(100, 'a');
99 s.erase(50);
100 test(s);
101 }
102 {
103 S s;
104 test(s, 5);
105 test(s, 10);
106 test(s, 50);
107 }
108 {
109 S s(100, 'a');
110 s.erase(50);
111 test(s, 5);
112 test(s, 10);
113 test(s, 50);
114 test(s, 100);
115 test(s, S::npos);
116 }
117 }
118#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000119}