blob: 8b9dc13db838a2f823a735b0635568ee3a8abcf3 [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
Marshall Clow4d64d7d2018-11-28 18:18:34 +000012// Split into two calls for C++20
13// void reserve();
14// void reserve(size_type res_arg);
Howard Hinnant3e519522010-05-11 19:42:16 +000015
16#include <string>
17#include <stdexcept>
18#include <cassert>
19
Eric Fiselier1f4231f2016-04-28 22:28:23 +000020#include "test_macros.h"
Marshall Clowe34f6f62013-11-26 20:58:02 +000021#include "min_allocator.h"
Howard Hinnanteec72182013-06-28 16:59:19 +000022
Howard Hinnant3e519522010-05-11 19:42:16 +000023template <class S>
24void
25test(S s)
26{
27 typename S::size_type old_cap = s.capacity();
28 S s0 = s;
29 s.reserve();
Eric Fiselier1f4231f2016-04-28 22:28:23 +000030 LIBCPP_ASSERT(s.__invariants());
Howard Hinnant3e519522010-05-11 19:42:16 +000031 assert(s == s0);
32 assert(s.capacity() <= old_cap);
33 assert(s.capacity() >= s.size());
34}
35
36template <class S>
37void
38test(S s, typename S::size_type res_arg)
39{
40 typename S::size_type old_cap = s.capacity();
Stephan T. Lavavej4dc0ed82016-11-14 17:35:14 +000041 ((void)old_cap); // Prevent unused warning
Howard Hinnant3e519522010-05-11 19:42:16 +000042 S s0 = s;
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000043 if (res_arg <= s.max_size())
Howard Hinnant3e519522010-05-11 19:42:16 +000044 {
45 s.reserve(res_arg);
Howard Hinnant3e519522010-05-11 19:42:16 +000046 assert(s == s0);
47 assert(s.capacity() >= res_arg);
48 assert(s.capacity() >= s.size());
Marshall Clow4d64d7d2018-11-28 18:18:34 +000049#if TEST_STD_VER > 17
50 assert(s.capacity() >= old_cap); // resize never shrinks as of P0966
51#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000052 }
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000053#ifndef TEST_HAS_NO_EXCEPTIONS
54 else
Howard Hinnant3e519522010-05-11 19:42:16 +000055 {
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000056 try
57 {
58 s.reserve(res_arg);
59 assert(false);
60 }
61 catch (std::length_error&)
62 {
63 assert(res_arg > s.max_size());
64 }
Howard Hinnant3e519522010-05-11 19:42:16 +000065 }
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000066#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000067}
68
69int main()
70{
Howard Hinnanteec72182013-06-28 16:59:19 +000071 {
Howard Hinnant3e519522010-05-11 19:42:16 +000072 typedef std::string S;
73 {
74 S s;
75 test(s);
76
77 s.assign(10, 'a');
78 s.erase(5);
79 test(s);
80
81 s.assign(100, 'a');
82 s.erase(50);
83 test(s);
84 }
85 {
86 S s;
87 test(s, 5);
88 test(s, 10);
89 test(s, 50);
90 }
91 {
92 S s(100, 'a');
93 s.erase(50);
94 test(s, 5);
95 test(s, 10);
96 test(s, 50);
97 test(s, 100);
Marshall Clow4d64d7d2018-11-28 18:18:34 +000098 test(s, 1000);
Howard Hinnant3e519522010-05-11 19:42:16 +000099 test(s, S::npos);
100 }
Howard Hinnanteec72182013-06-28 16:59:19 +0000101 }
Eric Fiselier1f4231f2016-04-28 22:28:23 +0000102#if TEST_STD_VER >= 11
Howard Hinnanteec72182013-06-28 16:59:19 +0000103 {
104 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
105 {
106 S s;
107 test(s);
108
109 s.assign(10, 'a');
110 s.erase(5);
111 test(s);
112
113 s.assign(100, 'a');
114 s.erase(50);
115 test(s);
116 }
117 {
118 S s;
119 test(s, 5);
120 test(s, 10);
121 test(s, 50);
122 }
123 {
124 S s(100, 'a');
125 s.erase(50);
126 test(s, 5);
127 test(s, 10);
128 test(s, 50);
129 test(s, 100);
Marshall Clow4d64d7d2018-11-28 18:18:34 +0000130 test(s, 1000);
Howard Hinnanteec72182013-06-28 16:59:19 +0000131 test(s, S::npos);
132 }
133 }
134#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000135}