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// <deque>
10
11// template <class T, class A>
12// void swap(deque<T, A>& x, deque<T, A>& y);
13
14#include "asan_testing.h"
15#include <deque>
16#include <cassert>
17#include "test_macros.h"
18#include "test_allocator.h"
19#include "min_allocator.h"
20
21template <class C>
22C
23make(int size, int start = 0 )
24{
25 const int b = 4096 / sizeof(int);
26 int init = 0;
27 if (start > 0)
28 {
29 init = (start+1) / b + ((start+1) % b != 0);
30 init *= b;
31 --init;
32 }
33 C c(init, 0);
34 for (int i = 0; i < init-start; ++i)
35 c.pop_back();
36 for (int i = 0; i < size; ++i)
37 c.push_back(i);
38 for (int i = 0; i < start; ++i)
39 c.pop_front();
40 return c;
41}
42
43template <class C>
44void testN(int start, int N, int M)
45{
46 C c1 = make<C>(N, start);
47 C c2 = make<C>(M);
48 C c1_save = c1;
49 C c2_save = c2;
50 swap(c1, c2);
51 assert(c1 == c2_save);
52 assert(c2 == c1_save);
53 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
54 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
55 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1_save));
56 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2_save));
57}
58
59int main(int, char**)
60{
61 {
62 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
63 const int N = sizeof(rng)/sizeof(rng[0]);
64 for (int i = 0; i < N; ++i)
65 for (int j = 0; j < N; ++j)
66 for (int k = 0; k < N; ++k)
67 testN<std::deque<int> >(start: rng[i], N: rng[j], M: rng[k]);
68 }
69 {
70 int a1[] = {1, 3, 7, 9, 10};
71 int a2[] = {0, 2, 4, 5, 6, 8, 11};
72 typedef test_allocator<int> A;
73 std::deque<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1, 1));
74 std::deque<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(1, 2));
75 swap(c1, c2);
76 assert((c1 == std::deque<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
77 assert(c1.get_allocator().get_id() == 1);
78 assert((c2 == std::deque<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
79 assert(c2.get_allocator().get_id() == 2);
80 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
81 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
82 }
83 {
84 int a1[] = {1, 3, 7, 9, 10};
85 int a2[] = {0, 2, 4, 5, 6, 8, 11};
86 typedef other_allocator<int> A;
87 std::deque<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1));
88 std::deque<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2));
89 swap(x&: c1, y&: c2);
90 assert((c1 == std::deque<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
91 assert(c1.get_allocator() == A(2));
92 assert((c2 == std::deque<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
93 assert(c2.get_allocator() == A(1));
94 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
95 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
96 }
97#if TEST_STD_VER >= 11
98 {
99 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
100 const int N = sizeof(rng)/sizeof(rng[0]);
101 for (int i = 0; i < N; ++i)
102 for (int j = 0; j < N; ++j)
103 for (int k = 0; k < N; ++k)
104 testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j], rng[k]);
105 }
106 {
107 int a1[] = {1, 3, 7, 9, 10};
108 int a2[] = {0, 2, 4, 5, 6, 8, 11};
109 typedef min_allocator<int> A;
110 std::deque<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A());
111 std::deque<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A());
112 swap(c1, c2);
113 assert((c1 == std::deque<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
114 assert(c1.get_allocator() == A());
115 assert((c2 == std::deque<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
116 assert(c2.get_allocator() == A());
117 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
118 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
119 }
120#endif
121
122 return 0;
123}
124

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