Debian Codegraph @

libcxx/test/std/strings/basic.string/string.cons/move_alloc.pass.cpp

raw
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
23 template <class S>
24 TEST_CONSTEXPR_CXX20 void test(S s0, const typename S::allocator_type& a) {
25 S s1 = s0;
26 S s2(std::move(s0), a);
27 LIBCPP_ASSERT(s2.__invariants());
28 LIBCPP_ASSERT(s0.__invariants());
29 assert(s2 == s1);
30 assert(s2.capacity() >= s2.size());
31 assert(s2.get_allocator() == a);
32 LIBCPP_ASSERT(is_string_asan_correct(s0));
33 LIBCPP_ASSERT(is_string_asan_correct(s1));
34 LIBCPP_ASSERT(is_string_asan_correct(s2));
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 test();
83 #if TEST_STD_VER > 17
84 static_assert(test());
85 #endif
86
87 return 0;
88 }