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     }
65     {
66         std::vector<int, min_allocator<int> > l(3, 2, min_allocator<int>());
67         std::vector<int, min_allocator<int> > l2(l, min_allocator<int>());
68         assert(l2 == l);
69         assert(l2.get_allocator() == min_allocator<int>());
70     }
71 #endif
72 
73     return true;
74 }
75 
main(int,char **)76 int main(int, char**)
77 {
78     tests();
79 #if TEST_STD_VER > 17
80     static_assert(tests());
81 #endif
82     return 0;
83 }
84