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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03 && !stdlib=libc++
13 // vector(vector&& c);
18 #include "test_macros.h"
20 #include "test_allocator.h"
21 #include "min_allocator.h"
22 #include "asan_testing.h"
24 TEST_CONSTEXPR_CXX20
bool tests()
26 test_allocator_statistics alloc_stats
;
28 std::vector
<MoveOnly
, test_allocator
<MoveOnly
> > l(test_allocator
<MoveOnly
>(5, &alloc_stats
));
29 std::vector
<MoveOnly
, test_allocator
<MoveOnly
> > lo(test_allocator
<MoveOnly
>(5, &alloc_stats
));
30 assert(is_contiguous_container_asan_correct(l
));
31 assert(is_contiguous_container_asan_correct(lo
));
32 for (int i
= 1; i
<= 3; ++i
)
37 assert(is_contiguous_container_asan_correct(l
));
38 assert(is_contiguous_container_asan_correct(lo
));
39 std::vector
<MoveOnly
, test_allocator
<MoveOnly
> > l2
= std::move(l
);
42 assert(l2
.get_allocator() == lo
.get_allocator());
43 assert(is_contiguous_container_asan_correct(l2
));
46 std::vector
<MoveOnly
, other_allocator
<MoveOnly
> > l(other_allocator
<MoveOnly
>(5));
47 std::vector
<MoveOnly
, other_allocator
<MoveOnly
> > lo(other_allocator
<MoveOnly
>(5));
48 assert(is_contiguous_container_asan_correct(l
));
49 assert(is_contiguous_container_asan_correct(lo
));
50 for (int i
= 1; i
<= 3; ++i
)
55 assert(is_contiguous_container_asan_correct(l
));
56 assert(is_contiguous_container_asan_correct(lo
));
57 std::vector
<MoveOnly
, other_allocator
<MoveOnly
> > l2
= std::move(l
);
60 assert(l2
.get_allocator() == lo
.get_allocator());
61 assert(is_contiguous_container_asan_correct(l2
));
64 int a1
[] = {1, 3, 7, 9, 10};
65 std::vector
<int> c1(a1
, a1
+sizeof(a1
)/sizeof(a1
[0]));
66 assert(is_contiguous_container_asan_correct(c1
));
67 std::vector
<int>::const_iterator i
= c1
.begin();
68 std::vector
<int> c2
= std::move(c1
);
69 assert(is_contiguous_container_asan_correct(c2
));
70 std::vector
<int>::iterator j
= c2
.erase(i
);
72 assert(is_contiguous_container_asan_correct(c2
));
75 std::vector
<MoveOnly
, min_allocator
<MoveOnly
> > l((min_allocator
<MoveOnly
>()));
76 std::vector
<MoveOnly
, min_allocator
<MoveOnly
> > lo((min_allocator
<MoveOnly
>()));
77 assert(is_contiguous_container_asan_correct(l
));
78 assert(is_contiguous_container_asan_correct(lo
));
79 for (int i
= 1; i
<= 3; ++i
)
84 assert(is_contiguous_container_asan_correct(l
));
85 assert(is_contiguous_container_asan_correct(lo
));
86 std::vector
<MoveOnly
, min_allocator
<MoveOnly
> > l2
= std::move(l
);
89 assert(l2
.get_allocator() == lo
.get_allocator());
90 assert(is_contiguous_container_asan_correct(l2
));
93 int a1
[] = {1, 3, 7, 9, 10};
94 std::vector
<int, min_allocator
<int> > c1(a1
, a1
+sizeof(a1
)/sizeof(a1
[0]));
95 assert(is_contiguous_container_asan_correct(c1
));
96 std::vector
<int, min_allocator
<int> >::const_iterator i
= c1
.begin();
97 std::vector
<int, min_allocator
<int> > c2
= std::move(c1
);
98 assert(is_contiguous_container_asan_correct(c2
));
99 std::vector
<int, min_allocator
<int> >::iterator j
= c2
.erase(i
);
101 assert(is_contiguous_container_asan_correct(c2
));
104 std::vector
<MoveOnly
, safe_allocator
<MoveOnly
> > l((safe_allocator
<MoveOnly
>()));
105 std::vector
<MoveOnly
, safe_allocator
<MoveOnly
> > lo((safe_allocator
<MoveOnly
>()));
106 assert(is_contiguous_container_asan_correct(l
));
107 assert(is_contiguous_container_asan_correct(lo
));
108 for (int i
= 1; i
<= 3; ++i
) {
112 assert(is_contiguous_container_asan_correct(l
));
113 assert(is_contiguous_container_asan_correct(lo
));
114 std::vector
<MoveOnly
, safe_allocator
<MoveOnly
> > l2
= std::move(l
);
117 assert(l2
.get_allocator() == lo
.get_allocator());
118 assert(is_contiguous_container_asan_correct(l2
));
121 int a1
[] = {1, 3, 7, 9, 10};
122 std::vector
<int, safe_allocator
<int> > c1(a1
, a1
+ sizeof(a1
) / sizeof(a1
[0]));
123 assert(is_contiguous_container_asan_correct(c1
));
124 std::vector
<int, safe_allocator
<int> >::const_iterator i
= c1
.begin();
125 std::vector
<int, safe_allocator
<int> > c2
= std::move(c1
);
126 assert(is_contiguous_container_asan_correct(c2
));
127 std::vector
<int, safe_allocator
<int> >::iterator j
= c2
.erase(i
);
129 assert(is_contiguous_container_asan_correct(c2
));
133 using Vect
= std::vector
<int, test_allocator
<int> >;
134 Vect
v(test_allocator
<int>(42, 101, &alloc_stats
));
135 assert(alloc_stats
.count
== 1);
136 assert(alloc_stats
.copied
== 1);
137 assert(alloc_stats
.moved
== 0);
139 const test_allocator
<int>& a
= v
.get_allocator();
140 assert(a
.get_data() == 42);
141 assert(a
.get_id() == 101);
143 assert(alloc_stats
.count
== 1);
144 alloc_stats
.clear_ctor_counters();
146 Vect v2
= std::move(v
);
147 assert(alloc_stats
.count
== 2);
148 assert(alloc_stats
.copied
== 0);
149 assert(alloc_stats
.moved
== 1);
151 const test_allocator
<int>& a1
= v
.get_allocator();
152 assert(a1
.get_id() == test_alloc_base::moved_value
);
153 assert(a1
.get_data() == 42);
155 const test_allocator
<int>& a2
= v2
.get_allocator();
156 assert(a2
.get_id() == 101);
157 assert(a2
.get_data() == 42);
166 int main(int, char**)
169 #if TEST_STD_VER > 17
170 static_assert(tests());