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