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 charT* s, size_type n); // 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 
21 template <class S>
22 TEST_CONSTEXPR_CXX20 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.assign(str, n);
26     LIBCPP_ASSERT(s.__invariants());
27     assert(s == expected);
28 }
29 
test()30 TEST_CONSTEXPR_CXX20 bool test() {
31   {
32     typedef std::string S;
33     test(S(), "", 0, S());
34     test(S(), "12345", 3, S("123"));
35     test(S(), "12345", 4, S("1234"));
36     test(S(), "12345678901234567890", 0, S());
37     test(S(), "12345678901234567890", 1, S("1"));
38     test(S(), "12345678901234567890", 3, S("123"));
39     test(S(), "12345678901234567890", 20, S("12345678901234567890"));
40 
41     test(S("12345"), "", 0, S());
42     test(S("12345"), "12345", 5, S("12345"));
43     test(S("12345"), "1234567890", 10, S("1234567890"));
44 
45     test(S("12345678901234567890"), "", 0, S());
46     test(S("12345678901234567890"), "12345", 5, S("12345"));
47     test(S("12345678901234567890"), "12345678901234567890", 20,
48          S("12345678901234567890"));
49   }
50 #if TEST_STD_VER >= 11
51   {
52     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
53     test(S(), "", 0, S());
54     test(S(), "12345", 3, S("123"));
55     test(S(), "12345", 4, S("1234"));
56     test(S(), "12345678901234567890", 0, S());
57     test(S(), "12345678901234567890", 1, S("1"));
58     test(S(), "12345678901234567890", 3, S("123"));
59     test(S(), "12345678901234567890", 20, S("12345678901234567890"));
60 
61     test(S("12345"), "", 0, S());
62     test(S("12345"), "12345", 5, S("12345"));
63     test(S("12345"), "1234567890", 10, S("1234567890"));
64 
65     test(S("12345678901234567890"), "", 0, S());
66     test(S("12345678901234567890"), "12345", 5, S("12345"));
67     test(S("12345678901234567890"), "12345678901234567890", 20,
68          S("12345678901234567890"));
69   }
70 #endif
71   { // test assign to self
72     typedef std::string S;
73     S s_short = "123/";
74     S s_long  = "Lorem ipsum dolor sit amet, consectetur/";
75 
76     s_short.assign(s_short.data(), s_short.size());
77     assert(s_short == "123/");
78     s_short.assign(s_short.data() + 2, s_short.size() - 2);
79     assert(s_short == "3/");
80 
81     s_long.assign(s_long.data(), s_long.size());
82     assert(s_long == "Lorem ipsum dolor sit amet, consectetur/");
83 
84     s_long.assign(s_long.data() + 2, 8 );
85     assert(s_long == "rem ipsu");
86   }
87 
88   return true;
89 }
90 
main(int,char **)91 int main(int, char**)
92 {
93   test();
94 #if TEST_STD_VER > 17
95   static_assert(test());
96 #endif
97 
98   return 0;
99 }
100