Debian Codegraph @

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

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