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