1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 // basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
20 #include "test_macros.h"
21 #include "min_allocator.h"
23 template <class S, class SV>
25 test(S s, SV sv, typename S::size_type pos, typename S::size_type n, S expected)
30 LIBCPP_ASSERT(s.__invariants());
31 assert(s == expected);
33 #ifndef TEST_HAS_NO_EXCEPTIONS
41 catch (std::out_of_range&)
43 assert(pos > sv.size());
49 template <class S, class SV>
51 test_npos(S s, SV sv, typename S::size_type pos, S expected)
56 LIBCPP_ASSERT(s.__invariants());
57 assert(s == expected);
59 #ifndef TEST_HAS_NO_EXCEPTIONS
67 catch (std::out_of_range&)
69 assert(pos > sv.size());
78 typedef std::string S;
79 typedef std::string_view SV;
80 test(S(), SV(), 0, 0, S());
81 test(S(), SV(), 1, 0, S());
82 test(S(), SV("12345"), 0, 3, S("123"));
83 test(S(), SV("12345"), 1, 4, S("2345"));
84 test(S(), SV("12345"), 3, 15, S("45"));
85 test(S(), SV("12345"), 5, 15, S(""));
86 test(S(), SV("12345"), 6, 15, S("not happening"));
87 test(S(), SV("12345678901234567890"), 0, 0, S());
88 test(S(), SV("12345678901234567890"), 1, 1, S("2"));
89 test(S(), SV("12345678901234567890"), 2, 3, S("345"));
90 test(S(), SV("12345678901234567890"), 12, 13, S("34567890"));
91 test(S(), SV("12345678901234567890"), 21, 13, S("not happening"));
93 test(S("12345"), SV(), 0, 0, S("12345"));
94 test(S("12345"), SV("12345"), 2, 2, S("1234534"));
95 test(S("12345"), SV("1234567890"), 0, 100, S("123451234567890"));
97 test(S("12345678901234567890"), SV(), 0, 0, S("12345678901234567890"));
98 test(S("12345678901234567890"), SV("12345"), 1, 3, S("12345678901234567890234"));
99 test(S("12345678901234567890"), SV("12345678901234567890"), 5, 10,
100 S("123456789012345678906789012345"));
102 #if TEST_STD_VER >= 11
104 typedef std::basic_string <char, std::char_traits<char>, min_allocator<char>> S;
105 typedef std::basic_string_view<char, std::char_traits<char> > SV;
106 test(S(), SV(), 0, 0, S());
107 test(S(), SV(), 1, 0, S());
108 test(S(), SV("12345"), 0, 3, S("123"));
109 test(S(), SV("12345"), 1, 4, S("2345"));
110 test(S(), SV("12345"), 3, 15, S("45"));
111 test(S(), SV("12345"), 5, 15, S(""));
112 test(S(), SV("12345"), 6, 15, S("not happening"));
113 test(S(), SV("12345678901234567890"), 0, 0, S());
114 test(S(), SV("12345678901234567890"), 1, 1, S("2"));
115 test(S(), SV("12345678901234567890"), 2, 3, S("345"));
116 test(S(), SV("12345678901234567890"), 12, 13, S("34567890"));
117 test(S(), SV("12345678901234567890"), 21, 13, S("not happening"));
119 test(S("12345"), SV(), 0, 0, S("12345"));
120 test(S("12345"), SV("12345"), 2, 2, S("1234534"));
121 test(S("12345"), SV("1234567890"), 0, 100, S("123451234567890"));
123 test(S("12345678901234567890"), SV(), 0, 0, S("12345678901234567890"));
124 test(S("12345678901234567890"), SV("12345"), 1, 3, S("12345678901234567890234"));
125 test(S("12345678901234567890"), SV("12345678901234567890"), 5, 10,
126 S("123456789012345678906789012345"));
130 typedef std::string S;
131 typedef std::string_view SV;
132 test_npos(S(), SV(), 0, S());
133 test_npos(S(), SV(), 1, S());
134 test_npos(S(), SV("12345"), 0, S("12345"));
135 test_npos(S(), SV("12345"), 1, S("2345"));
136 test_npos(S(), SV("12345"), 3, S("45"));
137 test_npos(S(), SV("12345"), 5, S(""));
138 test_npos(S(), SV("12345"), 6, S("not happening"));
143 std::string_view sv = "EFGH";
146 s.append("CDEF", 0); // calls append(const char *, len)
150 s.append("QRST", 0, std::string::npos); // calls append(string("QRST"), pos, npos)
154 s.append(sv, 0); // calls append(T, pos, npos)
158 s.append(sv, 0, std::string::npos); // calls append(T, pos, npos)
162 s.append(arr, 0); // calls append(const char *, len)
166 s.append(arr, 0, std::string::npos); // calls append(string("IJKL"), pos, npos)
170 s.append(arr, 0); // calls append(const char *, len)
176 std::string s = "ABCD";
177 std::string_view sv = s;
179 assert(s == "ABCDABCD");
182 s.append(sv, 0, std::string::npos);
183 assert(s == "ABCDABCDABCDABCD");
186 s.append(sv, sv.size());
187 assert(s == "ABCDABCDABCDABCD");
191 std::string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
192 std::string_view sv = s;
194 assert(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
197 s.append(sv, 0, std::string::npos);
198 assert(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");