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
main()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 {
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::list<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1));
67 std::list<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(1));
68 swap(c1, c2);
69 assert((c1 == std::list<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
70 assert(c1.get_allocator() == A(1));
71 assert((c2 == std::list<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
72 assert(c2.get_allocator() == A(1));
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::list<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1));
79 std::list<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2));
80 swap(c1, c2);
81 assert((c1 == std::list<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
82 assert(c1.get_allocator() == A(2));
83 assert((c2 == std::list<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
84 assert(c2.get_allocator() == A(1));
85 }
86 #if TEST_STD_VER >= 11
87 {
88 int a1[] = {1, 3, 7, 9, 10};
89 int a2[] = {0, 2, 4, 5, 6, 8, 11};
90 std::list<int, min_allocator<int>> c1(a1, a1+sizeof(a1)/sizeof(a1[0]));
91 std::list<int, min_allocator<int>> c2(a2, a2+sizeof(a2)/sizeof(a2[0]));
92 swap(c1, c2);
93 assert((c1 == std::list<int, min_allocator<int>>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
94 assert((c2 == std::list<int, min_allocator<int>>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
95 }
96 {
97 int a1[] = {1, 3, 7, 9, 10};
98 int a2[] = {0, 2, 4, 5, 6, 8, 11};
99 std::list<int, min_allocator<int>> c1(a1, a1);
100 std::list<int, min_allocator<int>> c2(a2, a2+sizeof(a2)/sizeof(a2[0]));
101 swap(c1, c2);
102 assert((c1 == std::list<int, min_allocator<int>>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
103 assert(c2.empty());
104 assert(distance(c2.begin(), c2.end()) == 0);
105 }
106 {
107 int a1[] = {1, 3, 7, 9, 10};
108 int a2[] = {0, 2, 4, 5, 6, 8, 11};
109 std::list<int, min_allocator<int>> c1(a1, a1+sizeof(a1)/sizeof(a1[0]));
110 std::list<int, min_allocator<int>> c2(a2, a2);
111 swap(c1, c2);
112 assert(c1.empty());
113 assert(distance(c1.begin(), c1.end()) == 0);
114 assert((c2 == std::list<int, min_allocator<int>>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
115 }
116 {
117 int a1[] = {1, 3, 7, 9, 10};
118 int a2[] = {0, 2, 4, 5, 6, 8, 11};
119 std::list<int, min_allocator<int>> c1(a1, a1);
120 std::list<int, min_allocator<int>> c2(a2, a2);
121 swap(c1, c2);
122 assert(c1.empty());
123 assert(distance(c1.begin(), c1.end()) == 0);
124 assert(c2.empty());
125 assert(distance(c2.begin(), c2.end()) == 0);
126 }
127 {
128 int a1[] = {1, 3, 7, 9, 10};
129 int a2[] = {0, 2, 4, 5, 6, 8, 11};
130 typedef min_allocator<int> A;
131 std::list<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A());
132 std::list<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A());
133 swap(c1, c2);
134 assert((c1 == std::list<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
135 assert(c1.get_allocator() == A());
136 assert((c2 == std::list<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
137 assert(c2.get_allocator() == A());
138 }
139 #endif
140 }
141