| 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 <cassert> |
| 20 | #include <string> |
| 21 | #include <utility> |
| 22 | |
| 23 | #include "asan_testing.h" |
| 24 | #include "min_allocator.h" |
| 25 | #include "test_macros.h" |
| 26 | |
| 27 | template <class S> |
| 28 | TEST_CONSTEXPR_CXX20 void test_string() { |
| 29 | const char* test_data[] = {"" , "12345" , "1234567890" , "12345678901234567890" }; |
| 30 | const char* results[] = {"a" , "12345a" , "1234567890a" , "12345678901234567890a" }; |
| 31 | |
| 32 | for (size_t i = 0; i != 4; ++i) { |
| 33 | { // operator+(const string&, value_type); |
| 34 | const S str(test_data[i]); |
| 35 | assert(str + 'a' == results[i]); |
| 36 | LIBCPP_ASSERT(is_string_asan_correct(str + 'a')); |
| 37 | } |
| 38 | #if TEST_STD_VER >= 11 |
| 39 | { // operator+(string&&, value_type); |
| 40 | S str(test_data[i]); |
| 41 | assert(std::move(str) + 'a' == results[i]); |
| 42 | } |
| 43 | #endif |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | TEST_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 = str + 'a'; |
| 59 | |
| 60 | assert(result.size() == result.max_size()); |
| 61 | assert(result.back() == 'a'); |
| 62 | assert(result.capacity() <= result.get_allocator().max_size()); |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | int main(int, char**) { |
| 69 | test(); |
| 70 | #if TEST_STD_VER >= 20 |
| 71 | static_assert(test()); |
| 72 | #endif |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |