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

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