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+(charT lhs, const basic_string<charT,traits,Allocator>& rhs); // constexpr since C++20
14
15// template<class charT, class traits, class Allocator>
16// basic_string<charT,traits,Allocator>&&
17// operator+(charT lhs, basic_string<charT,traits,Allocator>&& rhs); // constexpr since C++20
18
19#include <cassert>
20#include <string>
21#include <utility>
22
23#include "test_macros.h"
24#include "min_allocator.h"
25#include "asan_testing.h"
26
27template <class S>
28TEST_CONSTEXPR_CXX20 void test_string() {
29 const char* test_data[] = {"", "12345", "1234567890", "12345678901234567890"};
30 const char* results[] = {"a", "a12345", "a1234567890", "a12345678901234567890"};
31
32 for (size_t i = 0; i != 4; ++i) {
33 { // operator+(value_type, const string&);
34 const S str(test_data[i]);
35 assert('a' + str == results[i]);
36 LIBCPP_ASSERT(is_string_asan_correct('a' + str));
37 }
38#if TEST_STD_VER >= 11
39 { // operator+(value_type, string&&);
40 S str(test_data[i]);
41 assert('a' + std::move(str) == results[i]);
42 }
43#endif
44 }
45}
46
47TEST_CONSTEXPR_CXX20 bool test() {
48 test_string<std::string>();
49#if TEST_STD_VER >= 11
50 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
51 test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char> > >();
52#endif
53
54 { // check that growing to max_size() works
55 using string_type = std::basic_string<char, std::char_traits<char>, tiny_size_allocator<29, char> >;
56 string_type str;
57 str.resize(str.max_size() - 1);
58 string_type result = 'a' + str;
59
60 assert(result.size() == result.max_size());
61 assert(result.front() == 'a');
62 assert(result.capacity() <= result.get_allocator().max_size());
63 }
64
65 return true;
66}
67
68int main(int, char**) {
69 test();
70#if TEST_STD_VER >= 20
71 static_assert(test());
72#endif
73
74 return 0;
75}
76

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