blob: 34582a40dda8b74b363cd67213a9eaea606bb312 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnant3e519522010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
8
9// <string>
10
Marshall Clow4d64d7d2018-11-28 18:18:34 +000011// Split into two calls for C++20
12// void reserve();
13// void reserve(size_type res_arg);
Howard Hinnant3e519522010-05-11 19:42:16 +000014
Louis Dionne25838c62019-02-27 00:57:57 +000015// When back-deploying to macosx10.7, the RTTI for exception classes
16// incorrectly provided by libc++.dylib is mixed with the one in
17// libc++abi.dylib and exceptions are not caught properly.
18// XFAIL: with_system_cxx_lib=macosx10.7
19
Howard Hinnant3e519522010-05-11 19:42:16 +000020#include <string>
21#include <stdexcept>
22#include <cassert>
23
Eric Fiselier1f4231f2016-04-28 22:28:23 +000024#include "test_macros.h"
Marshall Clowe34f6f62013-11-26 20:58:02 +000025#include "min_allocator.h"
Howard Hinnanteec72182013-06-28 16:59:19 +000026
Howard Hinnant3e519522010-05-11 19:42:16 +000027template <class S>
28void
29test(S s)
30{
31 typename S::size_type old_cap = s.capacity();
32 S s0 = s;
33 s.reserve();
Eric Fiselier1f4231f2016-04-28 22:28:23 +000034 LIBCPP_ASSERT(s.__invariants());
Howard Hinnant3e519522010-05-11 19:42:16 +000035 assert(s == s0);
36 assert(s.capacity() <= old_cap);
37 assert(s.capacity() >= s.size());
38}
39
40template <class S>
41void
42test(S s, typename S::size_type res_arg)
43{
44 typename S::size_type old_cap = s.capacity();
Stephan T. Lavavej4dc0ed82016-11-14 17:35:14 +000045 ((void)old_cap); // Prevent unused warning
Howard Hinnant3e519522010-05-11 19:42:16 +000046 S s0 = s;
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000047 if (res_arg <= s.max_size())
Howard Hinnant3e519522010-05-11 19:42:16 +000048 {
49 s.reserve(res_arg);
Howard Hinnant3e519522010-05-11 19:42:16 +000050 assert(s == s0);
51 assert(s.capacity() >= res_arg);
52 assert(s.capacity() >= s.size());
Marshall Clow4d64d7d2018-11-28 18:18:34 +000053#if TEST_STD_VER > 17
54 assert(s.capacity() >= old_cap); // resize never shrinks as of P0966
55#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000056 }
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000057#ifndef TEST_HAS_NO_EXCEPTIONS
58 else
Howard Hinnant3e519522010-05-11 19:42:16 +000059 {
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000060 try
61 {
62 s.reserve(res_arg);
63 assert(false);
64 }
65 catch (std::length_error&)
66 {
67 assert(res_arg > s.max_size());
68 }
Howard Hinnant3e519522010-05-11 19:42:16 +000069 }
Roger Ferrer Ibanez8a915ed2016-11-01 15:46:16 +000070#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000071}
72
JF Bastien2df59c52019-02-04 20:31:13 +000073int main(int, char**)
Howard Hinnant3e519522010-05-11 19:42:16 +000074{
Howard Hinnanteec72182013-06-28 16:59:19 +000075 {
Howard Hinnant3e519522010-05-11 19:42:16 +000076 typedef std::string S;
77 {
78 S s;
79 test(s);
80
81 s.assign(10, 'a');
82 s.erase(5);
83 test(s);
84
85 s.assign(100, 'a');
86 s.erase(50);
87 test(s);
88 }
89 {
90 S s;
91 test(s, 5);
92 test(s, 10);
93 test(s, 50);
94 }
95 {
96 S s(100, 'a');
97 s.erase(50);
98 test(s, 5);
99 test(s, 10);
100 test(s, 50);
101 test(s, 100);
Marshall Clow4d64d7d2018-11-28 18:18:34 +0000102 test(s, 1000);
Howard Hinnant3e519522010-05-11 19:42:16 +0000103 test(s, S::npos);
104 }
Howard Hinnanteec72182013-06-28 16:59:19 +0000105 }
Eric Fiselier1f4231f2016-04-28 22:28:23 +0000106#if TEST_STD_VER >= 11
Howard Hinnanteec72182013-06-28 16:59:19 +0000107 {
108 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
109 {
110 S s;
111 test(s);
112
113 s.assign(10, 'a');
114 s.erase(5);
115 test(s);
116
117 s.assign(100, 'a');
118 s.erase(50);
119 test(s);
120 }
121 {
122 S s;
123 test(s, 5);
124 test(s, 10);
125 test(s, 50);
126 }
127 {
128 S s(100, 'a');
129 s.erase(50);
130 test(s, 5);
131 test(s, 10);
132 test(s, 50);
133 test(s, 100);
Marshall Clow4d64d7d2018-11-28 18:18:34 +0000134 test(s, 1000);
Howard Hinnanteec72182013-06-28 16:59:19 +0000135 test(s, S::npos);
136 }
137 }
138#endif
JF Bastien2df59c52019-02-04 20:31:13 +0000139
140 return 0;
Howard Hinnant3e519522010-05-11 19:42:16 +0000141}