| 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 | // <forward_list> |
| 10 | |
| 11 | // void swap(forward_list& x); // constexpr since C++26 |
| 12 | |
| 13 | #include <forward_list> |
| 14 | #include <cassert> |
| 15 | #include <iterator> |
| 16 | |
| 17 | #include "test_macros.h" |
| 18 | #include "test_allocator.h" |
| 19 | #include "min_allocator.h" |
| 20 | |
| 21 | TEST_CONSTEXPR_CXX26 bool test() { |
| 22 | { |
| 23 | typedef int T; |
| 24 | typedef test_allocator<T> A; |
| 25 | typedef std::forward_list<T, A> C; |
| 26 | const T t1[] = {0, 1, 2, 3, 4, 5}; |
| 27 | C c1(std::begin(t1), std::end(t1), A(1, 1)); |
| 28 | const T t2[] = {10, 11, 12}; |
| 29 | C c2(std::begin(arr: t2), std::end(arr: t2), A(1, 2)); |
| 30 | c1.swap(c2); |
| 31 | |
| 32 | assert(std::distance(c1.begin(), c1.end()) == 3); |
| 33 | assert(*std::next(c1.begin(), 0) == 10); |
| 34 | assert(*std::next(c1.begin(), 1) == 11); |
| 35 | assert(*std::next(c1.begin(), 2) == 12); |
| 36 | assert(c1.get_allocator().get_id() == 1); |
| 37 | |
| 38 | assert(std::distance(c2.begin(), c2.end()) == 6); |
| 39 | assert(*std::next(c2.begin(), 0) == 0); |
| 40 | assert(*std::next(c2.begin(), 1) == 1); |
| 41 | assert(*std::next(c2.begin(), 2) == 2); |
| 42 | assert(*std::next(c2.begin(), 3) == 3); |
| 43 | assert(*std::next(c2.begin(), 4) == 4); |
| 44 | assert(*std::next(c2.begin(), 5) == 5); |
| 45 | assert(c2.get_allocator().get_id() == 2); |
| 46 | } |
| 47 | { |
| 48 | typedef int T; |
| 49 | typedef test_allocator<T> A; |
| 50 | typedef std::forward_list<T, A> C; |
| 51 | const T t1[] = {0, 1, 2, 3, 4, 5}; |
| 52 | C c1(std::begin(arr: t1), std::end(arr: t1), A(1, 1)); |
| 53 | C c2(A(1, 2)); |
| 54 | c1.swap(list&: c2); |
| 55 | |
| 56 | assert(std::distance(c1.begin(), c1.end()) == 0); |
| 57 | assert(c1.get_allocator().get_id() == 1); |
| 58 | |
| 59 | assert(std::distance(c2.begin(), c2.end()) == 6); |
| 60 | assert(*std::next(c2.begin(), 0) == 0); |
| 61 | assert(*std::next(c2.begin(), 1) == 1); |
| 62 | assert(*std::next(c2.begin(), 2) == 2); |
| 63 | assert(*std::next(c2.begin(), 3) == 3); |
| 64 | assert(*std::next(c2.begin(), 4) == 4); |
| 65 | assert(*std::next(c2.begin(), 5) == 5); |
| 66 | assert(c2.get_allocator().get_id() == 2); |
| 67 | } |
| 68 | { |
| 69 | typedef int T; |
| 70 | typedef test_allocator<T> A; |
| 71 | typedef std::forward_list<T, A> C; |
| 72 | C c1(A(1, 1)); |
| 73 | const T t2[] = {10, 11, 12}; |
| 74 | C c2(std::begin(arr: t2), std::end(arr: t2), A(1, 2)); |
| 75 | c1.swap(list&: c2); |
| 76 | |
| 77 | assert(std::distance(c1.begin(), c1.end()) == 3); |
| 78 | assert(*std::next(c1.begin(), 0) == 10); |
| 79 | assert(*std::next(c1.begin(), 1) == 11); |
| 80 | assert(*std::next(c1.begin(), 2) == 12); |
| 81 | assert(c1.get_allocator().get_id() == 1); |
| 82 | |
| 83 | assert(std::distance(c2.begin(), c2.end()) == 0); |
| 84 | assert(c2.get_allocator().get_id() == 2); |
| 85 | } |
| 86 | { |
| 87 | typedef int T; |
| 88 | typedef test_allocator<T> A; |
| 89 | typedef std::forward_list<T, A> C; |
| 90 | C c1(A(1, 1)); |
| 91 | C c2(A(1, 2)); |
| 92 | c1.swap(list&: c2); |
| 93 | |
| 94 | assert(std::distance(c1.begin(), c1.end()) == 0); |
| 95 | assert(c1.get_allocator().get_id() == 1); |
| 96 | |
| 97 | assert(std::distance(c2.begin(), c2.end()) == 0); |
| 98 | assert(c2.get_allocator().get_id() == 2); |
| 99 | } |
| 100 | |
| 101 | { |
| 102 | typedef int T; |
| 103 | typedef other_allocator<T> A; |
| 104 | typedef std::forward_list<T, A> C; |
| 105 | const T t1[] = {0, 1, 2, 3, 4, 5}; |
| 106 | C c1(std::begin(arr: t1), std::end(arr: t1), A(1)); |
| 107 | const T t2[] = {10, 11, 12}; |
| 108 | C c2(std::begin(arr: t2), std::end(arr: t2), A(2)); |
| 109 | c1.swap(list&: c2); |
| 110 | |
| 111 | assert(std::distance(c1.begin(), c1.end()) == 3); |
| 112 | assert(*std::next(c1.begin(), 0) == 10); |
| 113 | assert(*std::next(c1.begin(), 1) == 11); |
| 114 | assert(*std::next(c1.begin(), 2) == 12); |
| 115 | assert(c1.get_allocator() == A(2)); |
| 116 | |
| 117 | assert(std::distance(c2.begin(), c2.end()) == 6); |
| 118 | assert(*std::next(c2.begin(), 0) == 0); |
| 119 | assert(*std::next(c2.begin(), 1) == 1); |
| 120 | assert(*std::next(c2.begin(), 2) == 2); |
| 121 | assert(*std::next(c2.begin(), 3) == 3); |
| 122 | assert(*std::next(c2.begin(), 4) == 4); |
| 123 | assert(*std::next(c2.begin(), 5) == 5); |
| 124 | assert(c2.get_allocator() == A(1)); |
| 125 | } |
| 126 | { |
| 127 | typedef int T; |
| 128 | typedef other_allocator<T> A; |
| 129 | typedef std::forward_list<T, A> C; |
| 130 | const T t1[] = {0, 1, 2, 3, 4, 5}; |
| 131 | C c1(std::begin(arr: t1), std::end(arr: t1), A(1)); |
| 132 | C c2(A(2)); |
| 133 | c1.swap(list&: c2); |
| 134 | |
| 135 | assert(std::distance(c1.begin(), c1.end()) == 0); |
| 136 | assert(c1.get_allocator() == A(2)); |
| 137 | |
| 138 | assert(std::distance(c2.begin(), c2.end()) == 6); |
| 139 | assert(*std::next(c2.begin(), 0) == 0); |
| 140 | assert(*std::next(c2.begin(), 1) == 1); |
| 141 | assert(*std::next(c2.begin(), 2) == 2); |
| 142 | assert(*std::next(c2.begin(), 3) == 3); |
| 143 | assert(*std::next(c2.begin(), 4) == 4); |
| 144 | assert(*std::next(c2.begin(), 5) == 5); |
| 145 | assert(c2.get_allocator() == A(1)); |
| 146 | } |
| 147 | { |
| 148 | typedef int T; |
| 149 | typedef other_allocator<T> A; |
| 150 | typedef std::forward_list<T, A> C; |
| 151 | C c1(A(1)); |
| 152 | const T t2[] = {10, 11, 12}; |
| 153 | C c2(std::begin(arr: t2), std::end(arr: t2), A(2)); |
| 154 | c1.swap(list&: c2); |
| 155 | |
| 156 | assert(std::distance(c1.begin(), c1.end()) == 3); |
| 157 | assert(*std::next(c1.begin(), 0) == 10); |
| 158 | assert(*std::next(c1.begin(), 1) == 11); |
| 159 | assert(*std::next(c1.begin(), 2) == 12); |
| 160 | assert(c1.get_allocator() == A(2)); |
| 161 | |
| 162 | assert(std::distance(c2.begin(), c2.end()) == 0); |
| 163 | assert(c2.get_allocator() == A(1)); |
| 164 | } |
| 165 | { |
| 166 | typedef int T; |
| 167 | typedef other_allocator<T> A; |
| 168 | typedef std::forward_list<T, A> C; |
| 169 | C c1(A(1)); |
| 170 | C c2(A(2)); |
| 171 | c1.swap(list&: c2); |
| 172 | |
| 173 | assert(std::distance(c1.begin(), c1.end()) == 0); |
| 174 | assert(c1.get_allocator() == A(2)); |
| 175 | |
| 176 | assert(std::distance(c2.begin(), c2.end()) == 0); |
| 177 | assert(c2.get_allocator() == A(1)); |
| 178 | } |
| 179 | #if TEST_STD_VER >= 11 |
| 180 | { |
| 181 | typedef int T; |
| 182 | typedef min_allocator<T> A; |
| 183 | typedef std::forward_list<T, A> C; |
| 184 | const T t1[] = {0, 1, 2, 3, 4, 5}; |
| 185 | C c1(std::begin(t1), std::end(t1), A()); |
| 186 | const T t2[] = {10, 11, 12}; |
| 187 | C c2(std::begin(t2), std::end(t2), A()); |
| 188 | c1.swap(c2); |
| 189 | |
| 190 | assert(std::distance(c1.begin(), c1.end()) == 3); |
| 191 | assert(*std::next(c1.begin(), 0) == 10); |
| 192 | assert(*std::next(c1.begin(), 1) == 11); |
| 193 | assert(*std::next(c1.begin(), 2) == 12); |
| 194 | assert(c1.get_allocator() == A()); |
| 195 | |
| 196 | assert(std::distance(c2.begin(), c2.end()) == 6); |
| 197 | assert(*std::next(c2.begin(), 0) == 0); |
| 198 | assert(*std::next(c2.begin(), 1) == 1); |
| 199 | assert(*std::next(c2.begin(), 2) == 2); |
| 200 | assert(*std::next(c2.begin(), 3) == 3); |
| 201 | assert(*std::next(c2.begin(), 4) == 4); |
| 202 | assert(*std::next(c2.begin(), 5) == 5); |
| 203 | assert(c2.get_allocator() == A()); |
| 204 | } |
| 205 | { |
| 206 | typedef int T; |
| 207 | typedef min_allocator<T> A; |
| 208 | typedef std::forward_list<T, A> C; |
| 209 | const T t1[] = {0, 1, 2, 3, 4, 5}; |
| 210 | C c1(std::begin(t1), std::end(t1), A()); |
| 211 | C c2(A{}); |
| 212 | c1.swap(c2); |
| 213 | |
| 214 | assert(std::distance(c1.begin(), c1.end()) == 0); |
| 215 | assert(c1.get_allocator() == A()); |
| 216 | |
| 217 | assert(std::distance(c2.begin(), c2.end()) == 6); |
| 218 | assert(*std::next(c2.begin(), 0) == 0); |
| 219 | assert(*std::next(c2.begin(), 1) == 1); |
| 220 | assert(*std::next(c2.begin(), 2) == 2); |
| 221 | assert(*std::next(c2.begin(), 3) == 3); |
| 222 | assert(*std::next(c2.begin(), 4) == 4); |
| 223 | assert(*std::next(c2.begin(), 5) == 5); |
| 224 | assert(c2.get_allocator() == A()); |
| 225 | } |
| 226 | { |
| 227 | typedef int T; |
| 228 | typedef min_allocator<T> A; |
| 229 | typedef std::forward_list<T, A> C; |
| 230 | C c1(A{}); |
| 231 | const T t2[] = {10, 11, 12}; |
| 232 | C c2(std::begin(t2), std::end(t2), A()); |
| 233 | c1.swap(c2); |
| 234 | |
| 235 | assert(std::distance(c1.begin(), c1.end()) == 3); |
| 236 | assert(*std::next(c1.begin(), 0) == 10); |
| 237 | assert(*std::next(c1.begin(), 1) == 11); |
| 238 | assert(*std::next(c1.begin(), 2) == 12); |
| 239 | assert(c1.get_allocator() == A()); |
| 240 | |
| 241 | assert(std::distance(c2.begin(), c2.end()) == 0); |
| 242 | assert(c2.get_allocator() == A()); |
| 243 | } |
| 244 | { |
| 245 | typedef int T; |
| 246 | typedef min_allocator<T> A; |
| 247 | typedef std::forward_list<T, A> C; |
| 248 | C c1(A{}); |
| 249 | C c2(A{}); |
| 250 | c1.swap(c2); |
| 251 | |
| 252 | assert(std::distance(c1.begin(), c1.end()) == 0); |
| 253 | assert(c1.get_allocator() == A()); |
| 254 | |
| 255 | assert(std::distance(c2.begin(), c2.end()) == 0); |
| 256 | assert(c2.get_allocator() == A()); |
| 257 | } |
| 258 | #endif |
| 259 | |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | int main(int, char**) { |
| 264 | assert(test()); |
| 265 | #if TEST_STD_VER >= 26 |
| 266 | static_assert(test()); |
| 267 | #endif |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |