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(basic_string_view<charT,traits> sv); // constexpr since C++20
13
14 #include <string>
15 #include <string_view>
16 #include <cassert>
17
18 #include "test_macros.h"
19 #include "min_allocator.h"
20 #include "test_allocator.h"
21
22 template <class S, class SV>
test(S s,SV sv,S expected)23 TEST_CONSTEXPR_CXX20 void test(S s, SV sv, S expected) {
24 s.assign(sv);
25 LIBCPP_ASSERT(s.__invariants());
26 assert(s == expected);
27 }
28
29 template <class S, class SV>
testAlloc(S s,SV sv,const typename S::allocator_type & a)30 TEST_CONSTEXPR_CXX20 void testAlloc(S s, SV sv, const typename S::allocator_type& a) {
31 s.assign(sv);
32 LIBCPP_ASSERT(s.__invariants());
33 assert(s == sv);
34 assert(s.get_allocator() == a);
35 }
36
37 template <class S>
test_string()38 TEST_CONSTEXPR_CXX20 void test_string() {
39 typedef std::string_view SV;
40 test(S(), SV(), S());
41 test(S(), SV("12345"), S("12345"));
42 test(S(), SV("1234567890"), S("1234567890"));
43 test(S(), SV("12345678901234567890"), S("12345678901234567890"));
44
45 test(S("12345"), SV(), S());
46 test(S("12345"), SV("12345"), S("12345"));
47 test(S("12345"), SV("1234567890"), S("1234567890"));
48 test(S("12345"), SV("12345678901234567890"), S("12345678901234567890"));
49
50 test(S("1234567890"), SV(), S());
51 test(S("1234567890"), SV("12345"), S("12345"));
52 test(S("1234567890"), SV("1234567890"), S("1234567890"));
53 test(S("1234567890"), SV("12345678901234567890"), S("12345678901234567890"));
54
55 test(S("12345678901234567890"), SV(), S());
56 test(S("12345678901234567890"), SV("12345"), S("12345"));
57 test(S("12345678901234567890"), SV("1234567890"), S("1234567890"));
58 test(S("12345678901234567890"), SV("12345678901234567890"), S("12345678901234567890"));
59
60 using A = typename S::allocator_type;
61
62 testAlloc(S(), SV(), A());
63 testAlloc(S(), SV("12345"), A());
64 testAlloc(S(), SV("1234567890"), A());
65 testAlloc(S(), SV("12345678901234567890"), A());
66 }
67
test()68 TEST_CONSTEXPR_CXX20 bool test() {
69 test_string<std::string>();
70 #if TEST_STD_VER >= 11
71 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
72 #endif
73
74 return true;
75 }
76
main(int,char **)77 int main(int, char**) {
78 test();
79 #if TEST_STD_VER > 17
80 static_assert(test());
81 #endif
82
83 return 0;
84 }
85