xref: /llvm-project/libcxx/test/std/containers/sequences/vector.bool/swap.pass.cpp (revision 98d3d5b5da66e3cf7807c23a0294280bb796466b)
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 // vector<bool>
11 
12 // void swap(vector& x);
13 
14 #include <vector>
15 #include <cassert>
16 #include "test_macros.h"
17 #include "test_allocator.h"
18 #include "min_allocator.h"
19 
tests()20 TEST_CONSTEXPR_CXX20 bool tests()
21 {
22     {
23         std::vector<bool> v1(100);
24         std::vector<bool> v2(200);
25         v1.swap(v2);
26         assert(v1.size() == 200);
27         assert(v1.capacity() >= 200);
28         assert(v2.size() == 100);
29         assert(v2.capacity() >= 100);
30     }
31     {
32         typedef test_allocator<bool> A;
33         std::vector<bool, A> v1(100, true, A(1, 1));
34         std::vector<bool, A> v2(200, false, A(1, 2));
35         swap(v1, v2);
36         assert(v1.size() == 200);
37         assert(v1.capacity() >= 200);
38         assert(v2.size() == 100);
39         assert(v2.capacity() >= 100);
40         assert(v1.get_allocator().get_id() == 1);
41         assert(v2.get_allocator().get_id() == 2);
42     }
43     {
44         typedef other_allocator<bool> A;
45         std::vector<bool, A> v1(100, true, A(1));
46         std::vector<bool, A> v2(200, false, A(2));
47         swap(v1, v2);
48         assert(v1.size() == 200);
49         assert(v1.capacity() >= 200);
50         assert(v2.size() == 100);
51         assert(v2.capacity() >= 100);
52         assert(v1.get_allocator() == A(2));
53         assert(v2.get_allocator() == A(1));
54     }
55     {
56         std::vector<bool> v(2);
57         std::vector<bool>::reference r1 = v[0];
58         std::vector<bool>::reference r2 = v[1];
59         r1 = true;
60         using std::swap;
61         swap(r1, r2);
62         assert(v[0] == false);
63         assert(v[1] == true);
64     }
65 #if TEST_STD_VER >= 11
66     {
67         std::vector<bool, min_allocator<bool>> v1(100);
68         std::vector<bool, min_allocator<bool>> v2(200);
69         v1.swap(v2);
70         assert(v1.size() == 200);
71         assert(v1.capacity() >= 200);
72         assert(v2.size() == 100);
73         assert(v2.capacity() >= 100);
74     }
75     {
76         typedef min_allocator<bool> A;
77         std::vector<bool, A> v1(100, true, A());
78         std::vector<bool, A> v2(200, false, A());
79         swap(v1, v2);
80         assert(v1.size() == 200);
81         assert(v1.capacity() >= 200);
82         assert(v2.size() == 100);
83         assert(v2.capacity() >= 100);
84         assert(v1.get_allocator() == A());
85         assert(v2.get_allocator() == A());
86     }
87     {
88         std::vector<bool, min_allocator<bool>> v(2);
89         std::vector<bool, min_allocator<bool>>::reference r1 = v[0];
90         std::vector<bool, min_allocator<bool>>::reference r2 = v[1];
91         r1 = true;
92         using std::swap;
93         swap(r1, r2);
94         assert(v[0] == false);
95         assert(v[1] == true);
96     }
97 #endif
98 
99     return true;
100 }
101 
main(int,char **)102 int main(int, char**)
103 {
104     tests();
105 #if TEST_STD_VER > 17
106     static_assert(tests());
107 #endif
108     return 0;
109 }
110