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); // constexpr since C++20
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); // constexpr since C++20
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); // constexpr since C++20
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); // constexpr since C++20
30
31#include <cassert>
32#include <string>
33#include <utility>
34
35#include "asan_testing.h"
36#include "min_allocator.h"
37#include "test_macros.h"
38
39template <class S>
40TEST_CONSTEXPR_CXX20 void test_string() {
41 const char* test_data[2][4] = {
42 {"", "abcde", "abcdefghij", "abcdefghijklmnopqrst"}, {"", "12345", "1234567890", "12345678901234567890"}};
43
44 const char* results[4][4] = {
45 {"", "12345", "1234567890", "12345678901234567890"},
46 {"abcde", "abcde12345", "abcde1234567890", "abcde12345678901234567890"},
47 {"abcdefghij", "abcdefghij12345", "abcdefghij1234567890", "abcdefghij12345678901234567890"},
48 {"abcdefghijklmnopqrst",
49 "abcdefghijklmnopqrst12345",
50 "abcdefghijklmnopqrst1234567890",
51 "abcdefghijklmnopqrst12345678901234567890"}};
52
53 for (size_t i = 0; i != 4; ++i) {
54 for (size_t k = 0; k != 4; ++k) {
55 { // operator+(const string&, const string&);
56 const S lhs(test_data[0][i]);
57 const S rhs(test_data[1][k]);
58 assert(lhs + rhs == results[i][k]);
59 LIBCPP_ASSERT(is_string_asan_correct(lhs + rhs));
60 }
61#if TEST_STD_VER >= 11
62 { // operator+(string&&, const string&);
63 S lhs(test_data[0][i]);
64 const S rhs(test_data[1][k]);
65 assert(std::move(lhs) + rhs == results[i][k]);
66 }
67 { // operator+(const string&, string&&);
68 const S lhs(test_data[0][i]);
69 S rhs(test_data[1][k]);
70 assert(lhs + std::move(rhs) == results[i][k]);
71 }
72 { // operator+(string&&, string&&);
73 S lhs(test_data[0][i]);
74 S rhs(test_data[1][k]);
75 assert(std::move(lhs) + std::move(rhs) == results[i][k]);
76 }
77#endif
78 }
79 }
80}
81
82TEST_CONSTEXPR_CXX20 bool test() {
83 test_string<std::string>();
84#if TEST_STD_VER >= 11
85 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
86 test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();
87#endif
88
89 { // check that growing to max_size() works
90 using string_type = std::basic_string<char, std::char_traits<char>, tiny_size_allocator<29, char> >;
91 string_type lhs;
92 lhs.resize(lhs.max_size() - 1);
93
94 string_type rhs = "a";
95
96 string_type result = lhs + rhs;
97
98 assert(result.size() == result.max_size());
99 assert(result.back() == 'a');
100 assert(result.capacity() <= result.get_allocator().max_size());
101 }
102
103 return true;
104}
105
106int main(int, char**) {
107 test();
108#if TEST_STD_VER >= 20
109 static_assert(test());
110#endif
111
112 return 0;
113}
114

source code of libcxx/test/std/strings/basic.string/string.nonmembers/string_op+/string_string.pass.cpp