xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/move_alloc.pass.cpp (revision 9a140a1586cc4d13ecc97a28065f9ed2f93c99de)
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 // UNSUPPORTED: c++03
10 
11 // <string>
12 
13 // basic_string(basic_string&& str, const Allocator& alloc);
14 
15 #include <string>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 #include "test_allocator.h"
20 #include "min_allocator.h"
21 
22 template <class S>
23 void
24 test(S s0, const typename S::allocator_type& a)
25 {
26     S s1 = s0;
27     S s2(std::move(s0), a);
28     LIBCPP_ASSERT(s2.__invariants());
29     LIBCPP_ASSERT(s0.__invariants());
30     assert(s2 == s1);
31     assert(s2.capacity() >= s2.size());
32     assert(s2.get_allocator() == a);
33 }
34 
35 int main(int, char**)
36 {
37     test_allocator_statistics alloc_stats;
38     {
39     typedef test_allocator<char> A;
40     typedef std::basic_string<char, std::char_traits<char>, A> S;
41 #if TEST_STD_VER > 14
42     static_assert((noexcept(S{})), "" );
43 #elif TEST_STD_VER >= 11
44     static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
45 #endif
46     test(S(), A(3, &alloc_stats));
47     test(S("1"), A(5, &alloc_stats));
48     test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7, &alloc_stats));
49     }
50 
51     int alloc_count = alloc_stats.alloc_count;
52     {
53     typedef test_allocator<char> A;
54     typedef std::basic_string<char, std::char_traits<char>, A> S;
55 #if TEST_STD_VER > 14
56     static_assert((noexcept(S{})), "" );
57 #elif TEST_STD_VER >= 11
58     static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
59 #endif
60     S s1 ( "Twas brillig, and the slivy toves did gyre and gymbal in the wabe", A(&alloc_stats));
61     S s2 (std::move(s1), A(1, &alloc_stats));
62     }
63     assert ( alloc_stats.alloc_count == alloc_count );
64     {
65     typedef min_allocator<char> A;
66     typedef std::basic_string<char, std::char_traits<char>, A> S;
67 #if TEST_STD_VER > 14
68     static_assert((noexcept(S{})), "" );
69 #elif TEST_STD_VER >= 11
70     static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
71 #endif
72     test(S(), A());
73     test(S("1"), A());
74     test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());
75     }
76 
77   return 0;
78 }
79