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// XFAIL: FROZEN-CXX03-HEADERS-FIXME
10
11// <string>
12
13// basic_string<charT,traits,Allocator>&
14// append(const charT* s, size_type n); // constexpr since C++20
15
16#include <string>
17#include <stdexcept>
18#include <cassert>
19
20#include "test_macros.h"
21#include "min_allocator.h"
22#include "asan_testing.h"
23
24template <class S>
25TEST_CONSTEXPR_CXX20 void test(S s, const typename S::value_type* str, typename S::size_type n, S expected) {
26 s.append(str, n);
27 LIBCPP_ASSERT(s.__invariants());
28 assert(s == expected);
29 LIBCPP_ASSERT(is_string_asan_correct(s));
30}
31
32template <class S>
33TEST_CONSTEXPR_CXX20 void test_string() {
34 test(S(), "", 0, S());
35 test(S(), "12345", 3, S("123"));
36 test(S(), "12345", 4, S("1234"));
37 test(S(), "12345678901234567890", 0, S());
38 test(S(), "12345678901234567890", 1, S("1"));
39 test(S(), "12345678901234567890", 3, S("123"));
40 test(S(), "12345678901234567890", 20, S("12345678901234567890"));
41 test(S(), "1234567890123456789012345678901234567890", 40, S("1234567890123456789012345678901234567890"));
42
43 test(S("12345"), "", 0, S("12345"));
44 test(S("12345"), "12345", 5, S("1234512345"));
45 test(S("12345"), "1234567890", 10, S("123451234567890"));
46
47 test(S("12345678901234567890"), "", 0, S("12345678901234567890"));
48 test(S("12345678901234567890"), "12345", 5, S("1234567890123456789012345"));
49 test(S("12345678901234567890"), "12345678901234567890", 20, S("1234567890123456789012345678901234567890"));
50
51 // Starting from long string (no SSO)
52 test(S("1234567890123456789012345678901234567890"), "", 0, S("1234567890123456789012345678901234567890"));
53 test(S("1234567890123456789012345678901234567890"), "a", 1, S("1234567890123456789012345678901234567890a"));
54 test(S("1234567890123456789012345678901234567890"),
55 "aaaaaaaaaa",
56 10,
57 S("1234567890123456789012345678901234567890aaaaaaaaaa"));
58 test(S("1234567890123456789012345678901234567890"),
59 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
60 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
61 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
62 300,
63 S("1234567890123456789012345678901234567890aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
64 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
65 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
66 "aaaaaaaaaaaaa"));
67}
68
69TEST_CONSTEXPR_CXX20 bool test() {
70 test_string<std::string>();
71#if TEST_STD_VER >= 11
72 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
73 test_string<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();
74#endif
75
76 { // test appending to self
77 typedef std::string S;
78 S s_short = "123/";
79 S s_long = "Lorem ipsum dolor sit amet, consectetur/";
80
81 s_short.append(s: s_short.data(), n: s_short.size());
82 assert(s_short == "123/123/");
83 s_short.append(s: s_short.data(), n: s_short.size());
84 assert(s_short == "123/123/123/123/");
85 s_short.append(s: s_short.data(), n: s_short.size());
86 assert(s_short == "123/123/123/123/123/123/123/123/");
87
88 s_long.append(s: s_long.data(), n: s_long.size());
89 assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
90 }
91
92 { // check that growing to max_size() works
93 using string_type = std::basic_string<char, std::char_traits<char>, tiny_size_allocator<29, char> >;
94 string_type str;
95 auto max_size = str.max_size();
96 str.resize(max_size / 2 + max_size % 2);
97 str.append(str.c_str(), max_size / 2);
98 assert(str.capacity() >= str.size());
99 assert(str.size() == str.max_size());
100 }
101
102 return true;
103}
104
105int main(int, char**) {
106 test();
107#if TEST_STD_VER > 17
108 static_assert(test());
109#endif
110
111 return 0;
112}
113

source code of libcxx/test/std/strings/basic.string/string.modifiers/string_append/pointer_size.pass.cpp