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 int main(int, char**) 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(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 alloc_stats.clear(); 105 using Vect = std::vector<int, test_allocator<int> >; 106 Vect v(test_allocator<int>(42, 101, &alloc_stats)); 107 assert(alloc_stats.count == 1); 108 assert(alloc_stats.copied == 1); 109 assert(alloc_stats.moved == 0); 110 { 111 const test_allocator<int>& a = v.get_allocator(); 112 assert(a.get_data() == 42); 113 assert(a.get_id() == 101); 114 } 115 assert(alloc_stats.count == 1); 116 alloc_stats.clear_ctor_counters(); 117 118 Vect v2 = std::move(v); 119 assert(alloc_stats.count == 2); 120 assert(alloc_stats.copied == 0); 121 assert(alloc_stats.moved == 1); 122 { 123 const test_allocator<int>& a = v.get_allocator(); 124 assert(a.get_id() == test_alloc_base::moved_value); 125 assert(a.get_data() == test_alloc_base::moved_value); 126 } 127 { 128 const test_allocator<int>& a = v2.get_allocator(); 129 assert(a.get_id() == 101); 130 assert(a.get_data() == 42); 131 } 132 } 133 134 return 0; 135 } 136