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 // <list> 11 12 // template <class T, class Alloc> 13 // void swap(list<T,Alloc>& x, list<T,Alloc>& y); 14 15 #include <list> 16 #include <cassert> 17 #include "test_allocator.h" 18 #include "min_allocator.h" 19 20 int main() 21 { 22 { 23 int a1[] = {1, 3, 7, 9, 10}; 24 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 25 std::list<int> c1(a1, a1+sizeof(a1)/sizeof(a1[0])); 26 std::list<int> c2(a2, a2+sizeof(a2)/sizeof(a2[0])); 27 swap(c1, c2); 28 assert(c1 == std::list<int>(a2, a2+sizeof(a2)/sizeof(a2[0]))); 29 assert(c2 == std::list<int>(a1, a1+sizeof(a1)/sizeof(a1[0]))); 30 } 31 { 32 int a1[] = {1, 3, 7, 9, 10}; 33 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 34 std::list<int> c1(a1, a1); 35 std::list<int> c2(a2, a2+sizeof(a2)/sizeof(a2[0])); 36 swap(c1, c2); 37 assert(c1 == std::list<int>(a2, a2+sizeof(a2)/sizeof(a2[0]))); 38 assert(c2.empty()); 39 assert(distance(c2.begin(), c2.end()) == 0); 40 } 41 { 42 int a1[] = {1, 3, 7, 9, 10}; 43 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 44 std::list<int> c1(a1, a1+sizeof(a1)/sizeof(a1[0])); 45 std::list<int> c2(a2, a2); 46 swap(c1, c2); 47 assert(c1.empty()); 48 assert(distance(c1.begin(), c1.end()) == 0); 49 assert(c2 == std::list<int>(a1, a1+sizeof(a1)/sizeof(a1[0]))); 50 } 51 { 52 int a1[] = {1, 3, 7, 9, 10}; 53 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 54 std::list<int> c1(a1, a1); 55 std::list<int> c2(a2, a2); 56 swap(c1, c2); 57 assert(c1.empty()); 58 assert(distance(c1.begin(), c1.end()) == 0); 59 assert(c2.empty()); 60 assert(distance(c2.begin(), c2.end()) == 0); 61 } 62 #ifndef _LIBCPP_DEBUG_LEVEL 63 // This test known to result in undefined behavior detected by _LIBCPP_DEBUG_LEVEL >= 1 64 { 65 int a1[] = {1, 3, 7, 9, 10}; 66 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 67 typedef test_allocator<int> A; 68 std::list<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1)); 69 std::list<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2)); 70 swap(c1, c2); 71 assert((c1 == std::list<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 72 assert(c1.get_allocator() == A(1)); 73 assert((c2 == std::list<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 74 assert(c2.get_allocator() == A(2)); 75 } 76 #endif 77 { 78 int a1[] = {1, 3, 7, 9, 10}; 79 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 80 typedef other_allocator<int> A; 81 std::list<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1)); 82 std::list<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2)); 83 swap(c1, c2); 84 assert((c1 == std::list<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 85 assert(c1.get_allocator() == A(2)); 86 assert((c2 == std::list<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 87 assert(c2.get_allocator() == A(1)); 88 } 89 #if __cplusplus >= 201103L 90 { 91 int a1[] = {1, 3, 7, 9, 10}; 92 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 93 std::list<int, min_allocator<int>> c1(a1, a1+sizeof(a1)/sizeof(a1[0])); 94 std::list<int, min_allocator<int>> c2(a2, a2+sizeof(a2)/sizeof(a2[0])); 95 swap(c1, c2); 96 assert((c1 == std::list<int, min_allocator<int>>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 97 assert((c2 == std::list<int, min_allocator<int>>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 98 } 99 { 100 int a1[] = {1, 3, 7, 9, 10}; 101 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 102 std::list<int, min_allocator<int>> c1(a1, a1); 103 std::list<int, min_allocator<int>> c2(a2, a2+sizeof(a2)/sizeof(a2[0])); 104 swap(c1, c2); 105 assert((c1 == std::list<int, min_allocator<int>>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 106 assert(c2.empty()); 107 assert(distance(c2.begin(), c2.end()) == 0); 108 } 109 { 110 int a1[] = {1, 3, 7, 9, 10}; 111 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 112 std::list<int, min_allocator<int>> c1(a1, a1+sizeof(a1)/sizeof(a1[0])); 113 std::list<int, min_allocator<int>> c2(a2, a2); 114 swap(c1, c2); 115 assert(c1.empty()); 116 assert(distance(c1.begin(), c1.end()) == 0); 117 assert((c2 == std::list<int, min_allocator<int>>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 118 } 119 { 120 int a1[] = {1, 3, 7, 9, 10}; 121 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 122 std::list<int, min_allocator<int>> c1(a1, a1); 123 std::list<int, min_allocator<int>> c2(a2, a2); 124 swap(c1, c2); 125 assert(c1.empty()); 126 assert(distance(c1.begin(), c1.end()) == 0); 127 assert(c2.empty()); 128 assert(distance(c2.begin(), c2.end()) == 0); 129 } 130 #ifndef _LIBCPP_DEBUG_LEVEL 131 // This test known to result in undefined behavior detected by _LIBCPP_DEBUG_LEVEL >= 1 132 { 133 int a1[] = {1, 3, 7, 9, 10}; 134 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 135 typedef min_allocator<int> A; 136 std::list<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A()); 137 std::list<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A()); 138 swap(c1, c2); 139 assert((c1 == std::list<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 140 assert(c1.get_allocator() == A()); 141 assert((c2 == std::list<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 142 assert(c2.get_allocator() == A()); 143 } 144 #endif 145 #endif 146 } 147