blob: 7210152ea3cb1318e78f1f16e74ed56d660fd4e5 [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
Eric Fiselier1f4231f2016-04-28 22:28:23 +000018#include "test_macros.h"
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();
Eric Fiselier1f4231f2016-04-28 22:28:23 +000028 LIBCPP_ASSERT(s.__invariants());
Howard Hinnant3e519522010-05-11 19:42:16 +000029 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();
Stephan T. Lavavej4dc0ed82016-11-14 17:35:14 +000039 ((void)old_cap); // Prevent unused warning
Howard Hinnant3e519522010-05-11 19:42:16 +000040 S s0 = s;
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000041 if (res_arg <= s.max_size())
Howard Hinnant3e519522010-05-11 19:42:16 +000042 {
43 s.reserve(res_arg);
Howard Hinnant3e519522010-05-11 19:42:16 +000044 assert(s == s0);
45 assert(s.capacity() >= res_arg);
46 assert(s.capacity() >= s.size());
47 }
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000048#ifndef TEST_HAS_NO_EXCEPTIONS
49 else
Howard Hinnant3e519522010-05-11 19:42:16 +000050 {
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000051 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 }
Howard Hinnant3e519522010-05-11 19:42:16 +000060 }
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000061#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000062}
63
64int main()
65{
Howard Hinnanteec72182013-06-28 16:59:19 +000066 {
Howard Hinnant3e519522010-05-11 19:42:16 +000067 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 }
Howard Hinnanteec72182013-06-28 16:59:19 +000095 }
Eric Fiselier1f4231f2016-04-28 22:28:23 +000096#if TEST_STD_VER >= 11
Howard Hinnanteec72182013-06-28 16:59:19 +000097 {
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
Howard Hinnant3e519522010-05-11 19:42:16 +0000128}