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

source code of libcxx/test/std/containers/sequences/vector/vector.cons/copy_alloc.pass.cpp