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);
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
23test(const C& x)
24{
25 typename C::size_type s = x.size();
26 C c(x);
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
33TEST_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));
38 }
39 {
40 std::vector<int, test_allocator<int> > v(3, 2, test_allocator<int>(5));
41 std::vector<int, test_allocator<int> > v2 = v;
42 assert(is_contiguous_container_asan_correct(v));
43 assert(is_contiguous_container_asan_correct(v2));
44 assert(v2 == v);
45 assert(v2.get_allocator() == v.get_allocator());
46 assert(is_contiguous_container_asan_correct(v));
47 assert(is_contiguous_container_asan_correct(v2));
48 }
49 {
50 // Test copy ctor with empty source
51 std::vector<int, test_allocator<int> > v(test_allocator<int>(5));
52 std::vector<int, test_allocator<int> > v2 = v;
53 assert(is_contiguous_container_asan_correct(v));
54 assert(is_contiguous_container_asan_correct(v2));
55 assert(v2 == v);
56 assert(v2.get_allocator() == v.get_allocator());
57 assert(is_contiguous_container_asan_correct(v));
58 assert(is_contiguous_container_asan_correct(v2));
59 assert(v2.empty());
60 }
61#if TEST_STD_VER >= 11
62 {
63 std::vector<int, other_allocator<int> > v(3, 2, other_allocator<int>(5));
64 std::vector<int, other_allocator<int> > v2 = v;
65 assert(is_contiguous_container_asan_correct(v));
66 assert(is_contiguous_container_asan_correct(v2));
67 assert(v2 == v);
68 assert(v2.get_allocator() == other_allocator<int>(-2));
69 assert(is_contiguous_container_asan_correct(v));
70 assert(is_contiguous_container_asan_correct(v2));
71 }
72 {
73 int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0};
74 int* an = a + sizeof(a)/sizeof(a[0]);
75 test(std::vector<int, min_allocator<int>>(a, an));
76 test(std::vector<int, safe_allocator<int>>(a, an));
77 }
78 {
79 std::vector<int, min_allocator<int> > v(3, 2, min_allocator<int>());
80 std::vector<int, min_allocator<int> > v2 = v;
81 assert(is_contiguous_container_asan_correct(v));
82 assert(is_contiguous_container_asan_correct(v2));
83 assert(v2 == v);
84 assert(v2.get_allocator() == v.get_allocator());
85 assert(is_contiguous_container_asan_correct(v));
86 assert(is_contiguous_container_asan_correct(v2));
87 }
88 {
89 std::vector<int, safe_allocator<int> > v(3, 2, safe_allocator<int>());
90 std::vector<int, safe_allocator<int> > v2 = v;
91 assert(is_contiguous_container_asan_correct(v));
92 assert(is_contiguous_container_asan_correct(v2));
93 assert(v2 == v);
94 assert(v2.get_allocator() == v.get_allocator());
95 assert(is_contiguous_container_asan_correct(v));
96 assert(is_contiguous_container_asan_correct(v2));
97 }
98#endif
99
100 return true;
101}
102
103void test_copy_from_volatile_src() {
104 volatile int src[] = {1, 2, 3};
105 std::vector<int> v(src, src + 3);
106 assert(v[0] == 1);
107 assert(v[1] == 2);
108 assert(v[2] == 3);
109}
110
111int main(int, char**)
112{
113 tests();
114#if TEST_STD_VER > 17
115 static_assert(tests());
116#endif
117 test_copy_from_volatile_src();
118 return 0;
119}
120

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