]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / strings / basic.string / string.modifiers / string_assign / iterator.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <string>
11
12 // template<class InputIterator>
13 //   basic_string& assign(InputIterator first, InputIterator last);
14
15 #include <string>
16 #include <cassert>
17
18 #include "test_macros.h"
19 #include "test_iterators.h"
20 #include "min_allocator.h"
21
22 template <class S, class It>
23 void
24 test(S s, It first, It last, S expected)
25 {
26     s.assign(first, last);
27     LIBCPP_ASSERT(s.__invariants());
28     assert(s == expected);
29 }
30
31 #ifndef TEST_HAS_NO_EXCEPTIONS
32 template <class S, class It>
33 void
34 test_exceptions(S s, It first, It last)
35 {
36         S aCopy = s;
37     try {
38             s.assign(first, last);
39         assert(false);
40     }
41     catch (...) {}
42     LIBCPP_ASSERT(s.__invariants());
43     assert(s == aCopy);
44 }
45 #endif
46
47 int main()
48 {
49     {
50     typedef std::string S;
51     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
52     test(S(), s, s, S());
53     test(S(), s, s+1, S("A"));
54     test(S(), s, s+10, S("ABCDEFGHIJ"));
55     test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
56
57     test(S("12345"), s, s, S());
58     test(S("12345"), s, s+1, S("A"));
59     test(S("12345"), s, s+10, S("ABCDEFGHIJ"));
60     test(S("12345"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
61
62     test(S("1234567890"), s, s, S());
63     test(S("1234567890"), s, s+1, S("A"));
64     test(S("1234567890"), s, s+10, S("ABCDEFGHIJ"));
65     test(S("1234567890"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
66
67     test(S("12345678901234567890"), s, s, S());
68     test(S("12345678901234567890"), s, s+1, S("A"));
69     test(S("12345678901234567890"), s, s+10, S("ABCDEFGHIJ"));
70     test(S("12345678901234567890"), s, s+52,
71          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
72
73     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s), S());
74     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("A"));
75     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
76          S("ABCDEFGHIJ"));
77     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
78          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
79
80     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s),
81          S());
82     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
83          S("A"));
84     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
85          S("ABCDEFGHIJ"));
86     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
87          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
88
89     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
90          S());
91     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
92          S("A"));
93     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
94          S("ABCDEFGHIJ"));
95     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
96          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
97
98     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
99          S());
100     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
101          S("A"));
102     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
103          S("ABCDEFGHIJ"));
104     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
105          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
106     }
107 #if TEST_STD_VER >= 11
108     {
109     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
110     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
111     test(S(), s, s, S());
112     test(S(), s, s+1, S("A"));
113     test(S(), s, s+10, S("ABCDEFGHIJ"));
114     test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
115
116     test(S("12345"), s, s, S());
117     test(S("12345"), s, s+1, S("A"));
118     test(S("12345"), s, s+10, S("ABCDEFGHIJ"));
119     test(S("12345"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
120
121     test(S("1234567890"), s, s, S());
122     test(S("1234567890"), s, s+1, S("A"));
123     test(S("1234567890"), s, s+10, S("ABCDEFGHIJ"));
124     test(S("1234567890"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
125
126     test(S("12345678901234567890"), s, s, S());
127     test(S("12345678901234567890"), s, s+1, S("A"));
128     test(S("12345678901234567890"), s, s+10, S("ABCDEFGHIJ"));
129     test(S("12345678901234567890"), s, s+52,
130          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
131
132     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s), S());
133     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("A"));
134     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
135          S("ABCDEFGHIJ"));
136     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
137          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
138
139     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s),
140          S());
141     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
142          S("A"));
143     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
144          S("ABCDEFGHIJ"));
145     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
146          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
147
148     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
149          S());
150     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
151          S("A"));
152     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
153          S("ABCDEFGHIJ"));
154     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
155          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
156
157     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
158          S());
159     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
160          S("A"));
161     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
162          S("ABCDEFGHIJ"));
163     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
164          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
165     }
166 #endif
167 #ifndef TEST_HAS_NO_EXCEPTIONS
168         { // test iterator operations that throw
169     typedef std::string S;
170     typedef ThrowingIterator<char> TIter;
171     typedef input_iterator<TIter> IIter;
172     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
173     test_exceptions(S(), IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter());
174     test_exceptions(S(), IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter());
175     test_exceptions(S(), IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter());
176
177     test_exceptions(S(), TIter(s, s+10, 4, TIter::TAIncrement), TIter());
178     test_exceptions(S(), TIter(s, s+10, 5, TIter::TADereference), TIter());
179     test_exceptions(S(), TIter(s, s+10, 6, TIter::TAComparison), TIter());
180         }
181 #endif
182
183         { // test assigning to self
184     typedef std::string S;
185         S s_short = "123/";
186         S s_long  = "Lorem ipsum dolor sit amet, consectetur/";
187
188         s_short.assign(s_short.begin(), s_short.end());
189         assert(s_short == "123/");
190         s_short.assign(s_short.begin() + 2, s_short.end());
191         assert(s_short == "3/");
192
193         s_long.assign(s_long.begin(), s_long.end());
194         assert(s_long == "Lorem ipsum dolor sit amet, consectetur/");
195
196         s_long.assign(s_long.begin() + 30, s_long.end());
197         assert(s_long == "nsectetur/");
198         }
199
200         { // test assigning a different type
201     typedef std::string S;
202         const uint8_t p[] = "ABCD";
203
204         S s;
205         s.assign(p, p + 4);
206         assert(s == "ABCD");
207         }
208 }