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