1 //===----------------------------------------------------------------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
11 // template<class InputIterator>
12 // basic_string& assign(InputIterator first, InputIterator last);
17 #include "test_macros.h"
18 #include "test_iterators.h"
19 #include "min_allocator.h"
21 template <class S
, class It
>
23 test(S s
, It first
, It last
, S expected
)
25 s
.assign(first
, last
);
26 LIBCPP_ASSERT(s
.__invariants());
27 assert(s
== expected
);
30 #ifndef TEST_HAS_NO_EXCEPTIONS
31 template <class S
, class It
>
33 test_exceptions(S s
, It first
, It last
)
37 s
.assign(first
, last
);
41 LIBCPP_ASSERT(s
.__invariants());
49 typedef std::string S
;
50 const char* s
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
52 test(S(), s
, s
+1, S("A"));
53 test(S(), s
, s
+10, S("ABCDEFGHIJ"));
54 test(S(), s
, s
+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
56 test(S("12345"), s
, s
, S());
57 test(S("12345"), s
, s
+1, S("A"));
58 test(S("12345"), s
, s
+10, S("ABCDEFGHIJ"));
59 test(S("12345"), s
, s
+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
61 test(S("1234567890"), s
, s
, S());
62 test(S("1234567890"), s
, s
+1, S("A"));
63 test(S("1234567890"), s
, s
+10, S("ABCDEFGHIJ"));
64 test(S("1234567890"), s
, s
+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
66 test(S("12345678901234567890"), s
, s
, S());
67 test(S("12345678901234567890"), s
, s
+1, S("A"));
68 test(S("12345678901234567890"), s
, s
+10, S("ABCDEFGHIJ"));
69 test(S("12345678901234567890"), s
, s
+52,
70 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
72 test(S(), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
), S());
73 test(S(), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+1), S("A"));
74 test(S(), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+10),
76 test(S(), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+52),
77 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
79 test(S("12345"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
),
81 test(S("12345"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+1),
83 test(S("12345"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+10),
85 test(S("12345"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+52),
86 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
88 test(S("1234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
),
90 test(S("1234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+1),
92 test(S("1234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+10),
94 test(S("1234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+52),
95 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
97 test(S("12345678901234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
),
99 test(S("12345678901234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+1),
101 test(S("12345678901234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+10),
103 test(S("12345678901234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+52),
104 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
106 #if TEST_STD_VER >= 11
108 typedef std::basic_string
<char, std::char_traits
<char>, min_allocator
<char>> S
;
109 const char* s
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
110 test(S(), s
, s
, S());
111 test(S(), s
, s
+1, S("A"));
112 test(S(), s
, s
+10, S("ABCDEFGHIJ"));
113 test(S(), s
, s
+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
115 test(S("12345"), s
, s
, S());
116 test(S("12345"), s
, s
+1, S("A"));
117 test(S("12345"), s
, s
+10, S("ABCDEFGHIJ"));
118 test(S("12345"), s
, s
+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
120 test(S("1234567890"), s
, s
, S());
121 test(S("1234567890"), s
, s
+1, S("A"));
122 test(S("1234567890"), s
, s
+10, S("ABCDEFGHIJ"));
123 test(S("1234567890"), s
, s
+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
125 test(S("12345678901234567890"), s
, s
, S());
126 test(S("12345678901234567890"), s
, s
+1, S("A"));
127 test(S("12345678901234567890"), s
, s
+10, S("ABCDEFGHIJ"));
128 test(S("12345678901234567890"), s
, s
+52,
129 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
131 test(S(), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
), S());
132 test(S(), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+1), S("A"));
133 test(S(), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+10),
135 test(S(), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+52),
136 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
138 test(S("12345"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
),
140 test(S("12345"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+1),
142 test(S("12345"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+10),
144 test(S("12345"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+52),
145 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
147 test(S("1234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
),
149 test(S("1234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+1),
151 test(S("1234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+10),
153 test(S("1234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+52),
154 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
156 test(S("12345678901234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
),
158 test(S("12345678901234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+1),
160 test(S("12345678901234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+10),
162 test(S("12345678901234567890"), input_iterator
<const char*>(s
), input_iterator
<const char*>(s
+52),
163 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
166 #ifndef TEST_HAS_NO_EXCEPTIONS
167 { // test iterator operations that throw
168 typedef std::string S
;
169 typedef ThrowingIterator
<char> TIter
;
170 typedef input_iterator
<TIter
> IIter
;
171 const char* s
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
172 test_exceptions(S(), IIter(TIter(s
, s
+10, 4, TIter::TAIncrement
)), IIter());
173 test_exceptions(S(), IIter(TIter(s
, s
+10, 5, TIter::TADereference
)), IIter());
174 test_exceptions(S(), IIter(TIter(s
, s
+10, 6, TIter::TAComparison
)), IIter());
176 test_exceptions(S(), TIter(s
, s
+10, 4, TIter::TAIncrement
), TIter());
177 test_exceptions(S(), TIter(s
, s
+10, 5, TIter::TADereference
), TIter());
178 test_exceptions(S(), TIter(s
, s
+10, 6, TIter::TAComparison
), TIter());
182 { // test assigning to self
183 typedef std::string S
;
185 S s_long
= "Lorem ipsum dolor sit amet, consectetur/";
187 s_short
.assign(s_short
.begin(), s_short
.end());
188 assert(s_short
== "123/");
189 s_short
.assign(s_short
.begin() + 2, s_short
.end());
190 assert(s_short
== "3/");
192 s_long
.assign(s_long
.begin(), s_long
.end());
193 assert(s_long
== "Lorem ipsum dolor sit amet, consectetur/");
195 s_long
.assign(s_long
.begin() + 30, s_long
.end());
196 assert(s_long
== "nsectetur/");
199 { // test assigning a different type
200 typedef std::string S
;
201 const uint8_t p
[] = "ABCD";