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// assign(const basic_string<charT,traits>& str, size_type pos, size_type n=npos); // constexpr since C++20
13// the =npos was added for C++14
14
15#include <string>
16#include <stdexcept>
17#include <cassert>
18
19#include "test_macros.h"
20#include "min_allocator.h"
21#include "asan_testing.h"
22
23template <class S>
24TEST_CONSTEXPR_CXX20 void test(S s, S str, typename S::size_type pos, typename S::size_type n, S expected) {
25 if (pos <= str.size()) {
26 s.assign(str, pos, n);
27 LIBCPP_ASSERT(s.__invariants());
28 assert(s == expected);
29 LIBCPP_ASSERT(is_string_asan_correct(s));
30 }
31#ifndef TEST_HAS_NO_EXCEPTIONS
32 else if (!TEST_IS_CONSTANT_EVALUATED) {
33 try {
34 s.assign(str, pos, n);
35 assert(false);
36 } catch (std::out_of_range&) {
37 assert(pos > str.size());
38 }
39 }
40#endif
41}
42
43template <class S>
44TEST_CONSTEXPR_CXX20 void test_npos(S s, S str, typename S::size_type pos, S expected) {
45 if (pos <= str.size()) {
46 s.assign(str, pos);
47 LIBCPP_ASSERT(s.__invariants());
48 assert(s == expected);
49 }
50#ifndef TEST_HAS_NO_EXCEPTIONS
51 else if (!TEST_IS_CONSTANT_EVALUATED) {
52 try {
53 s.assign(str, pos);
54 assert(false);
55 } catch (std::out_of_range&) {
56 assert(pos > str.size());
57 }
58 }
59#endif
60}
61
62template <class S>
63TEST_CONSTEXPR_CXX20 void test_string() {
64 test(S(), S(), 0, 0, S());
65 test(S(), S(), 1, 0, S());
66 test(S(), S("12345"), 0, 3, S("123"));
67 test(S(), S("12345"), 1, 4, S("2345"));
68 test(S(), S("12345"), 3, 15, S("45"));
69 test(S(), S("12345"), 5, 15, S(""));
70 test(S(), S("12345"), 6, 15, S("not happening"));
71 test(S(), S("12345678901234567890"), 0, 0, S());
72 test(S(), S("12345678901234567890"), 1, 1, S("2"));
73 test(S(), S("12345678901234567890"), 2, 3, S("345"));
74 test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
75 test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
76
77 test(S("12345"), S(), 0, 0, S());
78 test(S("12345"), S("12345"), 2, 2, S("34"));
79 test(S("12345"), S("1234567890"), 0, 100, S("1234567890"));
80
81 test(S("12345678901234567890"), S(), 0, 0, S());
82 test(S("12345678901234567890"), S("12345"), 1, 3, S("234"));
83 test(S("12345678901234567890"), S("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 test_npos(S(), S(), 0, S());
96 test_npos(S(), S(), 1, S());
97 test_npos(S(), S("12345"), 0, S("12345"));
98 test_npos(S(), S("12345"), 1, S("2345"));
99 test_npos(S(), S("12345"), 3, S("45"));
100 test_npos(S(), S("12345"), 5, S(""));
101 test_npos(S(), S("12345"), 6, S("not happening"));
102 }
103
104 return true;
105}
106
107int main(int, char**) {
108 test();
109#if TEST_STD_VER > 17
110 static_assert(test());
111#endif
112
113 return 0;
114}
115

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