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);
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 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
main(int,char **)30 int main(int, char**)
31 {
32 {
33 typedef std::string S;
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
42 test(S("12345"), "", 0, S("12345"));
43 test(S("12345"), "12345", 5, S("1234512345"));
44 test(S("12345"), "1234567890", 10, S("123451234567890"));
45
46 test(S("12345678901234567890"), "", 0, S("12345678901234567890"));
47 test(S("12345678901234567890"), "12345", 5, S("1234567890123456789012345"));
48 test(S("12345678901234567890"), "12345678901234567890", 20,
49 S("1234567890123456789012345678901234567890"));
50 }
51 #if TEST_STD_VER >= 11
52 {
53 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
54 test(S(), "", 0, S());
55 test(S(), "12345", 3, S("123"));
56 test(S(), "12345", 4, S("1234"));
57 test(S(), "12345678901234567890", 0, S());
58 test(S(), "12345678901234567890", 1, S("1"));
59 test(S(), "12345678901234567890", 3, S("123"));
60 test(S(), "12345678901234567890", 20, S("12345678901234567890"));
61
62 test(S("12345"), "", 0, S("12345"));
63 test(S("12345"), "12345", 5, S("1234512345"));
64 test(S("12345"), "1234567890", 10, S("123451234567890"));
65
66 test(S("12345678901234567890"), "", 0, S("12345678901234567890"));
67 test(S("12345678901234567890"), "12345", 5, S("1234567890123456789012345"));
68 test(S("12345678901234567890"), "12345678901234567890", 20,
69 S("1234567890123456789012345678901234567890"));
70 }
71 #endif
72
73 { // test appending to self
74 typedef std::string S;
75 S s_short = "123/";
76 S s_long = "Lorem ipsum dolor sit amet, consectetur/";
77
78 s_short.append(s_short.data(), s_short.size());
79 assert(s_short == "123/123/");
80 s_short.append(s_short.data(), s_short.size());
81 assert(s_short == "123/123/123/123/");
82 s_short.append(s_short.data(), s_short.size());
83 assert(s_short == "123/123/123/123/123/123/123/123/");
84
85 s_long.append(s_long.data(), s_long.size());
86 assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
87 }
88
89 return 0;
90 }
91