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 | // <set> |
10 | |
11 | // class set |
12 | |
13 | // void swap(set& m); |
14 | |
15 | #include <set> |
16 | #include <cassert> |
17 | #include "test_macros.h" |
18 | #include "test_allocator.h" |
19 | #include "../../../test_compare.h" |
20 | |
21 | int main(int, char**) |
22 | { |
23 | typedef int V; |
24 | { |
25 | typedef std::set<int> M; |
26 | { |
27 | M m1; |
28 | M m2; |
29 | M m1_save = m1; |
30 | M m2_save = m2; |
31 | swap(x&: m1, y&: m2); |
32 | assert(m1 == m2_save); |
33 | assert(m2 == m1_save); |
34 | } |
35 | { |
36 | V ar2[] = |
37 | { |
38 | 5, |
39 | 6, |
40 | 7, |
41 | 8, |
42 | 9, |
43 | 10, |
44 | 11, |
45 | 12 |
46 | }; |
47 | M m1; |
48 | M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0])); |
49 | M m1_save = m1; |
50 | M m2_save = m2; |
51 | swap(x&: m1, y&: m2); |
52 | assert(m1 == m2_save); |
53 | assert(m2 == m1_save); |
54 | } |
55 | { |
56 | V ar1[] = |
57 | { |
58 | 1, |
59 | 2, |
60 | 3, |
61 | 4 |
62 | }; |
63 | M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0])); |
64 | M m2; |
65 | M m1_save = m1; |
66 | M m2_save = m2; |
67 | swap(x&: m1, y&: m2); |
68 | assert(m1 == m2_save); |
69 | assert(m2 == m1_save); |
70 | } |
71 | { |
72 | V ar1[] = |
73 | { |
74 | 1, |
75 | 2, |
76 | 3, |
77 | 4 |
78 | }; |
79 | V ar2[] = |
80 | { |
81 | 5, |
82 | 6, |
83 | 7, |
84 | 8, |
85 | 9, |
86 | 10, |
87 | 11, |
88 | 12 |
89 | }; |
90 | M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0])); |
91 | M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0])); |
92 | M m1_save = m1; |
93 | M m2_save = m2; |
94 | swap(x&: m1, y&: m2); |
95 | assert(m1 == m2_save); |
96 | assert(m2 == m1_save); |
97 | } |
98 | } |
99 | { |
100 | typedef test_allocator<V> A; |
101 | typedef test_less<int> C; |
102 | typedef std::set<int, C, A> M; |
103 | V ar1[] = |
104 | { |
105 | 1, |
106 | 2, |
107 | 3, |
108 | 4 |
109 | }; |
110 | V ar2[] = |
111 | { |
112 | 5, |
113 | 6, |
114 | 7, |
115 | 8, |
116 | 9, |
117 | 10, |
118 | 11, |
119 | 12 |
120 | }; |
121 | M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]), C(1), A(1, 1)); |
122 | M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]), C(2), A(1, 2)); |
123 | M m1_save = m1; |
124 | M m2_save = m2; |
125 | swap(m1, m2); |
126 | assert(m1 == m2_save); |
127 | assert(m2 == m1_save); |
128 | assert(m1.key_comp() == C(2)); |
129 | assert(m1.get_allocator().get_id() == 1); |
130 | assert(m2.key_comp() == C(1)); |
131 | assert(m2.get_allocator().get_id() == 2); |
132 | } |
133 | { |
134 | typedef other_allocator<V> A; |
135 | typedef test_less<int> C; |
136 | typedef std::set<int, C, A> M; |
137 | V ar1[] = |
138 | { |
139 | 1, |
140 | 2, |
141 | 3, |
142 | 4 |
143 | }; |
144 | V ar2[] = |
145 | { |
146 | 5, |
147 | 6, |
148 | 7, |
149 | 8, |
150 | 9, |
151 | 10, |
152 | 11, |
153 | 12 |
154 | }; |
155 | M m1(ar1, ar1+sizeof(ar1)/sizeof(ar1[0]), C(1), A(1)); |
156 | M m2(ar2, ar2+sizeof(ar2)/sizeof(ar2[0]), C(2), A(2)); |
157 | M m1_save = m1; |
158 | M m2_save = m2; |
159 | swap(x&: m1, y&: m2); |
160 | assert(m1 == m2_save); |
161 | assert(m2 == m1_save); |
162 | assert(m1.key_comp() == C(2)); |
163 | assert(m1.get_allocator() == A(2)); |
164 | assert(m2.key_comp() == C(1)); |
165 | assert(m2.get_allocator() == A(1)); |
166 | } |
167 | |
168 | return 0; |
169 | } |
170 | |