]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/strings/basic.string/string.modifiers/string_append/T_size_size.pass.cpp
Vendor import of libc++ trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / std / strings / basic.string / string.modifiers / string_append / T_size_size.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 T>
13 //    basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
14
15 #include <string>
16 #include <string>
17 #include <stdexcept>
18 #include <cassert>
19
20 #include "test_macros.h"
21 #include "min_allocator.h"
22
23 template <class S, class SV>
24 void
25 test(S s, SV sv, typename S::size_type pos, typename S::size_type n, S expected)
26 {
27     if (pos <= sv.size())
28     {
29         s.append(sv, pos, n);
30         LIBCPP_ASSERT(s.__invariants());
31         assert(s == expected);
32     }
33 #ifndef TEST_HAS_NO_EXCEPTIONS
34     else
35     {
36         try
37         {
38             s.append(sv, pos, n);
39             assert(false);
40         }
41         catch (std::out_of_range&)
42         {
43             assert(pos > sv.size());
44         }
45     }
46 #endif
47 }
48
49 template <class S, class SV>
50 void
51 test_npos(S s, SV sv, typename S::size_type pos, S expected)
52 {
53     if (pos <= sv.size())
54     {
55         s.append(sv, pos);
56         LIBCPP_ASSERT(s.__invariants());
57         assert(s == expected);
58     }
59 #ifndef TEST_HAS_NO_EXCEPTIONS
60     else
61     {
62         try
63         {
64             s.append(sv, pos);
65             assert(false);
66         }
67         catch (std::out_of_range&)
68         {
69             assert(pos > sv.size());
70         }
71     }
72 #endif
73 }
74
75 int main()
76 {
77     {
78     typedef std::string S;
79     typedef std::string_view SV;
80     test(S(), SV(), 0, 0, S());
81     test(S(), SV(), 1, 0, S());
82     test(S(), SV("12345"), 0, 3, S("123"));
83     test(S(), SV("12345"), 1, 4, S("2345"));
84     test(S(), SV("12345"), 3, 15, S("45"));
85     test(S(), SV("12345"), 5, 15, S(""));
86     test(S(), SV("12345"), 6, 15, S("not happening"));
87     test(S(), SV("12345678901234567890"), 0, 0, S());
88     test(S(), SV("12345678901234567890"), 1, 1, S("2"));
89     test(S(), SV("12345678901234567890"), 2, 3, S("345"));
90     test(S(), SV("12345678901234567890"), 12, 13, S("34567890"));
91     test(S(), SV("12345678901234567890"), 21, 13, S("not happening"));
92
93     test(S("12345"), SV(), 0, 0, S("12345"));
94     test(S("12345"), SV("12345"), 2, 2, S("1234534"));
95     test(S("12345"), SV("1234567890"), 0, 100, S("123451234567890"));
96
97     test(S("12345678901234567890"), SV(), 0, 0, S("12345678901234567890"));
98     test(S("12345678901234567890"), SV("12345"), 1, 3, S("12345678901234567890234"));
99     test(S("12345678901234567890"), SV("12345678901234567890"), 5, 10,
100          S("123456789012345678906789012345"));
101     }
102 #if TEST_STD_VER >= 11
103     {
104     typedef std::basic_string     <char, std::char_traits<char>, min_allocator<char>> S;
105     typedef std::basic_string_view<char, std::char_traits<char> > SV;
106     test(S(), SV(), 0, 0, S());
107     test(S(), SV(), 1, 0, S());
108     test(S(), SV("12345"), 0, 3, S("123"));
109     test(S(), SV("12345"), 1, 4, S("2345"));
110     test(S(), SV("12345"), 3, 15, S("45"));
111     test(S(), SV("12345"), 5, 15, S(""));
112     test(S(), SV("12345"), 6, 15, S("not happening"));
113     test(S(), SV("12345678901234567890"), 0, 0, S());
114     test(S(), SV("12345678901234567890"), 1, 1, S("2"));
115     test(S(), SV("12345678901234567890"), 2, 3, S("345"));
116     test(S(), SV("12345678901234567890"), 12, 13, S("34567890"));
117     test(S(), SV("12345678901234567890"), 21, 13, S("not happening"));
118
119     test(S("12345"), SV(), 0, 0, S("12345"));
120     test(S("12345"), SV("12345"), 2, 2, S("1234534"));
121     test(S("12345"), SV("1234567890"), 0, 100, S("123451234567890"));
122
123     test(S("12345678901234567890"), SV(), 0, 0, S("12345678901234567890"));
124     test(S("12345678901234567890"), SV("12345"), 1, 3, S("12345678901234567890234"));
125     test(S("12345678901234567890"), SV("12345678901234567890"), 5, 10,
126          S("123456789012345678906789012345"));
127     }
128 #endif
129     {
130     typedef std::string S;
131     typedef std::string_view SV;
132     test_npos(S(), SV(), 0, S());
133     test_npos(S(), SV(), 1, S());
134     test_npos(S(), SV("12345"), 0, S("12345"));
135     test_npos(S(), SV("12345"), 1, S("2345"));
136     test_npos(S(), SV("12345"), 3, S("45"));
137     test_npos(S(), SV("12345"), 5, S(""));
138     test_npos(S(), SV("12345"), 6, S("not happening"));
139     }
140
141     {
142     std::string s;
143     std::string_view sv = "EFGH";
144     char arr[] = "IJKL";
145
146     s.append("CDEF", 0);    // calls append(const char *, len)
147     assert(s == "");
148     s.clear();
149
150     s.append("QRST", 0, std::string::npos); // calls append(string("QRST"), pos, npos)
151     assert(s == "QRST");
152     s.clear();
153
154     s.append(sv, 0);  // calls append(T, pos, npos)
155     assert(s == sv);
156     s.clear();
157
158     s.append(sv, 0, std::string::npos);   // calls append(T, pos, npos)
159     assert(s == sv);
160     s.clear();
161
162     s.append(arr, 0);     // calls append(const char *, len)
163     assert(s == "");
164     s.clear();
165
166     s.append(arr, 0, std::string::npos);    // calls append(string("IJKL"), pos, npos)
167     assert(s == "IJKL");
168     s.clear();
169
170     s.append(arr, 0);     // calls append(const char *, len)
171     assert(s == "");
172     s.clear();
173     }
174
175     {
176     std::string s = "ABCD";
177     std::string_view sv = s;
178     s.append(sv);
179     assert(s == "ABCDABCD");
180
181     sv = s;
182     s.append(sv, 0, std::string::npos);
183     assert(s == "ABCDABCDABCDABCD");
184
185     sv = s;
186     s.append(sv, sv.size());
187     assert(s == "ABCDABCDABCDABCD");
188     }
189
190     {
191     std::string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
192     std::string_view sv = s;
193     s.append(sv);
194     assert(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
195
196     sv = s;
197     s.append(sv, 0, std::string::npos);
198     assert(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
199     }
200 }