xref: /llvm-project/libcxx/test/std/strings/basic.string/string.nonmembers/string_op+/string_string.pass.cpp (revision 7853371146d1f40513d5dfe5bc6b3f958cda97af)
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 charT, class traits, class Allocator>
12 //   basic_string<charT,traits,Allocator>
13 //   operator+(const basic_string<charT,traits,Allocator>& lhs,
14 //             const basic_string<charT,traits,Allocator>& rhs);
15 
16 // template<class charT, class traits, class Allocator>
17 //   basic_string<charT,traits,Allocator>&&
18 //   operator+(const basic_string<charT,traits,Allocator>&& lhs,
19 //             const basic_string<charT,traits,Allocator>& rhs);
20 
21 // template<class charT, class traits, class Allocator>
22 //   basic_string<charT,traits,Allocator>&&
23 //   operator+(const basic_string<charT,traits,Allocator>& lhs,
24 //             const basic_string<charT,traits,Allocator>&& rhs);
25 
26 // template<class charT, class traits, class Allocator>
27 //   basic_string<charT,traits,Allocator>&&
28 //   operator+(const basic_string<charT,traits,Allocator>&& lhs,
29 //             const basic_string<charT,traits,Allocator>&& rhs);
30 
31 #include <string>
32 #include <utility>
33 #include <cassert>
34 
35 #include "test_macros.h"
36 #include "min_allocator.h"
37 
38 template <class S>
39 TEST_CONSTEXPR_CXX20 void test0(const S& lhs, const S& rhs, const S& x) {
40   assert(lhs + rhs == x);
41 }
42 
43 #if TEST_STD_VER >= 11
44 template <class S>
45 TEST_CONSTEXPR_CXX20 void test1(S&& lhs, const S& rhs, const S& x) {
46   assert(std::move(lhs) + rhs == x);
47 }
48 
49 template <class S>
50 TEST_CONSTEXPR_CXX20 void test2(const S& lhs, S&& rhs, const S& x) {
51   assert(lhs + std::move(rhs) == x);
52 }
53 
54 template <class S>
55 TEST_CONSTEXPR_CXX20 void test3(S&& lhs, S&& rhs, const S& x) {
56   assert(std::move(lhs) + std::move(rhs) == x);
57 }
58 #endif
59 
60 bool test() {
61   {
62     typedef std::string S;
63     test0(S(""), S(""), S(""));
64     test0(S(""), S("12345"), S("12345"));
65     test0(S(""), S("1234567890"), S("1234567890"));
66     test0(S(""), S("12345678901234567890"), S("12345678901234567890"));
67     test0(S("abcde"), S(""), S("abcde"));
68     test0(S("abcde"), S("12345"), S("abcde12345"));
69     test0(S("abcde"), S("1234567890"), S("abcde1234567890"));
70     test0(S("abcde"), S("12345678901234567890"),
71           S("abcde12345678901234567890"));
72     test0(S("abcdefghij"), S(""), S("abcdefghij"));
73     test0(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
74     test0(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
75     test0(S("abcdefghij"), S("12345678901234567890"),
76           S("abcdefghij12345678901234567890"));
77     test0(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
78     test0(S("abcdefghijklmnopqrst"), S("12345"),
79           S("abcdefghijklmnopqrst12345"));
80     test0(S("abcdefghijklmnopqrst"), S("1234567890"),
81           S("abcdefghijklmnopqrst1234567890"));
82     test0(S("abcdefghijklmnopqrst"), S("12345678901234567890"),
83           S("abcdefghijklmnopqrst12345678901234567890"));
84   }
85 #if TEST_STD_VER >= 11
86   {
87     typedef std::string S;
88     test1(S(""), S(""), S(""));
89     test1(S(""), S("12345"), S("12345"));
90     test1(S(""), S("1234567890"), S("1234567890"));
91     test1(S(""), S("12345678901234567890"), S("12345678901234567890"));
92     test1(S("abcde"), S(""), S("abcde"));
93     test1(S("abcde"), S("12345"), S("abcde12345"));
94     test1(S("abcde"), S("1234567890"), S("abcde1234567890"));
95     test1(S("abcde"), S("12345678901234567890"),
96           S("abcde12345678901234567890"));
97     test1(S("abcdefghij"), S(""), S("abcdefghij"));
98     test1(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
99     test1(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
100     test1(S("abcdefghij"), S("12345678901234567890"),
101           S("abcdefghij12345678901234567890"));
102     test1(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
103     test1(S("abcdefghijklmnopqrst"), S("12345"),
104           S("abcdefghijklmnopqrst12345"));
105     test1(S("abcdefghijklmnopqrst"), S("1234567890"),
106           S("abcdefghijklmnopqrst1234567890"));
107     test1(S("abcdefghijklmnopqrst"), S("12345678901234567890"),
108           S("abcdefghijklmnopqrst12345678901234567890"));
109 
110     test2(S(""), S(""), S(""));
111     test2(S(""), S("12345"), S("12345"));
112     test2(S(""), S("1234567890"), S("1234567890"));
113     test2(S(""), S("12345678901234567890"), S("12345678901234567890"));
114     test2(S("abcde"), S(""), S("abcde"));
115     test2(S("abcde"), S("12345"), S("abcde12345"));
116     test2(S("abcde"), S("1234567890"), S("abcde1234567890"));
117     test2(S("abcde"), S("12345678901234567890"),
118           S("abcde12345678901234567890"));
119     test2(S("abcdefghij"), S(""), S("abcdefghij"));
120     test2(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
121     test2(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
122     test2(S("abcdefghij"), S("12345678901234567890"),
123           S("abcdefghij12345678901234567890"));
124     test2(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
125     test2(S("abcdefghijklmnopqrst"), S("12345"),
126           S("abcdefghijklmnopqrst12345"));
127     test2(S("abcdefghijklmnopqrst"), S("1234567890"),
128           S("abcdefghijklmnopqrst1234567890"));
129     test2(S("abcdefghijklmnopqrst"), S("12345678901234567890"),
130           S("abcdefghijklmnopqrst12345678901234567890"));
131 
132     test3(S(""), S(""), S(""));
133     test3(S(""), S("12345"), S("12345"));
134     test3(S(""), S("1234567890"), S("1234567890"));
135     test3(S(""), S("12345678901234567890"), S("12345678901234567890"));
136     test3(S("abcde"), S(""), S("abcde"));
137     test3(S("abcde"), S("12345"), S("abcde12345"));
138     test3(S("abcde"), S("1234567890"), S("abcde1234567890"));
139     test3(S("abcde"), S("12345678901234567890"),
140           S("abcde12345678901234567890"));
141     test3(S("abcdefghij"), S(""), S("abcdefghij"));
142     test3(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
143     test3(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
144     test3(S("abcdefghij"), S("12345678901234567890"),
145           S("abcdefghij12345678901234567890"));
146     test3(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
147     test3(S("abcdefghijklmnopqrst"), S("12345"),
148           S("abcdefghijklmnopqrst12345"));
149     test3(S("abcdefghijklmnopqrst"), S("1234567890"),
150           S("abcdefghijklmnopqrst1234567890"));
151     test3(S("abcdefghijklmnopqrst"), S("12345678901234567890"),
152           S("abcdefghijklmnopqrst12345678901234567890"));
153   }
154   {
155     typedef std::basic_string<char, std::char_traits<char>,
156                               min_allocator<char> >
157         S;
158     test0(S(""), S(""), S(""));
159     test0(S(""), S("12345"), S("12345"));
160     test0(S(""), S("1234567890"), S("1234567890"));
161     test0(S(""), S("12345678901234567890"), S("12345678901234567890"));
162     test0(S("abcde"), S(""), S("abcde"));
163     test0(S("abcde"), S("12345"), S("abcde12345"));
164     test0(S("abcde"), S("1234567890"), S("abcde1234567890"));
165     test0(S("abcde"), S("12345678901234567890"),
166           S("abcde12345678901234567890"));
167     test0(S("abcdefghij"), S(""), S("abcdefghij"));
168     test0(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
169     test0(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
170     test0(S("abcdefghij"), S("12345678901234567890"),
171           S("abcdefghij12345678901234567890"));
172     test0(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
173     test0(S("abcdefghijklmnopqrst"), S("12345"),
174           S("abcdefghijklmnopqrst12345"));
175     test0(S("abcdefghijklmnopqrst"), S("1234567890"),
176           S("abcdefghijklmnopqrst1234567890"));
177     test0(S("abcdefghijklmnopqrst"), S("12345678901234567890"),
178           S("abcdefghijklmnopqrst12345678901234567890"));
179 
180     test1(S(""), S(""), S(""));
181     test1(S(""), S("12345"), S("12345"));
182     test1(S(""), S("1234567890"), S("1234567890"));
183     test1(S(""), S("12345678901234567890"), S("12345678901234567890"));
184     test1(S("abcde"), S(""), S("abcde"));
185     test1(S("abcde"), S("12345"), S("abcde12345"));
186     test1(S("abcde"), S("1234567890"), S("abcde1234567890"));
187     test1(S("abcde"), S("12345678901234567890"),
188           S("abcde12345678901234567890"));
189     test1(S("abcdefghij"), S(""), S("abcdefghij"));
190     test1(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
191     test1(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
192     test1(S("abcdefghij"), S("12345678901234567890"),
193           S("abcdefghij12345678901234567890"));
194     test1(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
195     test1(S("abcdefghijklmnopqrst"), S("12345"),
196           S("abcdefghijklmnopqrst12345"));
197     test1(S("abcdefghijklmnopqrst"), S("1234567890"),
198           S("abcdefghijklmnopqrst1234567890"));
199     test1(S("abcdefghijklmnopqrst"), S("12345678901234567890"),
200           S("abcdefghijklmnopqrst12345678901234567890"));
201 
202     test2(S(""), S(""), S(""));
203     test2(S(""), S("12345"), S("12345"));
204     test2(S(""), S("1234567890"), S("1234567890"));
205     test2(S(""), S("12345678901234567890"), S("12345678901234567890"));
206     test2(S("abcde"), S(""), S("abcde"));
207     test2(S("abcde"), S("12345"), S("abcde12345"));
208     test2(S("abcde"), S("1234567890"), S("abcde1234567890"));
209     test2(S("abcde"), S("12345678901234567890"),
210           S("abcde12345678901234567890"));
211     test2(S("abcdefghij"), S(""), S("abcdefghij"));
212     test2(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
213     test2(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
214     test2(S("abcdefghij"), S("12345678901234567890"),
215           S("abcdefghij12345678901234567890"));
216     test2(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
217     test2(S("abcdefghijklmnopqrst"), S("12345"),
218           S("abcdefghijklmnopqrst12345"));
219     test2(S("abcdefghijklmnopqrst"), S("1234567890"),
220           S("abcdefghijklmnopqrst1234567890"));
221     test2(S("abcdefghijklmnopqrst"), S("12345678901234567890"),
222           S("abcdefghijklmnopqrst12345678901234567890"));
223 
224     test3(S(""), S(""), S(""));
225     test3(S(""), S("12345"), S("12345"));
226     test3(S(""), S("1234567890"), S("1234567890"));
227     test3(S(""), S("12345678901234567890"), S("12345678901234567890"));
228     test3(S("abcde"), S(""), S("abcde"));
229     test3(S("abcde"), S("12345"), S("abcde12345"));
230     test3(S("abcde"), S("1234567890"), S("abcde1234567890"));
231     test3(S("abcde"), S("12345678901234567890"),
232           S("abcde12345678901234567890"));
233     test3(S("abcdefghij"), S(""), S("abcdefghij"));
234     test3(S("abcdefghij"), S("12345"), S("abcdefghij12345"));
235     test3(S("abcdefghij"), S("1234567890"), S("abcdefghij1234567890"));
236     test3(S("abcdefghij"), S("12345678901234567890"),
237           S("abcdefghij12345678901234567890"));
238     test3(S("abcdefghijklmnopqrst"), S(""), S("abcdefghijklmnopqrst"));
239     test3(S("abcdefghijklmnopqrst"), S("12345"),
240           S("abcdefghijklmnopqrst12345"));
241     test3(S("abcdefghijklmnopqrst"), S("1234567890"),
242           S("abcdefghijklmnopqrst1234567890"));
243     test3(S("abcdefghijklmnopqrst"), S("12345678901234567890"),
244           S("abcdefghijklmnopqrst12345678901234567890"));
245   }
246 #endif // TEST_STD_VER >= 11
247 
248   return true;
249 }
250 
251 int main(int, char**) {
252   test();
253 #if TEST_STD_VER > 17
254   // static_assert(test());
255 #endif
256 
257   return 0;
258 }
259