xref: /llvm-project/libcxx/test/std/containers/sequences/vector/vector.cons/copy_alloc.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 // vector(const vector& v, const allocator_type& a);
12 
13 #include <vector>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 #include "test_allocator.h"
18 #include "min_allocator.h"
19 #include "asan_testing.h"
20 
21 template <class C>
22 TEST_CONSTEXPR_CXX20 void
test(const C & x,const typename C::allocator_type & a)23 test(const C& x, const typename C::allocator_type& a)
24 {
25     typename C::size_type s = x.size();
26     C c(x, a);
27     LIBCPP_ASSERT(c.__invariants());
28     assert(c.size() == s);
29     assert(c == x);
30     LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
31 }
32 
tests()33 TEST_CONSTEXPR_CXX20 bool tests() {
34     {
35         int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0};
36         int* an = a + sizeof(a)/sizeof(a[0]);
37         test(std::vector<int>(a, an), std::allocator<int>());
38     }
39     {
40         std::vector<int, test_allocator<int> > l(3, 2, test_allocator<int>(5));
41         std::vector<int, test_allocator<int> > l2(l, test_allocator<int>(3));
42         assert(l2 == l);
43         assert(l2.get_allocator() == test_allocator<int>(3));
44     }
45     {
46         std::vector<int, other_allocator<int> > l(3, 2, other_allocator<int>(5));
47         std::vector<int, other_allocator<int> > l2(l, other_allocator<int>(3));
48         assert(l2 == l);
49         assert(l2.get_allocator() == other_allocator<int>(3));
50     }
51     {
52         // Test copy ctor with allocator and empty source
53         std::vector<int, other_allocator<int> > l(other_allocator<int>(5));
54         std::vector<int, other_allocator<int> > l2(l, other_allocator<int>(3));
55         assert(l2 == l);
56         assert(l2.get_allocator() == other_allocator<int>(3));
57         assert(l2.empty());
58     }
59 #if TEST_STD_VER >= 11
60     {
61         int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0};
62         int* an = a + sizeof(a)/sizeof(a[0]);
63         test(std::vector<int, min_allocator<int>>(a, an), min_allocator<int>());
64         test(std::vector<int, safe_allocator<int>>(a, an), safe_allocator<int>());
65     }
66     {
67         std::vector<int, min_allocator<int> > l(3, 2, min_allocator<int>());
68         std::vector<int, min_allocator<int> > l2(l, min_allocator<int>());
69         assert(l2 == l);
70         assert(l2.get_allocator() == min_allocator<int>());
71     }
72     {
73       std::vector<int, safe_allocator<int> > l(3, 2, safe_allocator<int>());
74       std::vector<int, safe_allocator<int> > l2(l, safe_allocator<int>());
75       assert(l2 == l);
76       assert(l2.get_allocator() == safe_allocator<int>());
77     }
78 #endif
79 
80     return true;
81 }
82 
main(int,char **)83 int main(int, char**)
84 {
85     tests();
86 #if TEST_STD_VER > 17
87     static_assert(tests());
88 #endif
89     return 0;
90 }
91