]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/strings/basic.string/string.modifiers/string_insert/iter_iter_iter.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / strings / basic.string / string.modifiers / string_insert / iter_iter_iter.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 //   iterator insert(const_iterator p, InputIterator first, InputIterator last);
14
15 #if _LIBCPP_DEBUG >= 1
16 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
17 #endif
18
19 #include <string>
20 #include <cassert>
21
22 #include "test_iterators.h"
23 #include "min_allocator.h"
24
25 template <class S, class It>
26 void
27 test(S s, typename S::difference_type pos, It first, It last, S expected)
28 {
29     typename S::const_iterator p = s.cbegin() + pos;
30     typename S::iterator i = s.insert(p, first, last);
31     LIBCPP_ASSERT(s.__invariants());
32     assert(i - s.begin() == pos);
33     assert(s == expected);
34 }
35
36 #ifndef TEST_HAS_NO_EXCEPTIONS
37 template <class S, class It>
38 void
39 test_exceptions(S s, typename S::difference_type pos, It first, It last)
40 {
41     typename S::const_iterator p = s.cbegin() + pos;
42         S aCopy = s;
43     try {
44         s.insert(p, first, last);
45         assert(false);
46         }
47     catch (...) {}
48     LIBCPP_ASSERT(s.__invariants());
49     assert(s == aCopy);
50 }
51 #endif
52
53 int main()
54 {
55     {
56     typedef std::string S;
57     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
58     test(S(), 0, s, s, S());
59     test(S(), 0, s, s+1, S("A"));
60     test(S(), 0, s, s+10, S("ABCDEFGHIJ"));
61     test(S(), 0, s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
62
63     test(S("12345"), 0, s, s, S("12345"));
64     test(S("12345"), 1, s, s+1, S("1A2345"));
65     test(S("12345"), 4, s, s+10, S("1234ABCDEFGHIJ5"));
66     test(S("12345"), 5, s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
67
68     test(S("1234567890"), 0, s, s, S("1234567890"));
69     test(S("1234567890"), 1, s, s+1, S("1A234567890"));
70     test(S("1234567890"), 10, s, s+10, S("1234567890ABCDEFGHIJ"));
71     test(S("1234567890"), 8, s, s+52, S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90"));
72
73     test(S("12345678901234567890"), 3, s, s, S("12345678901234567890"));
74     test(S("12345678901234567890"), 3, s, s+1, S("123A45678901234567890"));
75     test(S("12345678901234567890"), 15, s, s+10, S("123456789012345ABCDEFGHIJ67890"));
76     test(S("12345678901234567890"), 20, s, s+52,
77          S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
78
79     test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S());
80     test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("A"));
81     test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("ABCDEFGHIJ"));
82     test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
83
84     test(S("12345"), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S("12345"));
85     test(S("12345"), 1, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("1A2345"));
86     test(S("12345"), 4, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("1234ABCDEFGHIJ5"));
87     test(S("12345"), 5, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
88
89     test(S("1234567890"), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S("1234567890"));
90     test(S("1234567890"), 1, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("1A234567890"));
91     test(S("1234567890"), 10, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("1234567890ABCDEFGHIJ"));
92     test(S("1234567890"), 8, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90"));
93
94     test(S("12345678901234567890"), 3, input_iterator<const char*>(s), input_iterator<const char*>(s), S("12345678901234567890"));
95     test(S("12345678901234567890"), 3, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("123A45678901234567890"));
96     test(S("12345678901234567890"), 15, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("123456789012345ABCDEFGHIJ67890"));
97     test(S("12345678901234567890"), 20, input_iterator<const char*>(s), input_iterator<const char*>(s+52),
98          S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
99     }
100 #if TEST_STD_VER >= 11
101     {
102     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
103     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
104     test(S(), 0, s, s, S());
105     test(S(), 0, s, s+1, S("A"));
106     test(S(), 0, s, s+10, S("ABCDEFGHIJ"));
107     test(S(), 0, s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
108
109     test(S("12345"), 0, s, s, S("12345"));
110     test(S("12345"), 1, s, s+1, S("1A2345"));
111     test(S("12345"), 4, s, s+10, S("1234ABCDEFGHIJ5"));
112     test(S("12345"), 5, s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
113
114     test(S("1234567890"), 0, s, s, S("1234567890"));
115     test(S("1234567890"), 1, s, s+1, S("1A234567890"));
116     test(S("1234567890"), 10, s, s+10, S("1234567890ABCDEFGHIJ"));
117     test(S("1234567890"), 8, s, s+52, S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90"));
118
119     test(S("12345678901234567890"), 3, s, s, S("12345678901234567890"));
120     test(S("12345678901234567890"), 3, s, s+1, S("123A45678901234567890"));
121     test(S("12345678901234567890"), 15, s, s+10, S("123456789012345ABCDEFGHIJ67890"));
122     test(S("12345678901234567890"), 20, s, s+52,
123          S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
124
125     test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S());
126     test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("A"));
127     test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("ABCDEFGHIJ"));
128     test(S(), 0, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
129
130     test(S("12345"), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S("12345"));
131     test(S("12345"), 1, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("1A2345"));
132     test(S("12345"), 4, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("1234ABCDEFGHIJ5"));
133     test(S("12345"), 5, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
134
135     test(S("1234567890"), 0, input_iterator<const char*>(s), input_iterator<const char*>(s), S("1234567890"));
136     test(S("1234567890"), 1, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("1A234567890"));
137     test(S("1234567890"), 10, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("1234567890ABCDEFGHIJ"));
138     test(S("1234567890"), 8, input_iterator<const char*>(s), input_iterator<const char*>(s+52), S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90"));
139
140     test(S("12345678901234567890"), 3, input_iterator<const char*>(s), input_iterator<const char*>(s), S("12345678901234567890"));
141     test(S("12345678901234567890"), 3, input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("123A45678901234567890"));
142     test(S("12345678901234567890"), 15, input_iterator<const char*>(s), input_iterator<const char*>(s+10), S("123456789012345ABCDEFGHIJ67890"));
143     test(S("12345678901234567890"), 20, input_iterator<const char*>(s), input_iterator<const char*>(s+52),
144          S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
145     }
146 #endif
147 #ifndef TEST_HAS_NO_EXCEPTIONS
148         { // test iterator operations that throw
149     typedef std::string S;
150     typedef ThrowingIterator<char> TIter;
151     typedef input_iterator<TIter> IIter;
152     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
153     test_exceptions(S(), 0, IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter());
154     test_exceptions(S(), 0, IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter());
155     test_exceptions(S(), 0, IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter());
156
157     test_exceptions(S(), 0, TIter(s, s+10, 4, TIter::TAIncrement), TIter());
158     test_exceptions(S(), 0, TIter(s, s+10, 5, TIter::TADereference), TIter());
159     test_exceptions(S(), 0, TIter(s, s+10, 6, TIter::TAComparison), TIter());
160         }
161 #endif
162 #if _LIBCPP_DEBUG >= 1
163     {
164         std::string v;
165         std::string v2;
166         char a[] = "123";
167         const int N = sizeof(a)/sizeof(a[0]);
168         std::string::iterator i = v.insert(v2.cbegin() + 10, a, a+N);
169         assert(false);
170     }
171 #endif
172
173         { // test inserting into self
174     typedef std::string S;
175         S s_short = "123/";
176         S s_long  = "Lorem ipsum dolor sit amet, consectetur/";
177
178         s_short.insert(s_short.begin(), s_short.begin(), s_short.end());
179         assert(s_short == "123/123/");
180         s_short.insert(s_short.begin(), s_short.begin(), s_short.end());
181         assert(s_short == "123/123/123/123/");
182         s_short.insert(s_short.begin(), s_short.begin(), s_short.end());
183         assert(s_short == "123/123/123/123/123/123/123/123/");
184
185         s_long.insert(s_long.begin(), s_long.begin(), s_long.end());
186         assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
187         }
188
189         { // test assigning a different type
190     typedef std::string S;
191         const uint8_t p[] = "ABCD";
192
193         S s;
194         s.insert(s.begin(), p, p + 4);
195         assert(s == "ABCD");
196         }
197 }