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// template <class T>
12// basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
13
14#include <string>
15#include <string>
16#include <stdexcept>
17#include <cassert>
18
19#include "test_macros.h"
20#include "min_allocator.h"
21
22template <class S, class SV>
23TEST_CONSTEXPR_CXX20 void test(S s, SV sv, typename S::size_type pos, typename S::size_type n, S expected) {
24 if (pos <= sv.size()) {
25 s.append(sv, pos, n);
26 LIBCPP_ASSERT(s.__invariants());
27 assert(s == expected);
28 }
29#ifndef TEST_HAS_NO_EXCEPTIONS
30 else if (!TEST_IS_CONSTANT_EVALUATED) {
31 try {
32 s.append(sv, pos, n);
33 assert(false);
34 } catch (std::out_of_range&) {
35 assert(pos > sv.size());
36 }
37 }
38#endif
39}
40
41template <class S, class SV>
42TEST_CONSTEXPR_CXX20 void test_npos(S s, SV sv, typename S::size_type pos, S expected) {
43 if (pos <= sv.size()) {
44 s.append(sv, pos);
45 LIBCPP_ASSERT(s.__invariants());
46 assert(s == expected);
47 }
48#ifndef TEST_HAS_NO_EXCEPTIONS
49 else if (!TEST_IS_CONSTANT_EVALUATED) {
50 try {
51 s.append(sv, pos);
52 assert(false);
53 } catch (std::out_of_range&) {
54 assert(pos > sv.size());
55 }
56 }
57#endif
58}
59
60template <class S>
61TEST_CONSTEXPR_CXX20 void test_string() {
62 typedef std::string_view SV;
63 test(S(), SV(), 0, 0, S());
64 test(S(), SV(), 1, 0, S());
65 test(S(), SV("12345"), 0, 3, S("123"));
66 test(S(), SV("12345"), 1, 4, S("2345"));
67 test(S(), SV("12345"), 3, 15, S("45"));
68 test(S(), SV("12345"), 5, 15, S(""));
69 test(S(), SV("12345"), 6, 15, S("not happening"));
70 test(S(), SV("12345678901234567890"), 0, 0, S());
71 test(S(), SV("12345678901234567890"), 1, 1, S("2"));
72 test(S(), SV("12345678901234567890"), 2, 3, S("345"));
73 test(S(), SV("12345678901234567890"), 12, 13, S("34567890"));
74 test(S(), SV("12345678901234567890"), 21, 13, S("not happening"));
75
76 test(S("12345"), SV(), 0, 0, S("12345"));
77 test(S("12345"), SV("12345"), 2, 2, S("1234534"));
78 test(S("12345"), SV("1234567890"), 0, 100, S("123451234567890"));
79
80 test(S("12345678901234567890"), SV(), 0, 0, S("12345678901234567890"));
81 test(S("12345678901234567890"), SV("12345"), 1, 3, S("12345678901234567890234"));
82 test(S("12345678901234567890"), SV("12345678901234567890"), 5, 10, S("123456789012345678906789012345"));
83
84 // Starting from long string (no SSO)
85 test(S("1234512345678901234567890"), SV(), 0, 0, S("1234512345678901234567890"));
86 test(S("1234512345678901234567890"), SV("12345"), 1, 3, S("1234512345678901234567890234"));
87 test(S("1234512345678901234567890"), SV("12345678901234567890"), 5, 10, S("12345123456789012345678906789012345"));
88}
89
90TEST_CONSTEXPR_CXX20 bool test() {
91 test_string<std::string>();
92#if TEST_STD_VER >= 11
93 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
94#endif
95
96 {
97 typedef std::string S;
98 typedef std::string_view SV;
99 test_npos(S(), SV(), 0, S());
100 test_npos(S(), SV(), 1, S());
101 test_npos(S(), SV("12345"), 0, S("12345"));
102 test_npos(S(), SV("12345"), 1, S("2345"));
103 test_npos(S(), SV("12345"), 3, S("45"));
104 test_npos(S(), SV("12345"), 5, S(""));
105 test_npos(S(), SV("12345"), 6, S("not happening"));
106 }
107
108 {
109 std::string s;
110 std::string_view sv = "EFGH";
111 char arr[] = "IJKL";
112
113 s.append(s: "CDEF", n: 0); // calls append(const char *, len)
114 assert(s == "");
115 s.clear();
116
117 s.append(str: "QRST", pos: 0, n: std::string::npos); // calls append(string("QRST"), pos, npos)
118 assert(s == "QRST");
119 s.clear();
120
121 s.append(svt: sv, pos: 0); // calls append(T, pos, npos)
122 assert(s == sv);
123 s.clear();
124
125 s.append(svt: sv, pos: 0, n: std::string::npos); // calls append(T, pos, npos)
126 assert(s == sv);
127 s.clear();
128
129 s.append(s: arr, n: 0); // calls append(const char *, len)
130 assert(s == "");
131 s.clear();
132
133 s.append(str: arr, pos: 0, n: std::string::npos); // calls append(string("IJKL"), pos, npos)
134 assert(s == "IJKL");
135 s.clear();
136
137 s.append(s: arr, n: 0); // calls append(const char *, len)
138 assert(s == "");
139 s.clear();
140 }
141
142 {
143 std::string s = "ABCD";
144 std::string_view sv = s;
145 s.append(svt: sv);
146 assert(s == "ABCDABCD");
147
148 sv = s;
149 s.append(svt: sv, pos: 0, n: std::string::npos);
150 assert(s == "ABCDABCDABCDABCD");
151
152 sv = s;
153 s.append(svt: sv, pos: sv.size());
154 assert(s == "ABCDABCDABCDABCD");
155 }
156
157 {
158 std::string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
159 std::string_view sv = s;
160 s.append(svt: sv);
161 assert(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
162
163 sv = s;
164 s.append(svt: sv, pos: 0, n: std::string::npos);
165 assert(s ==
166 "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
167 }
168 return true;
169}
170
171int main(int, char**) {
172 test();
173#if TEST_STD_VER > 17
174 static_assert(test());
175#endif
176
177 return 0;
178}
179

source code of libcxx/test/std/strings/basic.string/string.modifiers/string_append/T_size_size.pass.cpp