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 // <vector>
12
13 // vector(vector&& c);
14
15 #include <vector>
16 #include <cassert>
17 #include "test_macros.h"
18 #include "test_allocator.h"
19 #include "min_allocator.h"
20
tests()21 TEST_CONSTEXPR_CXX20 bool tests()
22 {
23 test_allocator_statistics alloc_stats;
24 {
25 std::vector<bool, test_allocator<bool> > l(test_allocator<bool>(5, &alloc_stats));
26 std::vector<bool, test_allocator<bool> > lo(test_allocator<bool>(5, &alloc_stats));
27 for (int i = 1; i <= 3; ++i)
28 {
29 l.push_back(true);
30 lo.push_back(true);
31 }
32 std::vector<bool, test_allocator<bool> > l2 = std::move(l);
33 assert(l2 == lo);
34 assert(l.empty());
35 assert(l2.get_allocator() == lo.get_allocator());
36 }
37 {
38 std::vector<bool, other_allocator<bool> > l(other_allocator<bool>(5));
39 std::vector<bool, other_allocator<bool> > lo(other_allocator<bool>(5));
40 for (int i = 1; i <= 3; ++i)
41 {
42 l.push_back(true);
43 lo.push_back(true);
44 }
45 std::vector<bool, other_allocator<bool> > l2 = std::move(l);
46 assert(l2 == lo);
47 assert(l.empty());
48 assert(l2.get_allocator() == lo.get_allocator());
49 }
50 {
51 std::vector<bool, min_allocator<bool> > l(min_allocator<bool>{});
52 std::vector<bool, min_allocator<bool> > lo(min_allocator<bool>{});
53 for (int i = 1; i <= 3; ++i)
54 {
55 l.push_back(true);
56 lo.push_back(true);
57 }
58 std::vector<bool, min_allocator<bool> > l2 = std::move(l);
59 assert(l2 == lo);
60 assert(l.empty());
61 assert(l2.get_allocator() == lo.get_allocator());
62 }
63 {
64 alloc_stats.clear();
65 using Vect = std::vector<bool, test_allocator<bool> >;
66 using AllocT = Vect::allocator_type;
67 Vect v(test_allocator<bool>(42, 101, &alloc_stats));
68 assert(alloc_stats.count == 1);
69 {
70 const AllocT& a = v.get_allocator();
71 assert(alloc_stats.count == 2);
72 assert(a.get_data() == 42);
73 assert(a.get_id() == 101);
74 }
75 assert(alloc_stats.count == 1);
76 alloc_stats.clear_ctor_counters();
77
78 Vect v2 = std::move(v);
79 assert(alloc_stats.count == 2);
80 assert(alloc_stats.copied == 0);
81 assert(alloc_stats.moved == 1);
82 {
83 const AllocT& a = v.get_allocator();
84 assert(a.get_id() == test_alloc_base::moved_value);
85 assert(a.get_data() == test_alloc_base::moved_value);
86 }
87 {
88 const AllocT& a = v2.get_allocator();
89 assert(a.get_id() == 101);
90 assert(a.get_data() == 42);
91 }
92 }
93
94 return true;
95 }
96
main(int,char **)97 int main(int, char**)
98 {
99 tests();
100 #if TEST_STD_VER > 17
101 static_assert(tests());
102 #endif
103 return 0;
104 }
105