1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <vector> 11 12 // template <class T, class Alloc> 13 // void swap(vector<T,Alloc>& x, vector<T,Alloc>& y); 14 15 #include <vector> 16 #include <iterator> 17 #include <cassert> 18 #include "test_allocator.h" 19 #include "min_allocator.h" 20 #include "asan_testing.h" 21 22 int main() 23 { 24 { 25 int a1[] = {1, 3, 7, 9, 10}; 26 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 27 std::vector<int> c1(a1, a1+sizeof(a1)/sizeof(a1[0])); 28 std::vector<int> c2(a2, a2+sizeof(a2)/sizeof(a2[0])); 29 assert(is_contiguous_container_asan_correct(c1)); 30 assert(is_contiguous_container_asan_correct(c2)); 31 swap(c1, c2); 32 assert(c1 == std::vector<int>(a2, a2+sizeof(a2)/sizeof(a2[0]))); 33 assert(c2 == std::vector<int>(a1, a1+sizeof(a1)/sizeof(a1[0]))); 34 assert(is_contiguous_container_asan_correct(c1)); 35 assert(is_contiguous_container_asan_correct(c2)); 36 } 37 { 38 int a1[] = {1, 3, 7, 9, 10}; 39 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 40 std::vector<int> c1(a1, a1); 41 std::vector<int> c2(a2, a2+sizeof(a2)/sizeof(a2[0])); 42 assert(is_contiguous_container_asan_correct(c1)); 43 assert(is_contiguous_container_asan_correct(c2)); 44 swap(c1, c2); 45 assert(c1 == std::vector<int>(a2, a2+sizeof(a2)/sizeof(a2[0]))); 46 assert(c2.empty()); 47 assert(distance(c2.begin(), c2.end()) == 0); 48 assert(is_contiguous_container_asan_correct(c1)); 49 assert(is_contiguous_container_asan_correct(c2)); 50 } 51 { 52 int a1[] = {1, 3, 7, 9, 10}; 53 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 54 std::vector<int> c1(a1, a1+sizeof(a1)/sizeof(a1[0])); 55 std::vector<int> c2(a2, a2); 56 assert(is_contiguous_container_asan_correct(c1)); 57 assert(is_contiguous_container_asan_correct(c2)); 58 swap(c1, c2); 59 assert(c1.empty()); 60 assert(distance(c1.begin(), c1.end()) == 0); 61 assert(c2 == std::vector<int>(a1, a1+sizeof(a1)/sizeof(a1[0]))); 62 assert(is_contiguous_container_asan_correct(c1)); 63 assert(is_contiguous_container_asan_correct(c2)); 64 } 65 { 66 int a1[] = {1, 3, 7, 9, 10}; 67 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 68 std::vector<int> c1(a1, a1); 69 std::vector<int> c2(a2, a2); 70 assert(is_contiguous_container_asan_correct(c1)); 71 assert(is_contiguous_container_asan_correct(c2)); 72 swap(c1, c2); 73 assert(c1.empty()); 74 assert(distance(c1.begin(), c1.end()) == 0); 75 assert(c2.empty()); 76 assert(distance(c2.begin(), c2.end()) == 0); 77 assert(is_contiguous_container_asan_correct(c1)); 78 assert(is_contiguous_container_asan_correct(c2)); 79 } 80 #ifndef _LIBCPP_DEBUG_LEVEL 81 // This test known to result in undefined behavior detected by _LIBCPP_DEBUG_LEVEL >= 1 82 { 83 int a1[] = {1, 3, 7, 9, 10}; 84 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 85 typedef test_allocator<int> A; 86 std::vector<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1)); 87 std::vector<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2)); 88 swap(c1, c2); 89 assert((c1 == std::vector<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 90 assert(c1.get_allocator() == A(1)); 91 assert((c2 == std::vector<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 92 assert(c2.get_allocator() == A(2)); 93 } 94 #endif 95 { 96 int a1[] = {1, 3, 7, 9, 10}; 97 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 98 typedef other_allocator<int> A; 99 std::vector<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1)); 100 std::vector<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2)); 101 assert(is_contiguous_container_asan_correct(c1)); 102 assert(is_contiguous_container_asan_correct(c2)); 103 swap(c1, c2); 104 assert((c1 == std::vector<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 105 assert(c1.get_allocator() == A(2)); 106 assert((c2 == std::vector<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 107 assert(c2.get_allocator() == A(1)); 108 assert(is_contiguous_container_asan_correct(c1)); 109 assert(is_contiguous_container_asan_correct(c2)); 110 } 111 #if __cplusplus >= 201103L 112 { 113 int a1[] = {1, 3, 7, 9, 10}; 114 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 115 std::vector<int, min_allocator<int>> c1(a1, a1+sizeof(a1)/sizeof(a1[0])); 116 std::vector<int, min_allocator<int>> c2(a2, a2+sizeof(a2)/sizeof(a2[0])); 117 assert(is_contiguous_container_asan_correct(c1)); 118 assert(is_contiguous_container_asan_correct(c2)); 119 swap(c1, c2); 120 assert((c1 == std::vector<int, min_allocator<int>>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 121 assert((c2 == std::vector<int, min_allocator<int>>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 122 assert(is_contiguous_container_asan_correct(c1)); 123 assert(is_contiguous_container_asan_correct(c2)); 124 } 125 { 126 int a1[] = {1, 3, 7, 9, 10}; 127 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 128 std::vector<int, min_allocator<int>> c1(a1, a1); 129 std::vector<int, min_allocator<int>> c2(a2, a2+sizeof(a2)/sizeof(a2[0])); 130 assert(is_contiguous_container_asan_correct(c1)); 131 assert(is_contiguous_container_asan_correct(c2)); 132 swap(c1, c2); 133 assert((c1 == std::vector<int, min_allocator<int>>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 134 assert(c2.empty()); 135 assert(distance(c2.begin(), c2.end()) == 0); 136 assert(is_contiguous_container_asan_correct(c1)); 137 assert(is_contiguous_container_asan_correct(c2)); 138 } 139 { 140 int a1[] = {1, 3, 7, 9, 10}; 141 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 142 std::vector<int, min_allocator<int>> c1(a1, a1+sizeof(a1)/sizeof(a1[0])); 143 std::vector<int, min_allocator<int>> c2(a2, a2); 144 assert(is_contiguous_container_asan_correct(c1)); 145 assert(is_contiguous_container_asan_correct(c2)); 146 swap(c1, c2); 147 assert(c1.empty()); 148 assert(distance(c1.begin(), c1.end()) == 0); 149 assert((c2 == std::vector<int, min_allocator<int>>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 150 assert(is_contiguous_container_asan_correct(c1)); 151 assert(is_contiguous_container_asan_correct(c2)); 152 } 153 { 154 int a1[] = {1, 3, 7, 9, 10}; 155 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 156 std::vector<int, min_allocator<int>> c1(a1, a1); 157 std::vector<int, min_allocator<int>> c2(a2, a2); 158 assert(is_contiguous_container_asan_correct(c1)); 159 assert(is_contiguous_container_asan_correct(c2)); 160 swap(c1, c2); 161 assert(c1.empty()); 162 assert(distance(c1.begin(), c1.end()) == 0); 163 assert(c2.empty()); 164 assert(distance(c2.begin(), c2.end()) == 0); 165 assert(is_contiguous_container_asan_correct(c1)); 166 assert(is_contiguous_container_asan_correct(c2)); 167 } 168 #ifndef _LIBCPP_DEBUG_LEVEL 169 // This test known to result in undefined behavior detected by _LIBCPP_DEBUG_LEVEL >= 1 170 { 171 int a1[] = {1, 3, 7, 9, 10}; 172 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 173 typedef min_allocator<int> A; 174 std::vector<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A()); 175 std::vector<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A()); 176 assert(is_contiguous_container_asan_correct(c1)); 177 assert(is_contiguous_container_asan_correct(c2)); 178 swap(c1, c2); 179 assert((c1 == std::vector<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 180 assert(c1.get_allocator() == A()); 181 assert((c2 == std::vector<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 182 assert(c2.get_allocator() == A()); 183 assert(is_contiguous_container_asan_correct(c1)); 184 assert(is_contiguous_container_asan_correct(c2)); 185 } 186 #endif 187 #endif 188 } 189