xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp (revision 6e1dcc9335116f650d68cdbed12bbb34a99b2d9b)
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <string>
10 
11 // template<class InputIterator>
12 //   basic_string& assign(InputIterator first, InputIterator last); // constexpr since C++20
13 
14 #include <string>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 #include "test_iterators.h"
19 #include "min_allocator.h"
20 
21 template <class S, class It>
22 TEST_CONSTEXPR_CXX20 void test(S s, It first, It last, S expected) {
23   s.assign(first, last);
24   LIBCPP_ASSERT(s.__invariants());
25   assert(s == expected);
26 }
27 
28 #ifndef TEST_HAS_NO_EXCEPTIONS
29 struct Widget {
30   operator char() const { throw 42; }
31 };
32 
33 template <class S, class It>
34 void test_exceptions(S s, It first, It last) {
35   S original                 = s;
36   typename S::iterator begin = s.begin();
37   typename S::iterator end   = s.end();
38 
39   try {
40     s.assign(first, last);
41     assert(false);
42   } catch (...) {
43   }
44 
45   // Part of "no effects" is that iterators and pointers
46   // into the string must not have been invalidated.
47   LIBCPP_ASSERT(s.__invariants());
48   assert(s == original);
49   assert(s.begin() == begin);
50   assert(s.end() == end);
51 }
52 #endif
53 
54 template <class S>
55 TEST_CONSTEXPR_CXX20 void test_string() {
56   {
57     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
58     test(S(), s, s, S());
59     test(S(), s, s + 1, S("A"));
60     test(S(), s, s + 10, S("ABCDEFGHIJ"));
61     test(S(), s, s + 52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
62 
63     test(S("12345"), s, s, S());
64     test(S("12345"), s, s + 1, S("A"));
65     test(S("12345"), s, s + 10, S("ABCDEFGHIJ"));
66     test(S("12345"), s, s + 52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
67 
68     test(S("1234567890"), s, s, S());
69     test(S("1234567890"), s, s + 1, S("A"));
70     test(S("1234567890"), s, s + 10, S("ABCDEFGHIJ"));
71     test(S("1234567890"), s, s + 52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
72 
73     test(S("12345678901234567890"), s, s, S());
74     test(S("12345678901234567890"), s, s + 1, S("A"));
75     test(S("12345678901234567890"), s, s + 10, S("ABCDEFGHIJ"));
76     test(S("12345678901234567890"), s, s + 52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
77 
78     test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
79     test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 1), S("A"));
80     test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 10), S("ABCDEFGHIJ"));
81     test(S(),
82          cpp17_input_iterator<const char*>(s),
83          cpp17_input_iterator<const char*>(s + 52),
84          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
85 
86     test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
87     test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 1), S("A"));
88     test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 10), S("ABCDEFGHIJ"));
89     test(S("12345"),
90          cpp17_input_iterator<const char*>(s),
91          cpp17_input_iterator<const char*>(s + 52),
92          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
93 
94     test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
95     test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 1), S("A"));
96     test(S("1234567890"),
97          cpp17_input_iterator<const char*>(s),
98          cpp17_input_iterator<const char*>(s + 10),
99          S("ABCDEFGHIJ"));
100     test(S("1234567890"),
101          cpp17_input_iterator<const char*>(s),
102          cpp17_input_iterator<const char*>(s + 52),
103          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
104 
105     test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
106     test(S("12345678901234567890"),
107          cpp17_input_iterator<const char*>(s),
108          cpp17_input_iterator<const char*>(s + 1),
109          S("A"));
110     test(S("12345678901234567890"),
111          cpp17_input_iterator<const char*>(s),
112          cpp17_input_iterator<const char*>(s + 10),
113          S("ABCDEFGHIJ"));
114     test(S("12345678901234567890"),
115          cpp17_input_iterator<const char*>(s),
116          cpp17_input_iterator<const char*>(s + 52),
117          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
118   }
119 
120 #ifndef TEST_HAS_NO_EXCEPTIONS
121   if (!TEST_IS_CONSTANT_EVALUATED) { // test iterator operations that throw
122     typedef ThrowingIterator<char> TIter;
123     typedef cpp17_input_iterator<TIter> IIter;
124     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
125     test_exceptions(S(), IIter(TIter(s, s + 10, 4, TIter::TAIncrement)), IIter(TIter()));
126     test_exceptions(S(), IIter(TIter(s, s + 10, 5, TIter::TADereference)), IIter(TIter()));
127     test_exceptions(S(), IIter(TIter(s, s + 10, 6, TIter::TAComparison)), IIter(TIter()));
128 
129     test_exceptions(S(), TIter(s, s + 10, 4, TIter::TAIncrement), TIter());
130     test_exceptions(S(), TIter(s, s + 10, 5, TIter::TADereference), TIter());
131     test_exceptions(S(), TIter(s, s + 10, 6, TIter::TAComparison), TIter());
132 
133     Widget w[100];
134     test_exceptions(S(), w, w + 100);
135   }
136 #endif
137 
138   { // test assigning to self
139     S s_short = "123/";
140     S s_long  = "Lorem ipsum dolor sit amet, consectetur/";
141 
142     s_short.assign(s_short.begin(), s_short.end());
143     assert(s_short == "123/");
144     s_short.assign(s_short.begin() + 2, s_short.end());
145     assert(s_short == "3/");
146 
147     s_long.assign(s_long.begin(), s_long.end());
148     assert(s_long == "Lorem ipsum dolor sit amet, consectetur/");
149 
150     s_long.assign(s_long.begin() + 30, s_long.end());
151     assert(s_long == "nsectetur/");
152   }
153 
154   { // test assigning a different type
155     const std::uint8_t p[] = "ABCD";
156 
157     S s;
158     s.assign(p, p + 4);
159     assert(s == "ABCD");
160   }
161 
162   { // regression-test assigning to self in sneaky ways
163     S sneaky = "hello";
164     sneaky.resize(sneaky.capacity(), 'x');
165     S expected = sneaky + S(1, '\0');
166     test(sneaky, sneaky.data(), sneaky.data() + sneaky.size() + 1, expected);
167   }
168 }
169 
170 TEST_CONSTEXPR_CXX20 bool test() {
171   test_string<std::string>();
172 #if TEST_STD_VER >= 11
173   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
174 #endif
175 
176   return true;
177 }
178 
179 int main(int, char**) {
180   test();
181 #if TEST_STD_VER > 17
182   static_assert(test());
183 #endif
184 
185   return 0;
186 }
187