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