xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/move_alloc.pass.cpp (revision e85018b7dd3a89dda6b22319b03b5906f8a85aed)
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 TEST_CONSTEXPR_CXX20 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 bool test() {
36   test_allocator_statistics alloc_stats;
37   {
38     typedef test_allocator<char> A;
39     typedef std::basic_string<char, std::char_traits<char>, A> S;
40 #if TEST_STD_VER > 14
41     static_assert((noexcept(S{})), "" );
42 #elif TEST_STD_VER >= 11
43     static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
44 #endif
45     test(S(), A(3, &alloc_stats));
46     test(S("1"), A(5, &alloc_stats));
47     test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7, &alloc_stats));
48   }
49 
50     int alloc_count = alloc_stats.alloc_count;
51   {
52     typedef test_allocator<char> A;
53     typedef std::basic_string<char, std::char_traits<char>, A> S;
54 #if TEST_STD_VER > 14
55     static_assert((noexcept(S{})), "" );
56 #elif TEST_STD_VER >= 11
57     static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
58 #endif
59     S s1 ( "Twas brillig, and the slivy toves did gyre and gymbal in the wabe", A(&alloc_stats));
60     S s2 (std::move(s1), A(1, &alloc_stats));
61   }
62     assert ( alloc_stats.alloc_count == alloc_count );
63   {
64     typedef min_allocator<char> A;
65     typedef std::basic_string<char, std::char_traits<char>, A> S;
66 #if TEST_STD_VER > 14
67     static_assert((noexcept(S{})), "" );
68 #elif TEST_STD_VER >= 11
69     static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
70 #endif
71     test(S(), A());
72     test(S("1"), A());
73     test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());
74   }
75 
76   return true;
77 }
78 
79 int main(int, char**)
80 {
81   test();
82 #if TEST_STD_VER > 17
83   // static_assert(test());
84 #endif
85 
86   return 0;
87 }
88