1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
13 // basic_string(basic_string&& str, const Allocator& alloc); // constexpr since C++20
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"
25 TEST_CONSTEXPR_CXX20
void test(S s0
, const typename
S::allocator_type
& a
) {
27 S
s2(std::move(s0
), a
);
28 LIBCPP_ASSERT(s2
.__invariants());
29 LIBCPP_ASSERT(s0
.__invariants());
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
));
38 TEST_CONSTEXPR_CXX20
bool test() {
39 test_allocator_statistics alloc_stats
;
41 typedef test_allocator
<char> A
;
42 typedef std::basic_string
<char, std::char_traits
<char>, A
> S
;
44 static_assert((noexcept(S
{})), "");
45 #elif TEST_STD_VER >= 11
46 static_assert((noexcept(S()) == std::is_nothrow_move_constructible
<A
>::value
), "");
48 test(S(), A(3, &alloc_stats
));
49 test(S("1"), A(5, &alloc_stats
));
50 test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7, &alloc_stats
));
53 int alloc_count
= alloc_stats
.alloc_count
;
55 typedef test_allocator
<char> A
;
56 typedef std::basic_string
<char, std::char_traits
<char>, A
> S
;
58 static_assert((noexcept(S
{})), "");
59 #elif TEST_STD_VER >= 11
60 static_assert((noexcept(S()) == std::is_nothrow_move_constructible
<A
>::value
), "");
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
));
65 assert(alloc_stats
.alloc_count
== alloc_count
);
67 typedef min_allocator
<char> A
;
68 typedef std::basic_string
<char, std::char_traits
<char>, A
> S
;
70 static_assert((noexcept(S
{})), "");
71 #elif TEST_STD_VER >= 11
72 static_assert((noexcept(S()) == std::is_nothrow_move_constructible
<A
>::value
), "");
76 test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());
79 typedef operator_hijacker_allocator
<char> A
;
80 typedef std::basic_string
<char, std::char_traits
<char>, A
> S
;
82 static_assert((noexcept(S
{})), "");
83 #elif TEST_STD_VER >= 11
84 static_assert((noexcept(S()) == std::is_nothrow_move_constructible
<A
>::value
), "");
88 test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());
94 int main(int, char**) {
97 static_assert(test());