| 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 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+(const 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 | |
| 26 | template <class S> |
| 27 | TEST_CONSTEXPR_CXX20 void test_string() { |
| 28 | const char* test_data[2][4] = { |
| 29 | {"" , "abcde" , "abcdefghij" , "abcdefghijklmnopqrst" }, {"" , "12345" , "1234567890" , "12345678901234567890" }}; |
| 30 | |
| 31 | const char* results[4][4] = { |
| 32 | {"" , "12345" , "1234567890" , "12345678901234567890" }, |
| 33 | {"abcde" , "abcde12345" , "abcde1234567890" , "abcde12345678901234567890" }, |
| 34 | {"abcdefghij" , "abcdefghij12345" , "abcdefghij1234567890" , "abcdefghij12345678901234567890" }, |
| 35 | {"abcdefghijklmnopqrst" , |
| 36 | "abcdefghijklmnopqrst12345" , |
| 37 | "abcdefghijklmnopqrst1234567890" , |
| 38 | "abcdefghijklmnopqrst12345678901234567890" }}; |
| 39 | |
| 40 | for (size_t i = 0; i != 4; ++i) { |
| 41 | for (size_t k = 0; k != 4; ++k) { |
| 42 | { // operator+(const value_type*, const string&); |
| 43 | const char* lhs = test_data[0][i]; |
| 44 | const S rhs(test_data[1][k]); |
| 45 | assert(lhs + rhs == results[i][k]); |
| 46 | } |
| 47 | #if TEST_STD_VER >= 11 |
| 48 | { // operator+(const value_type*, string&&); |
| 49 | const char* lhs = test_data[0][i]; |
| 50 | S rhs(test_data[1][k]); |
| 51 | assert(lhs + std::move(rhs) == results[i][k]); |
| 52 | } |
| 53 | #endif |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | TEST_CONSTEXPR_CXX20 bool test() { |
| 59 | test_string<std::string>(); |
| 60 | #if TEST_STD_VER >= 11 |
| 61 | test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); |
| 62 | #endif |
| 63 | |
| 64 | { // check that growing to max_size() works |
| 65 | using string_type = std::basic_string<char, std::char_traits<char>, tiny_size_allocator<29, char> >; |
| 66 | string_type str; |
| 67 | str.resize(str.max_size() - 1); |
| 68 | string_type result = "a" + str; |
| 69 | |
| 70 | assert(result.size() == result.max_size()); |
| 71 | assert(result.front() == 'a'); |
| 72 | assert(result.capacity() <= result.get_allocator().max_size()); |
| 73 | } |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | int main(int, char**) { |
| 79 | test(); |
| 80 | #if TEST_STD_VER >= 20 |
| 81 | static_assert(test()); |
| 82 | #endif |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |