substr.pass.cpp
7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string(const basic_string<charT,traits,Allocator>& str,
// size_type pos, size_type n,
// const Allocator& a = Allocator());
//
// basic_string(const basic_string<charT,traits,Allocator>& str,
// size_type pos,
// const Allocator& a = Allocator());
// When back-deploying to macosx10.7, the RTTI for exception classes
// incorrectly provided by libc++.dylib is mixed with the one in
// libc++abi.dylib and exceptions are not caught properly.
// XFAIL: with_system_cxx_lib=macosx10.7
#include <string>
#include <stdexcept>
#include <algorithm>
#include <vector>
#include <scoped_allocator>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class S>
void
test(S str, unsigned pos)
{
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
if (pos <= str.size())
{
S s2(str, pos);
LIBCPP_ASSERT(s2.__invariants());
typename S::size_type rlen = str.size() - pos;
assert(s2.size() == rlen);
assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
try
{
S s2(str, pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}
template <class S>
void
test(S str, unsigned pos, unsigned n)
{
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
if (pos <= str.size())
{
S s2(str, pos, n);
LIBCPP_ASSERT(s2.__invariants());
typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n);
assert(s2.size() == rlen);
assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
try
{
S s2(str, pos, n);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}
template <class S>
void
test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a)
{
typedef typename S::traits_type T;
if (pos <= str.size())
{
S s2(str, pos, n, a);
LIBCPP_ASSERT(s2.__invariants());
typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n);
assert(s2.size() == rlen);
assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
assert(s2.get_allocator() == a);
assert(s2.capacity() >= s2.size());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
try
{
S s2(str, pos, n, a);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}
#if TEST_STD_VER >= 11
#ifndef TEST_HAS_NO_EXCEPTIONS
void test2583()
{ // LWG #2583
typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > StringA;
std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA>>> vs;
StringA s{"1234"};
vs.emplace_back(s, 2);
try { vs.emplace_back(s, 5); }
catch (const std::out_of_range&) { return; }
assert(false);
}
#endif
#endif
int main(int, char**)
{
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A(3)), 0);
test(S(A(3)), 1);
test(S("1", A(5)), 0);
test(S("1", A(5)), 1);
test(S("1", A(5)), 2);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500);
test(S(A(3)), 0, 0);
test(S(A(3)), 0, 1);
test(S(A(3)), 1, 0);
test(S(A(3)), 1, 1);
test(S(A(3)), 1, 2);
test(S("1", A(5)), 0, 0);
test(S("1", A(5)), 0, 1);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100);
test(S(A(3)), 0, 0, A(4));
test(S(A(3)), 0, 1, A(4));
test(S(A(3)), 1, 0, A(4));
test(S(A(3)), 1, 1, A(4));
test(S(A(3)), 1, 2, A(4));
test(S("1", A(5)), 0, 0, A(6));
test(S("1", A(5)), 0, 1, A(6));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8));
}
#if TEST_STD_VER >= 11
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A()), 0);
test(S(A()), 1);
test(S("1", A()), 0);
test(S("1", A()), 1);
test(S("1", A()), 2);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500);
test(S(A()), 0, 0);
test(S(A()), 0, 1);
test(S(A()), 1, 0);
test(S(A()), 1, 1);
test(S(A()), 1, 2);
test(S("1", A()), 0, 0);
test(S("1", A()), 0, 1);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100);
test(S(A()), 0, 0, A());
test(S(A()), 0, 1, A());
test(S(A()), 1, 0, A());
test(S(A()), 1, 1, A());
test(S(A()), 1, 2, A());
test(S("1", A()), 0, 0, A());
test(S("1", A()), 0, 1, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
test2583();
#endif
#endif
return 0;
}