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 // <deque> 11 12 // template <class T, class A> 13 // void swap(deque<T, A>& x, deque<T, A>& y); 14 15 #include <deque> 16 #include <cassert> 17 #include "../../../test_allocator.h" 18 19 std::deque<int> 20 make(int size, int start = 0 ) 21 { 22 const int b = 4096 / sizeof(int); 23 int init = 0; 24 if (start > 0) 25 { 26 init = (start+1) / b + ((start+1) % b != 0); 27 init *= b; 28 --init; 29 } 30 std::deque<int> c(init, 0); 31 for (int i = 0; i < init-start; ++i) 32 c.pop_back(); 33 for (int i = 0; i < size; ++i) 34 c.push_back(i); 35 for (int i = 0; i < start; ++i) 36 c.pop_front(); 37 return c; 38 }; 39 40 void testN(int start, int N, int M) 41 { 42 typedef std::deque<int> C; 43 C c1 = make(N, start); 44 C c2 = make(M); 45 C c1_save = c1; 46 C c2_save = c2; 47 swap(c1, c2); 48 assert(c1 == c2_save); 49 assert(c2 == c1_save); 50 } 51 52 int main() 53 { 54 { 55 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049}; 56 const int N = sizeof(rng)/sizeof(rng[0]); 57 for (int i = 0; i < N; ++i) 58 for (int j = 0; j < N; ++j) 59 for (int k = 0; k < N; ++k) 60 testN(rng[i], rng[j], rng[k]); 61 } 62 { 63 int a1[] = {1, 3, 7, 9, 10}; 64 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 65 typedef test_allocator<int> A; 66 std::deque<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1)); 67 std::deque<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2)); 68 swap(c1, c2); 69 assert((c1 == std::deque<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 70 assert(c1.get_allocator() == A(1)); 71 assert((c2 == std::deque<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 72 assert(c2.get_allocator() == A(2)); 73 } 74 { 75 int a1[] = {1, 3, 7, 9, 10}; 76 int a2[] = {0, 2, 4, 5, 6, 8, 11}; 77 typedef other_allocator<int> A; 78 std::deque<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1)); 79 std::deque<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2)); 80 swap(c1, c2); 81 assert((c1 == std::deque<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 82 assert(c1.get_allocator() == A(2)); 83 assert((c2 == std::deque<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0])))); 84 assert(c2.get_allocator() == A(1)); 85 } 86 } 87