xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
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   const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
57   test(S(), s, s, S());
58   test(S(), s, s + 1, S("A"));
59   test(S(), s, s + 10, S("ABCDEFGHIJ"));
60   test(S(), s, s + 52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
61 
62   test(S("12345"), s, s, S());
63   test(S("12345"), s, s + 1, S("A"));
64   test(S("12345"), s, s + 10, S("ABCDEFGHIJ"));
65   test(S("12345"), s, s + 52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
66 
67   test(S("1234567890"), s, s, S());
68   test(S("1234567890"), s, s + 1, S("A"));
69   test(S("1234567890"), s, s + 10, S("ABCDEFGHIJ"));
70   test(S("1234567890"), s, s + 52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
71 
72   test(S("12345678901234567890"), s, s, S());
73   test(S("12345678901234567890"), s, s + 1, S("A"));
74   test(S("12345678901234567890"), s, s + 10, S("ABCDEFGHIJ"));
75   test(S("12345678901234567890"), s, s + 52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
76 
77   test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
78   test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 1), S("A"));
79   test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 10), S("ABCDEFGHIJ"));
80   test(S(),
81        cpp17_input_iterator<const char*>(s),
82        cpp17_input_iterator<const char*>(s + 52),
83        S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
84 
85   test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
86   test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 1), S("A"));
87   test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 10), S("ABCDEFGHIJ"));
88   test(S("12345"),
89        cpp17_input_iterator<const char*>(s),
90        cpp17_input_iterator<const char*>(s + 52),
91        S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
92 
93   test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
94   test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s + 1), S("A"));
95   test(S("1234567890"),
96        cpp17_input_iterator<const char*>(s),
97        cpp17_input_iterator<const char*>(s + 10),
98        S("ABCDEFGHIJ"));
99   test(S("1234567890"),
100        cpp17_input_iterator<const char*>(s),
101        cpp17_input_iterator<const char*>(s + 52),
102        S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
103 
104   test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
105   test(S("12345678901234567890"),
106        cpp17_input_iterator<const char*>(s),
107        cpp17_input_iterator<const char*>(s + 1),
108        S("A"));
109   test(S("12345678901234567890"),
110        cpp17_input_iterator<const char*>(s),
111        cpp17_input_iterator<const char*>(s + 10),
112        S("ABCDEFGHIJ"));
113   test(S("12345678901234567890"),
114        cpp17_input_iterator<const char*>(s),
115        cpp17_input_iterator<const char*>(s + 52),
116        S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
117 }
118 
119 TEST_CONSTEXPR_CXX20 bool test() {
120   test_string<std::string>();
121 #if TEST_STD_VER >= 11
122   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
123 #endif
124 
125 #ifndef TEST_HAS_NO_EXCEPTIONS
126   if (!TEST_IS_CONSTANT_EVALUATED) { // test iterator operations that throw
127     typedef std::string S;
128     typedef ThrowingIterator<char> TIter;
129     typedef cpp17_input_iterator<TIter> IIter;
130     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
131     test_exceptions(S(), IIter(TIter(s, s + 10, 4, TIter::TAIncrement)), IIter(TIter()));
132     test_exceptions(S(), IIter(TIter(s, s + 10, 5, TIter::TADereference)), IIter(TIter()));
133     test_exceptions(S(), IIter(TIter(s, s + 10, 6, TIter::TAComparison)), IIter(TIter()));
134 
135     test_exceptions(S(), TIter(s, s + 10, 4, TIter::TAIncrement), TIter());
136     test_exceptions(S(), TIter(s, s + 10, 5, TIter::TADereference), TIter());
137     test_exceptions(S(), TIter(s, s + 10, 6, TIter::TAComparison), TIter());
138 
139     Widget w[100];
140     test_exceptions(S(), w, w + 100);
141   }
142 #endif
143 
144   { // test assigning to self
145     typedef std::string S;
146     S s_short = "123/";
147     S s_long  = "Lorem ipsum dolor sit amet, consectetur/";
148 
149     s_short.assign(s_short.begin(), s_short.end());
150     assert(s_short == "123/");
151     s_short.assign(s_short.begin() + 2, s_short.end());
152     assert(s_short == "3/");
153 
154     s_long.assign(s_long.begin(), s_long.end());
155     assert(s_long == "Lorem ipsum dolor sit amet, consectetur/");
156 
157     s_long.assign(s_long.begin() + 30, s_long.end());
158     assert(s_long == "nsectetur/");
159   }
160 
161   { // test assigning a different type
162     typedef std::string S;
163     const std::uint8_t p[] = "ABCD";
164 
165     S s;
166     s.assign(p, p + 4);
167     assert(s == "ABCD");
168   }
169 
170   { // regression-test assigning to self in sneaky ways
171     std::string sneaky = "hello";
172     sneaky.resize(sneaky.capacity(), 'x');
173     std::string expected = sneaky + std::string(1, '\0');
174     test(sneaky, sneaky.data(), sneaky.data() + sneaky.size() + 1, expected);
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