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 && !stdlib=libc++ |
10 | |
11 | // <vector> |
12 | |
13 | // vector(vector&& c); |
14 | |
15 | #include <vector> |
16 | #include <cassert> |
17 | |
18 | #include "test_macros.h" |
19 | #include "MoveOnly.h" |
20 | #include "test_allocator.h" |
21 | #include "min_allocator.h" |
22 | #include "asan_testing.h" |
23 | |
24 | TEST_CONSTEXPR_CXX20 bool tests() |
25 | { |
26 | test_allocator_statistics alloc_stats; |
27 | { |
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) |
33 | { |
34 | l.push_back(i); |
35 | lo.push_back(i); |
36 | } |
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); |
40 | assert(l2 == lo); |
41 | assert(l.empty()); |
42 | assert(l2.get_allocator() == lo.get_allocator()); |
43 | assert(is_contiguous_container_asan_correct(l2)); |
44 | } |
45 | { |
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) |
51 | { |
52 | l.push_back(i); |
53 | lo.push_back(i); |
54 | } |
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); |
58 | assert(l2 == lo); |
59 | assert(l.empty()); |
60 | assert(l2.get_allocator() == lo.get_allocator()); |
61 | assert(is_contiguous_container_asan_correct(l2)); |
62 | } |
63 | { |
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(position: i); |
71 | assert(*j == 3); |
72 | assert(is_contiguous_container_asan_correct(c2)); |
73 | } |
74 | { |
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) |
80 | { |
81 | l.push_back(i); |
82 | lo.push_back(i); |
83 | } |
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); |
87 | assert(l2 == lo); |
88 | assert(l.empty()); |
89 | assert(l2.get_allocator() == lo.get_allocator()); |
90 | assert(is_contiguous_container_asan_correct(l2)); |
91 | } |
92 | { |
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); |
100 | assert(*j == 3); |
101 | assert(is_contiguous_container_asan_correct(c2)); |
102 | } |
103 | { |
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) { |
109 | l.push_back(i); |
110 | lo.push_back(i); |
111 | } |
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); |
115 | assert(l2 == lo); |
116 | assert(l.empty()); |
117 | assert(l2.get_allocator() == lo.get_allocator()); |
118 | assert(is_contiguous_container_asan_correct(l2)); |
119 | } |
120 | { |
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); |
128 | assert(*j == 3); |
129 | assert(is_contiguous_container_asan_correct(c2)); |
130 | } |
131 | { |
132 | alloc_stats.clear(); |
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); |
138 | { |
139 | const test_allocator<int>& a = v.get_allocator(); |
140 | assert(a.get_data() == 42); |
141 | assert(a.get_id() == 101); |
142 | } |
143 | assert(alloc_stats.count == 1); |
144 | alloc_stats.clear_ctor_counters(); |
145 | |
146 | Vect v2 = std::move(v); |
147 | assert(alloc_stats.count == 2); |
148 | assert(alloc_stats.copied == 0); |
149 | assert(alloc_stats.moved == 1); |
150 | { |
151 | const test_allocator<int>& a = v.get_allocator(); |
152 | assert(a.get_id() == test_alloc_base::moved_value); |
153 | assert(a.get_data() == test_alloc_base::moved_value); |
154 | } |
155 | { |
156 | const test_allocator<int>& a = v2.get_allocator(); |
157 | assert(a.get_id() == 101); |
158 | assert(a.get_data() == 42); |
159 | } |
160 | } |
161 | |
162 | return true; |
163 | } |
164 | |
165 | int main(int, char**) |
166 | { |
167 | tests(); |
168 | #if TEST_STD_VER > 17 |
169 | static_assert(tests()); |
170 | #endif |
171 | return 0; |
172 | } |
173 | |