15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <string>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // basic_string<charT,traits,Allocator>&
12*425620ccSNikolas Klauser //   assign(const basic_string<charT,traits>& str); // constexpr since C++20
135a83710eSEric Fiselier 
145a83710eSEric Fiselier #include <string>
155a83710eSEric Fiselier #include <cassert>
165a83710eSEric Fiselier 
17ffc888bcSMarshall Clow #include "test_macros.h"
185a83710eSEric Fiselier #include "min_allocator.h"
19ffc888bcSMarshall Clow #include "test_allocator.h"
205a83710eSEric Fiselier 
215a83710eSEric Fiselier template <class S>
22dcffa7d3SNikolas Klauser TEST_CONSTEXPR_CXX20 void
test(S s,S str,S expected)235a83710eSEric Fiselier test(S s, S str, S expected)
245a83710eSEric Fiselier {
255a83710eSEric Fiselier     s.assign(str);
261f4231f8SEric Fiselier     LIBCPP_ASSERT(s.__invariants());
275a83710eSEric Fiselier     assert(s == expected);
285a83710eSEric Fiselier }
295a83710eSEric Fiselier 
30ffc888bcSMarshall Clow template <class S>
31dcffa7d3SNikolas Klauser TEST_CONSTEXPR_CXX20 void
testAlloc(S s,S str,const typename S::allocator_type & a)32ffc888bcSMarshall Clow testAlloc(S s, S str, const typename S::allocator_type& a)
33ffc888bcSMarshall Clow {
34ffc888bcSMarshall Clow     s.assign(str);
351f4231f8SEric Fiselier     LIBCPP_ASSERT(s.__invariants());
36ffc888bcSMarshall Clow     assert(s == str);
37ffc888bcSMarshall Clow     assert(s.get_allocator() == a);
38ffc888bcSMarshall Clow }
39ffc888bcSMarshall Clow 
test()40*425620ccSNikolas Klauser TEST_CONSTEXPR_CXX20 bool test() {
415a83710eSEric Fiselier   {
425a83710eSEric Fiselier     typedef std::string S;
435a83710eSEric Fiselier     test(S(), S(), S());
445a83710eSEric Fiselier     test(S(), S("12345"), S("12345"));
455a83710eSEric Fiselier     test(S(), S("1234567890"), S("1234567890"));
465a83710eSEric Fiselier     test(S(), S("12345678901234567890"), S("12345678901234567890"));
475a83710eSEric Fiselier 
485a83710eSEric Fiselier     test(S("12345"), S(), S());
495a83710eSEric Fiselier     test(S("12345"), S("12345"), S("12345"));
505a83710eSEric Fiselier     test(S("12345"), S("1234567890"), S("1234567890"));
515a83710eSEric Fiselier     test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
525a83710eSEric Fiselier 
535a83710eSEric Fiselier     test(S("1234567890"), S(), S());
545a83710eSEric Fiselier     test(S("1234567890"), S("12345"), S("12345"));
555a83710eSEric Fiselier     test(S("1234567890"), S("1234567890"), S("1234567890"));
565a83710eSEric Fiselier     test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
575a83710eSEric Fiselier 
585a83710eSEric Fiselier     test(S("12345678901234567890"), S(), S());
595a83710eSEric Fiselier     test(S("12345678901234567890"), S("12345"), S("12345"));
605a83710eSEric Fiselier     test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
615a83710eSEric Fiselier     test(S("12345678901234567890"), S("12345678901234567890"),
625a83710eSEric Fiselier          S("12345678901234567890"));
63ffc888bcSMarshall Clow 
64ffc888bcSMarshall Clow     testAlloc(S(), S(), std::allocator<char>());
65ffc888bcSMarshall Clow     testAlloc(S(), S("12345"), std::allocator<char>());
66ffc888bcSMarshall Clow     testAlloc(S(), S("1234567890"), std::allocator<char>());
67ffc888bcSMarshall Clow     testAlloc(S(), S("12345678901234567890"), std::allocator<char>());
685a83710eSEric Fiselier   }
69ffc888bcSMarshall Clow 
70ffc888bcSMarshall Clow   { //  LWG#5579 make sure assign takes the allocators where appropriate
71ffc888bcSMarshall Clow     typedef other_allocator<char> A;  // has POCCA --> true
72ffc888bcSMarshall Clow     typedef std::basic_string<char, std::char_traits<char>, A> S;
73ffc888bcSMarshall Clow     testAlloc(S(A(5)), S(A(3)), A(3));
74ffc888bcSMarshall Clow     testAlloc(S(A(5)), S("1"), A());
75ffc888bcSMarshall Clow     testAlloc(S(A(5)), S("1", A(7)), A(7));
76ffc888bcSMarshall Clow     testAlloc(S(A(5)), S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), A(7));
77ffc888bcSMarshall Clow   }
78ffc888bcSMarshall Clow 
7976b4afc0SMarshall Clow #if TEST_STD_VER >= 11
805a83710eSEric Fiselier   {
815a83710eSEric Fiselier     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
825a83710eSEric Fiselier     test(S(), S(), S());
835a83710eSEric Fiselier     test(S(), S("12345"), S("12345"));
845a83710eSEric Fiselier     test(S(), S("1234567890"), S("1234567890"));
855a83710eSEric Fiselier     test(S(), S("12345678901234567890"), S("12345678901234567890"));
865a83710eSEric Fiselier 
875a83710eSEric Fiselier     test(S("12345"), S(), S());
885a83710eSEric Fiselier     test(S("12345"), S("12345"), S("12345"));
895a83710eSEric Fiselier     test(S("12345"), S("1234567890"), S("1234567890"));
905a83710eSEric Fiselier     test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
915a83710eSEric Fiselier 
925a83710eSEric Fiselier     test(S("1234567890"), S(), S());
935a83710eSEric Fiselier     test(S("1234567890"), S("12345"), S("12345"));
945a83710eSEric Fiselier     test(S("1234567890"), S("1234567890"), S("1234567890"));
955a83710eSEric Fiselier     test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
965a83710eSEric Fiselier 
975a83710eSEric Fiselier     test(S("12345678901234567890"), S(), S());
985a83710eSEric Fiselier     test(S("12345678901234567890"), S("12345"), S("12345"));
995a83710eSEric Fiselier     test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
1005a83710eSEric Fiselier     test(S("12345678901234567890"), S("12345678901234567890"),
1015a83710eSEric Fiselier          S("12345678901234567890"));
102ffc888bcSMarshall Clow 
103ffc888bcSMarshall Clow     testAlloc(S(), S(), min_allocator<char>());
104ffc888bcSMarshall Clow     testAlloc(S(), S("12345"), min_allocator<char>());
105ffc888bcSMarshall Clow     testAlloc(S(), S("1234567890"), min_allocator<char>());
106ffc888bcSMarshall Clow     testAlloc(S(), S("12345678901234567890"), min_allocator<char>());
1075a83710eSEric Fiselier   }
1085a83710eSEric Fiselier #endif
109ffc888bcSMarshall Clow #if TEST_STD_VER > 14
1108428a9d5SMarshall Clow   {
1118428a9d5SMarshall Clow     typedef std::string S;
1128428a9d5SMarshall Clow     static_assert(noexcept(S().assign(S())), "");  // LWG#2063
1138428a9d5SMarshall Clow   }
1148428a9d5SMarshall Clow #endif
1152df59c50SJF Bastien 
116dcffa7d3SNikolas Klauser   return true;
117dcffa7d3SNikolas Klauser }
118dcffa7d3SNikolas Klauser 
main(int,char **)119dcffa7d3SNikolas Klauser int main(int, char**)
120dcffa7d3SNikolas Klauser {
121dcffa7d3SNikolas Klauser   test();
122dcffa7d3SNikolas Klauser #if TEST_STD_VER > 17
123*425620ccSNikolas Klauser   static_assert(test());
124dcffa7d3SNikolas Klauser #endif
125dcffa7d3SNikolas Klauser 
1262df59c50SJF Bastien   return 0;
1275a83710eSEric Fiselier }
128