| 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 | // basic_string<charT,traits,Allocator>& append(const charT* s); // constexpr since C++20 |
| 12 | |
| 13 | #include <string> |
| 14 | #include <stdexcept> |
| 15 | #include <cassert> |
| 16 | |
| 17 | #include "test_macros.h" |
| 18 | #include "min_allocator.h" |
| 19 | #include "asan_testing.h" |
| 20 | |
| 21 | template <class S> |
| 22 | TEST_CONSTEXPR_CXX20 void test(S s, const typename S::value_type* str, S expected) { |
| 23 | s.append(str); |
| 24 | LIBCPP_ASSERT(s.__invariants()); |
| 25 | assert(s == expected); |
| 26 | LIBCPP_ASSERT(is_string_asan_correct(s)); |
| 27 | } |
| 28 | |
| 29 | template <class S> |
| 30 | TEST_CONSTEXPR_CXX20 void test_string() { |
| 31 | test(S(), "" , S()); |
| 32 | test(S(), "12345" , S("12345" )); |
| 33 | test(S(), "12345678901234567890" , S("12345678901234567890" )); |
| 34 | |
| 35 | test(S("12345" ), "" , S("12345" )); |
| 36 | test(S("12345" ), "12345" , S("1234512345" )); |
| 37 | test(S("12345" ), "1234567890" , S("123451234567890" )); |
| 38 | |
| 39 | test(S("12345678901234567890" ), "" , S("12345678901234567890" )); |
| 40 | test(S("12345678901234567890" ), "12345" , S("1234567890123456789012345" )); |
| 41 | test(S("12345678901234567890" ), "12345678901234567890" , S("1234567890123456789012345678901234567890" )); |
| 42 | } |
| 43 | |
| 44 | TEST_CONSTEXPR_CXX20 bool test() { |
| 45 | test_string<std::string>(); |
| 46 | #if TEST_STD_VER >= 11 |
| 47 | test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); |
| 48 | test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>(); |
| 49 | #endif |
| 50 | |
| 51 | { // test appending to self |
| 52 | typedef std::string S; |
| 53 | S s_short = "123/" ; |
| 54 | S s_long = "Lorem ipsum dolor sit amet, consectetur/" ; |
| 55 | |
| 56 | s_short.append(s: s_short.c_str()); |
| 57 | assert(s_short == "123/123/" ); |
| 58 | s_short.append(s: s_short.c_str()); |
| 59 | assert(s_short == "123/123/123/123/" ); |
| 60 | s_short.append(s: s_short.c_str()); |
| 61 | assert(s_short == "123/123/123/123/123/123/123/123/" ); |
| 62 | |
| 63 | s_long.append(s: s_long.c_str()); |
| 64 | assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/" ); |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | int main(int, char**) { |
| 70 | test(); |
| 71 | #if TEST_STD_VER > 17 |
| 72 | static_assert(test()); |
| 73 | #endif |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |