| 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 | test_allocator_statistics alloc_stats; |
| 26 | { |
| 27 | std::vector<MoveOnly, test_allocator<MoveOnly> > l(test_allocator<MoveOnly>(5, &alloc_stats)); |
| 28 | std::vector<MoveOnly, test_allocator<MoveOnly> > lo(test_allocator<MoveOnly>(5, &alloc_stats)); |
| 29 | assert(is_contiguous_container_asan_correct(l)); |
| 30 | assert(is_contiguous_container_asan_correct(lo)); |
| 31 | for (int i = 1; i <= 3; ++i) { |
| 32 | l.push_back(i); |
| 33 | lo.push_back(i); |
| 34 | } |
| 35 | assert(is_contiguous_container_asan_correct(l)); |
| 36 | assert(is_contiguous_container_asan_correct(lo)); |
| 37 | std::vector<MoveOnly, test_allocator<MoveOnly> > l2 = std::move(l); |
| 38 | assert(l2 == lo); |
| 39 | assert(l.empty()); |
| 40 | assert(l2.get_allocator() == lo.get_allocator()); |
| 41 | assert(is_contiguous_container_asan_correct(l2)); |
| 42 | } |
| 43 | { |
| 44 | std::vector<MoveOnly, other_allocator<MoveOnly> > l(other_allocator<MoveOnly>(5)); |
| 45 | std::vector<MoveOnly, other_allocator<MoveOnly> > lo(other_allocator<MoveOnly>(5)); |
| 46 | assert(is_contiguous_container_asan_correct(l)); |
| 47 | assert(is_contiguous_container_asan_correct(lo)); |
| 48 | for (int i = 1; i <= 3; ++i) { |
| 49 | l.push_back(i); |
| 50 | lo.push_back(i); |
| 51 | } |
| 52 | assert(is_contiguous_container_asan_correct(l)); |
| 53 | assert(is_contiguous_container_asan_correct(lo)); |
| 54 | std::vector<MoveOnly, other_allocator<MoveOnly> > l2 = std::move(l); |
| 55 | assert(l2 == lo); |
| 56 | assert(l.empty()); |
| 57 | assert(l2.get_allocator() == lo.get_allocator()); |
| 58 | assert(is_contiguous_container_asan_correct(l2)); |
| 59 | } |
| 60 | { |
| 61 | int a1[] = {1, 3, 7, 9, 10}; |
| 62 | std::vector<int> c1(a1, a1 + sizeof(a1) / sizeof(a1[0])); |
| 63 | assert(is_contiguous_container_asan_correct(c1)); |
| 64 | std::vector<int>::const_iterator i = c1.begin(); |
| 65 | std::vector<int> c2 = std::move(c1); |
| 66 | assert(is_contiguous_container_asan_correct(c2)); |
| 67 | std::vector<int>::iterator j = c2.erase(position: i); |
| 68 | assert(*j == 3); |
| 69 | assert(is_contiguous_container_asan_correct(c2)); |
| 70 | } |
| 71 | { |
| 72 | std::vector<MoveOnly, min_allocator<MoveOnly> > l((min_allocator<MoveOnly>())); |
| 73 | std::vector<MoveOnly, min_allocator<MoveOnly> > lo((min_allocator<MoveOnly>())); |
| 74 | assert(is_contiguous_container_asan_correct(l)); |
| 75 | assert(is_contiguous_container_asan_correct(lo)); |
| 76 | for (int i = 1; i <= 3; ++i) { |
| 77 | l.push_back(i); |
| 78 | lo.push_back(i); |
| 79 | } |
| 80 | assert(is_contiguous_container_asan_correct(l)); |
| 81 | assert(is_contiguous_container_asan_correct(lo)); |
| 82 | std::vector<MoveOnly, min_allocator<MoveOnly> > l2 = std::move(l); |
| 83 | assert(l2 == lo); |
| 84 | assert(l.empty()); |
| 85 | assert(l2.get_allocator() == lo.get_allocator()); |
| 86 | assert(is_contiguous_container_asan_correct(l2)); |
| 87 | } |
| 88 | { |
| 89 | int a1[] = {1, 3, 7, 9, 10}; |
| 90 | std::vector<int, min_allocator<int> > c1(a1, a1 + sizeof(a1) / sizeof(a1[0])); |
| 91 | assert(is_contiguous_container_asan_correct(c1)); |
| 92 | std::vector<int, min_allocator<int> >::const_iterator i = c1.begin(); |
| 93 | std::vector<int, min_allocator<int> > c2 = std::move(c1); |
| 94 | assert(is_contiguous_container_asan_correct(c2)); |
| 95 | std::vector<int, min_allocator<int> >::iterator j = c2.erase(i); |
| 96 | assert(*j == 3); |
| 97 | assert(is_contiguous_container_asan_correct(c2)); |
| 98 | } |
| 99 | { |
| 100 | std::vector<MoveOnly, safe_allocator<MoveOnly> > l((safe_allocator<MoveOnly>())); |
| 101 | std::vector<MoveOnly, safe_allocator<MoveOnly> > lo((safe_allocator<MoveOnly>())); |
| 102 | assert(is_contiguous_container_asan_correct(l)); |
| 103 | assert(is_contiguous_container_asan_correct(lo)); |
| 104 | for (int i = 1; i <= 3; ++i) { |
| 105 | l.push_back(i); |
| 106 | lo.push_back(i); |
| 107 | } |
| 108 | assert(is_contiguous_container_asan_correct(l)); |
| 109 | assert(is_contiguous_container_asan_correct(lo)); |
| 110 | std::vector<MoveOnly, safe_allocator<MoveOnly> > l2 = std::move(l); |
| 111 | assert(l2 == lo); |
| 112 | assert(l.empty()); |
| 113 | assert(l2.get_allocator() == lo.get_allocator()); |
| 114 | assert(is_contiguous_container_asan_correct(l2)); |
| 115 | } |
| 116 | { |
| 117 | int a1[] = {1, 3, 7, 9, 10}; |
| 118 | std::vector<int, safe_allocator<int> > c1(a1, a1 + sizeof(a1) / sizeof(a1[0])); |
| 119 | assert(is_contiguous_container_asan_correct(c1)); |
| 120 | std::vector<int, safe_allocator<int> >::const_iterator i = c1.begin(); |
| 121 | std::vector<int, safe_allocator<int> > c2 = std::move(c1); |
| 122 | assert(is_contiguous_container_asan_correct(c2)); |
| 123 | std::vector<int, safe_allocator<int> >::iterator j = c2.erase(i); |
| 124 | assert(*j == 3); |
| 125 | assert(is_contiguous_container_asan_correct(c2)); |
| 126 | } |
| 127 | { |
| 128 | alloc_stats.clear(); |
| 129 | using Vect = std::vector<int, test_allocator<int> >; |
| 130 | Vect v(test_allocator<int>(42, 101, &alloc_stats)); |
| 131 | assert(alloc_stats.count == 1); |
| 132 | assert(alloc_stats.copied == 1); |
| 133 | assert(alloc_stats.moved == 0); |
| 134 | { |
| 135 | const test_allocator<int>& a = v.get_allocator(); |
| 136 | assert(a.get_data() == 42); |
| 137 | assert(a.get_id() == 101); |
| 138 | } |
| 139 | assert(alloc_stats.count == 1); |
| 140 | alloc_stats.clear_ctor_counters(); |
| 141 | |
| 142 | Vect v2 = std::move(v); |
| 143 | assert(alloc_stats.count == 2); |
| 144 | assert(alloc_stats.copied == 0); |
| 145 | assert(alloc_stats.moved == 1); |
| 146 | { |
| 147 | const test_allocator<int>& a1 = v.get_allocator(); |
| 148 | assert(a1.get_id() == test_alloc_base::moved_value); |
| 149 | assert(a1.get_data() == 42); |
| 150 | |
| 151 | const test_allocator<int>& a2 = v2.get_allocator(); |
| 152 | assert(a2.get_id() == 101); |
| 153 | assert(a2.get_data() == 42); |
| 154 | |
| 155 | assert(a1 == a2); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | int main(int, char**) { |
| 163 | tests(); |
| 164 | #if TEST_STD_VER > 17 |
| 165 | static_assert(tests()); |
| 166 | #endif |
| 167 | return 0; |
| 168 | } |
| 169 | |