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// <vector>
10
11// template <class T, class Alloc>
12// void swap(vector<T,Alloc>& x, vector<T,Alloc>& y);
13
14#include <vector>
15#include <iterator>
16#include <cassert>
17#include "test_macros.h"
18#include "test_allocator.h"
19#include "min_allocator.h"
20#include "asan_testing.h"
21
22template <typename A>
23TEST_CONSTEXPR_CXX20 void test_with_allocator() {
24 {
25 int a1[] = {1, 3, 7, 9, 10};
26 int a2[] = {0, 2, 4, 5, 6, 8, 11};
27 std::vector<int, A> c1(a1, a1 + sizeof(a1) / sizeof(a1[0]));
28 std::vector<int, A> 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, A>(a2, a2 + sizeof(a2) / sizeof(a2[0]))));
33 assert((c2 == std::vector<int, A>(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, A> c1(a1, a1);
41 std::vector<int, A> 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, A>(a2, a2 + sizeof(a2) / sizeof(a2[0]))));
46 assert(c2.empty());
47 assert(std::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, A> c1(a1, a1 + sizeof(a1) / sizeof(a1[0]));
55 std::vector<int, A> 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(std::distance(c1.begin(), c1.end()) == 0);
61 assert((c2 == std::vector<int, A>(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, A> c1(a1, a1);
69 std::vector<int, A> 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(std::distance(c1.begin(), c1.end()) == 0);
75 assert(c2.empty());
76 assert(std::distance(c2.begin(), c2.end()) == 0);
77 assert(is_contiguous_container_asan_correct(c1));
78 assert(is_contiguous_container_asan_correct(c2));
79 }
80 {
81 int a1[] = {1, 3, 7, 9, 10};
82 int a2[] = {0, 2, 4, 5, 6, 8, 11};
83 std::vector<int, A> c1(a1, a1 + sizeof(a1) / sizeof(a1[0]), A());
84 std::vector<int, A> c2(a2, a2 + sizeof(a2) / sizeof(a2[0]), A());
85 assert(is_contiguous_container_asan_correct(c1));
86 assert(is_contiguous_container_asan_correct(c2));
87 swap(c1, c2);
88 assert((c1 == std::vector<int, A>(a2, a2 + sizeof(a2) / sizeof(a2[0]))));
89 assert(c1.get_allocator() == A());
90 assert((c2 == std::vector<int, A>(a1, a1 + sizeof(a1) / sizeof(a1[0]))));
91 assert(c2.get_allocator() == A());
92 assert(is_contiguous_container_asan_correct(c1));
93 assert(is_contiguous_container_asan_correct(c2));
94 }
95}
96
97TEST_CONSTEXPR_CXX20 bool tests()
98{
99 {
100 int a1[] = {1, 3, 7, 9, 10};
101 int a2[] = {0, 2, 4, 5, 6, 8, 11};
102 std::vector<int> c1(a1, a1+sizeof(a1)/sizeof(a1[0]));
103 std::vector<int> c2(a2, a2+sizeof(a2)/sizeof(a2[0]));
104 assert(is_contiguous_container_asan_correct(c1));
105 assert(is_contiguous_container_asan_correct(c2));
106 swap(x&: c1, y&: c2);
107 assert(c1 == std::vector<int>(a2, a2+sizeof(a2)/sizeof(a2[0])));
108 assert(c2 == std::vector<int>(a1, a1+sizeof(a1)/sizeof(a1[0])));
109 assert(is_contiguous_container_asan_correct(c1));
110 assert(is_contiguous_container_asan_correct(c2));
111 }
112 {
113 int a1[] = {1, 3, 7, 9, 10};
114 int a2[] = {0, 2, 4, 5, 6, 8, 11};
115 std::vector<int> c1(a1, a1);
116 std::vector<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(x&: c1, y&: c2);
120 assert(c1 == std::vector<int>(a2, a2+sizeof(a2)/sizeof(a2[0])));
121 assert(c2.empty());
122 assert(std::distance(c2.begin(), c2.end()) == 0);
123 assert(is_contiguous_container_asan_correct(c1));
124 assert(is_contiguous_container_asan_correct(c2));
125 }
126 {
127 int a1[] = {1, 3, 7, 9, 10};
128 int a2[] = {0, 2, 4, 5, 6, 8, 11};
129 std::vector<int> c1(a1, a1+sizeof(a1)/sizeof(a1[0]));
130 std::vector<int> c2(a2, a2);
131 assert(is_contiguous_container_asan_correct(c1));
132 assert(is_contiguous_container_asan_correct(c2));
133 swap(x&: c1, y&: c2);
134 assert(c1.empty());
135 assert(std::distance(c1.begin(), c1.end()) == 0);
136 assert(c2 == std::vector<int>(a1, a1+sizeof(a1)/sizeof(a1[0])));
137 assert(is_contiguous_container_asan_correct(c1));
138 assert(is_contiguous_container_asan_correct(c2));
139 }
140 {
141 int a1[] = {1, 3, 7, 9, 10};
142 int a2[] = {0, 2, 4, 5, 6, 8, 11};
143 std::vector<int> c1(a1, a1);
144 std::vector<int> c2(a2, a2);
145 assert(is_contiguous_container_asan_correct(c1));
146 assert(is_contiguous_container_asan_correct(c2));
147 swap(x&: c1, y&: c2);
148 assert(c1.empty());
149 assert(std::distance(c1.begin(), c1.end()) == 0);
150 assert(c2.empty());
151 assert(std::distance(c2.begin(), c2.end()) == 0);
152 assert(is_contiguous_container_asan_correct(c1));
153 assert(is_contiguous_container_asan_correct(c2));
154 }
155 {
156 int a1[] = {1, 3, 7, 9, 10};
157 int a2[] = {0, 2, 4, 5, 6, 8, 11};
158 typedef test_allocator<int> A;
159 std::vector<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1, 1));
160 std::vector<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(1, 2));
161 swap(c1, c2);
162 assert((c1 == std::vector<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
163 assert(c1.get_allocator().get_id() == 1);
164 assert((c2 == std::vector<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
165 assert(c2.get_allocator().get_id() == 2);
166 }
167 {
168 int a1[] = {1, 3, 7, 9, 10};
169 int a2[] = {0, 2, 4, 5, 6, 8, 11};
170 typedef other_allocator<int> A;
171 std::vector<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1));
172 std::vector<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2));
173 assert(is_contiguous_container_asan_correct(c1));
174 assert(is_contiguous_container_asan_correct(c2));
175 swap(x&: c1, y&: c2);
176 assert((c1 == std::vector<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
177 assert(c1.get_allocator() == A(2));
178 assert((c2 == std::vector<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
179 assert(c2.get_allocator() == A(1));
180 assert(is_contiguous_container_asan_correct(c1));
181 assert(is_contiguous_container_asan_correct(c2));
182 }
183#if TEST_STD_VER >= 11
184 test_with_allocator<min_allocator<int>>();
185 test_with_allocator<safe_allocator<int>>();
186#endif
187
188 return true;
189}
190
191int main(int, char**)
192{
193 tests();
194#if TEST_STD_VER > 17
195 static_assert(tests());
196#endif
197 return 0;
198}
199

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