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// <list>
10
11// template <class T, class Alloc>
12// void swap(list<T,Alloc>& x, list<T,Alloc>& y);
13
14#include <list>
15#include <cassert>
16#include "test_macros.h"
17#include "test_allocator.h"
18#include "min_allocator.h"
19
20int main(int, char**)
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 + 5);
26 std::list<int> c2(a2, a2 + 7);
27 std::list<int>::iterator it1 = c1.begin();
28 std::list<int>::const_iterator it2 = c2.begin();
29 swap(x&: c1, y&: c2);
30 assert(c1 == std::list<int>(a2, a2 + 7));
31 assert(c2 == std::list<int>(a1, a1 + 5));
32 assert(it1 == c2.begin()); // Iterators remain valid
33 assert(it2 == c1.begin()); // Iterators remain valid
34 }
35 {
36 int a2[] = {0, 2, 4, 5, 6, 8, 11};
37 std::list<int> c1;
38 std::list<int> c2(a2, a2 + 7);
39 swap(x&: c1, y&: c2);
40 assert(c1 == std::list<int>(a2, a2 + 7));
41 assert(c2.empty());
42 assert(std::distance(c2.begin(), c2.end()) == 0);
43 }
44 {
45 int a1[] = {1, 3, 7, 9, 10};
46 std::list<int> c1(a1, a1 + 5);
47 std::list<int> c2;
48 swap(x&: c1, y&: c2);
49 assert(c1.empty());
50 assert(std::distance(c1.begin(), c1.end()) == 0);
51 assert(c2 == std::list<int>(a1, a1 + 5));
52 }
53 {
54 std::list<int> c1;
55 std::list<int> c2;
56 swap(x&: c1, y&: c2);
57 assert(c1.empty());
58 assert(std::distance(c1.begin(), c1.end()) == 0);
59 assert(c2.empty());
60 assert(std::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 + 5, A(1));
67 std::list<int, A> c2(a2, a2 + 7, A(1));
68 swap(c1, c2);
69 assert((c1 == std::list<int, A>(a2, a2 + 7)));
70 assert(c1.get_allocator() == A(1));
71 assert((c2 == std::list<int, A>(a1, a1 + 5)));
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 + 5, A(1));
79 std::list<int, A> c2(a2, a2 + 7, A(2));
80 swap(x&: c1, y&: c2);
81 assert((c1 == std::list<int, A>(a2, a2 + 7)));
82 assert(c1.get_allocator() == A(2));
83 assert((c2 == std::list<int, A>(a1, a1 + 5)));
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 + 5);
91 std::list<int, min_allocator<int>> c2(a2, a2 + 7);
92 swap(c1, c2);
93 assert((c1 == std::list<int, min_allocator<int>>(a2, a2 + 7)));
94 assert((c2 == std::list<int, min_allocator<int>>(a1, a1 + 5)));
95 }
96 {
97 int a2[] = {0, 2, 4, 5, 6, 8, 11};
98 std::list<int, min_allocator<int>> c1;
99 std::list<int, min_allocator<int>> c2(a2, a2 + 7);
100 swap(c1, c2);
101 assert((c1 == std::list<int, min_allocator<int>>(a2, a2 + 7)));
102 assert(c2.empty());
103 assert(std::distance(c2.begin(), c2.end()) == 0);
104 }
105 {
106 int a1[] = {1, 3, 7, 9, 10};
107 std::list<int, min_allocator<int>> c1(a1, a1 + 5);
108 std::list<int, min_allocator<int>> c2;
109 swap(c1, c2);
110 assert(c1.empty());
111 assert(std::distance(c1.begin(), c1.end()) == 0);
112 assert((c2 == std::list<int, min_allocator<int>>(a1, a1 + 5)));
113 }
114 {
115 std::list<int, min_allocator<int>> c1;
116 std::list<int, min_allocator<int>> c2;
117 swap(c1, c2);
118 assert(c1.empty());
119 assert(std::distance(c1.begin(), c1.end()) == 0);
120 assert(c2.empty());
121 assert(std::distance(c2.begin(), c2.end()) == 0);
122 }
123 {
124 int a1[] = {1, 3, 7, 9, 10};
125 int a2[] = {0, 2, 4, 5, 6, 8, 11};
126 typedef min_allocator<int> A;
127 std::list<int, A> c1(a1, a1 + 5, A());
128 std::list<int, A> c2(a2, a2 + 7, A());
129 swap(c1, c2);
130 assert((c1 == std::list<int, A>(a2, a2 + 7)));
131 assert(c1.get_allocator() == A());
132 assert((c2 == std::list<int, A>(a1, a1 + 5)));
133 assert(c2.get_allocator() == A());
134 }
135#endif
136
137 return 0;
138}
139

source code of libcxx/test/std/containers/sequences/list/list.special/swap.pass.cpp