Home | History | Annotate | Download | only in string_assign
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <string>
     11 
     12 // basic_string<charT,traits,Allocator>&
     13 //   assign(const basic_string<charT,traits>& str);
     14 
     15 #include <string>
     16 #include <cassert>
     17 
     18 #include "test_macros.h"
     19 #include "min_allocator.h"
     20 #include "test_allocator.h"
     21 
     22 template <class S>
     23 void
     24 test(S s, S str, S expected)
     25 {
     26     s.assign(str);
     27     LIBCPP_ASSERT(s.__invariants());
     28     assert(s == expected);
     29 }
     30 
     31 template <class S>
     32 void
     33 testAlloc(S s, S str, const typename S::allocator_type& a)
     34 {
     35     s.assign(str);
     36     LIBCPP_ASSERT(s.__invariants());
     37     assert(s == str);
     38     assert(s.get_allocator() == a);
     39 }
     40 
     41 int main()
     42 {
     43     {
     44     typedef std::string S;
     45     test(S(), S(), S());
     46     test(S(), S("12345"), S("12345"));
     47     test(S(), S("1234567890"), S("1234567890"));
     48     test(S(), S("12345678901234567890"), S("12345678901234567890"));
     49 
     50     test(S("12345"), S(), S());
     51     test(S("12345"), S("12345"), S("12345"));
     52     test(S("12345"), S("1234567890"), S("1234567890"));
     53     test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
     54 
     55     test(S("1234567890"), S(), S());
     56     test(S("1234567890"), S("12345"), S("12345"));
     57     test(S("1234567890"), S("1234567890"), S("1234567890"));
     58     test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
     59 
     60     test(S("12345678901234567890"), S(), S());
     61     test(S("12345678901234567890"), S("12345"), S("12345"));
     62     test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
     63     test(S("12345678901234567890"), S("12345678901234567890"),
     64          S("12345678901234567890"));
     65 
     66     testAlloc(S(), S(), std::allocator<char>());
     67     testAlloc(S(), S("12345"), std::allocator<char>());
     68     testAlloc(S(), S("1234567890"), std::allocator<char>());
     69     testAlloc(S(), S("12345678901234567890"), std::allocator<char>());
     70     }
     71 
     72     { //  LWG#5579 make sure assign takes the allocators where appropriate
     73     typedef other_allocator<char> A;  // has POCCA --> true
     74     typedef std::basic_string<char, std::char_traits<char>, A> S;
     75     testAlloc(S(A(5)), S(A(3)), A(3));
     76     testAlloc(S(A(5)), S("1"), A());
     77     testAlloc(S(A(5)), S("1", A(7)), A(7));
     78     testAlloc(S(A(5)), S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), A(7));
     79     }
     80 
     81 #if TEST_STD_VER >= 11
     82     {
     83     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
     84     test(S(), S(), S());
     85     test(S(), S("12345"), S("12345"));
     86     test(S(), S("1234567890"), S("1234567890"));
     87     test(S(), S("12345678901234567890"), S("12345678901234567890"));
     88 
     89     test(S("12345"), S(), S());
     90     test(S("12345"), S("12345"), S("12345"));
     91     test(S("12345"), S("1234567890"), S("1234567890"));
     92     test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
     93 
     94     test(S("1234567890"), S(), S());
     95     test(S("1234567890"), S("12345"), S("12345"));
     96     test(S("1234567890"), S("1234567890"), S("1234567890"));
     97     test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
     98 
     99     test(S("12345678901234567890"), S(), S());
    100     test(S("12345678901234567890"), S("12345"), S("12345"));
    101     test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
    102     test(S("12345678901234567890"), S("12345678901234567890"),
    103          S("12345678901234567890"));
    104 
    105     testAlloc(S(), S(), min_allocator<char>());
    106     testAlloc(S(), S("12345"), min_allocator<char>());
    107     testAlloc(S(), S("1234567890"), min_allocator<char>());
    108     testAlloc(S(), S("12345678901234567890"), min_allocator<char>());
    109     }
    110 #endif
    111 #if TEST_STD_VER > 14
    112     {
    113     typedef std::string S;
    114     static_assert(noexcept(S().assign(S())), "");  // LWG#2063
    115     }
    116 #endif
    117 }
    118