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 
21 template <class S>
22 TEST_CONSTEXPR_CXX20 void
test(S s,const typename S::value_type * str,typename S::size_type n,S expected)23 test(S s, const typename S::value_type* str, typename S::size_type n, S expected)
24 {
25     s.append(str, n);
26     LIBCPP_ASSERT(s.__invariants());
27     assert(s == expected);
28 }
29 
test()30 TEST_CONSTEXPR_CXX20 bool test() {
31   {
32     typedef std::string S;
33     test(S(), "", 0, S());
34     test(S(), "12345", 3, S("123"));
35     test(S(), "12345", 4, S("1234"));
36     test(S(), "12345678901234567890", 0, S());
37     test(S(), "12345678901234567890", 1, S("1"));
38     test(S(), "12345678901234567890", 3, S("123"));
39     test(S(), "12345678901234567890", 20, S("12345678901234567890"));
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,
48          S("1234567890123456789012345678901234567890"));
49   }
50 #if TEST_STD_VER >= 11
51   {
52     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
53     test(S(), "", 0, S());
54     test(S(), "12345", 3, S("123"));
55     test(S(), "12345", 4, S("1234"));
56     test(S(), "12345678901234567890", 0, S());
57     test(S(), "12345678901234567890", 1, S("1"));
58     test(S(), "12345678901234567890", 3, S("123"));
59     test(S(), "12345678901234567890", 20, S("12345678901234567890"));
60 
61     test(S("12345"), "", 0, S("12345"));
62     test(S("12345"), "12345", 5, S("1234512345"));
63     test(S("12345"), "1234567890", 10, S("123451234567890"));
64 
65     test(S("12345678901234567890"), "", 0, S("12345678901234567890"));
66     test(S("12345678901234567890"), "12345", 5, S("1234567890123456789012345"));
67     test(S("12345678901234567890"), "12345678901234567890", 20,
68          S("1234567890123456789012345678901234567890"));
69   }
70 #endif
71 
72   { // test appending to self
73     typedef std::string S;
74     S s_short = "123/";
75     S s_long  = "Lorem ipsum dolor sit amet, consectetur/";
76 
77     s_short.append(s_short.data(), s_short.size());
78     assert(s_short == "123/123/");
79     s_short.append(s_short.data(), s_short.size());
80     assert(s_short == "123/123/123/123/");
81     s_short.append(s_short.data(), s_short.size());
82     assert(s_short == "123/123/123/123/123/123/123/123/");
83 
84     s_long.append(s_long.data(), s_long.size());
85     assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
86   }
87 
88   return true;
89 }
90 
main(int,char **)91 int main(int, char**)
92 {
93   test();
94 #if TEST_STD_VER > 17
95   static_assert(test());
96 #endif
97 
98   return 0;
99 }
100