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