xref: /llvm-project/libcxx/test/std/strings/basic.string/string.nonmembers/string_op+/string_char.pass.cpp (revision 9ed20568e7de53dce85f1631d7d8c1415e7930ae)
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, charT rhs); // constexpr since C++20
14 
15 // template<class charT, class traits, class Allocator>
16 //   basic_string<charT,traits,Allocator>&&
17 //   operator+(basic_string<charT,traits,Allocator>&& lhs, charT rhs); // constexpr since C++20
18 
19 #include <string>
20 #include <utility>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "min_allocator.h"
25 #include "asan_testing.h"
26 
27 template <class S>
test0(const S & lhs,typename S::value_type rhs,const S & x)28 TEST_CONSTEXPR_CXX20 void test0(const S& lhs, typename S::value_type rhs, const S& x) {
29   assert(lhs + rhs == x);
30   LIBCPP_ASSERT(is_string_asan_correct(lhs + rhs));
31 }
32 
33 #if TEST_STD_VER >= 11
34 template <class S>
test1(S && lhs,typename S::value_type rhs,const S & x)35 TEST_CONSTEXPR_CXX20 void test1(S&& lhs, typename S::value_type rhs, const S& x) {
36   assert(std::move(lhs) + rhs == x);
37 }
38 #endif
39 
40 template <class S>
test_string()41 TEST_CONSTEXPR_CXX20 void test_string() {
42   test0(S(""), '1', S("1"));
43   test0(S(""), '1', S("1"));
44   test0(S("abcde"), '1', S("abcde1"));
45   test0(S("abcdefghij"), '1', S("abcdefghij1"));
46   test0(S("abcdefghijklmnopqrst"), '1', S("abcdefghijklmnopqrst1"));
47 
48 #if TEST_STD_VER >= 11
49   test1(S(""), '1', S("1"));
50   test1(S("abcde"), '1', S("abcde1"));
51   test1(S("abcdefghij"), '1', S("abcdefghij1"));
52   test1(S("abcdefghijklmnopqrst"), '1', S("abcdefghijklmnopqrst1"));
53 #endif
54 }
55 
test()56 TEST_CONSTEXPR_CXX20 bool test() {
57   test_string<std::string>();
58 #if TEST_STD_VER >= 11
59   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
60   test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char> > >();
61 #endif
62 
63   return true;
64 }
65 
main(int,char **)66 int main(int, char**) {
67   test();
68 #if TEST_STD_VER > 17
69   static_assert(test());
70 #endif
71 
72   return 0;
73 }
74