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