xref: /llvm-project/libcxx/test/std/containers/sequences/vector/vector.capacity/swap.pass.cpp (revision e3dd9f7e66fec22986605da2dcd8120a7864455d)
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 // void swap(vector& x);
12 
13 #include <vector>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 #include "min_allocator.h"
18 #include "asan_testing.h"
19 
tests()20 TEST_CONSTEXPR_CXX20 bool tests() {
21     {
22         std::vector<int> v1(100);
23         std::vector<int> v2(200);
24         assert(is_contiguous_container_asan_correct(v1));
25         assert(is_contiguous_container_asan_correct(v2));
26         v1.swap(v2);
27         assert(v1.size() == 200);
28         assert(v1.capacity() == 200);
29         assert(is_contiguous_container_asan_correct(v1));
30         assert(v2.size() == 100);
31         assert(v2.capacity() == 100);
32         assert(is_contiguous_container_asan_correct(v2));
33     }
34 #if TEST_STD_VER >= 11
35     {
36         std::vector<int, min_allocator<int>> v1(100);
37         std::vector<int, min_allocator<int>> v2(200);
38         assert(is_contiguous_container_asan_correct(v1));
39         assert(is_contiguous_container_asan_correct(v2));
40         v1.swap(v2);
41         assert(v1.size() == 200);
42         assert(v1.capacity() == 200);
43         assert(is_contiguous_container_asan_correct(v1));
44         assert(v2.size() == 100);
45         assert(v2.capacity() == 100);
46         assert(is_contiguous_container_asan_correct(v2));
47     }
48     {
49       std::vector<int, safe_allocator<int>> v1(100);
50       std::vector<int, safe_allocator<int>> v2(200);
51       assert(is_contiguous_container_asan_correct(v1));
52       assert(is_contiguous_container_asan_correct(v2));
53       v1.swap(v2);
54       assert(v1.size() == 200);
55       assert(v1.capacity() == 200);
56       assert(is_contiguous_container_asan_correct(v1));
57       assert(v2.size() == 100);
58       assert(v2.capacity() == 100);
59       assert(is_contiguous_container_asan_correct(v2));
60     }
61 #endif
62 
63     return true;
64 }
65 
main(int,char **)66 int main(int, char**)
67 {
68     tests();
69 #if TEST_STD_VER > 17
70     static_assert(tests());
71 #endif
72     return 0;
73 }
74