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 // template <class InputIter> vector(InputIter first, InputIter last);
12 
13 #include <vector>
14 #include <cassert>
15 #include <cstddef>
16 
17 #include "test_macros.h"
18 #include "test_iterators.h"
19 #include "test_allocator.h"
20 #include "min_allocator.h"
21 #include "asan_testing.h"
22 #if TEST_STD_VER >= 11
23 #include "emplace_constructible.h"
24 #include "container_test_types.h"
25 #endif
26 
27 template <class C, class Iterator>
test(Iterator first,Iterator last)28 TEST_CONSTEXPR_CXX20 void test(Iterator first, Iterator last) {
29   {
30     C c(first, last);
31     LIBCPP_ASSERT(c.__invariants());
32     assert(c.size() == static_cast<std::size_t>(std::distance(first, last)));
33     LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
34     for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e;
35       ++i, ++first)
36     assert(*i == *first);
37   }
38   // Test with an empty range
39   {
40     C c(first, first);
41     LIBCPP_ASSERT(c.__invariants());
42     assert(c.empty());
43     LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
44   }
45 }
46 
basic_test_cases()47 TEST_CONSTEXPR_CXX20 void basic_test_cases() {
48   int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0};
49   int* an = a + sizeof(a) / sizeof(a[0]);
50   test<std::vector<int> >(cpp17_input_iterator<const int*>(a),
51                           cpp17_input_iterator<const int*>(an));
52   test<std::vector<int> >(forward_iterator<const int*>(a),
53                           forward_iterator<const int*>(an));
54   test<std::vector<int> >(bidirectional_iterator<const int*>(a),
55                           bidirectional_iterator<const int*>(an));
56   test<std::vector<int> >(random_access_iterator<const int*>(a),
57                           random_access_iterator<const int*>(an));
58   test<std::vector<int> >(a, an);
59 
60   test<std::vector<int, limited_allocator<int, 63> > >(
61       cpp17_input_iterator<const int*>(a), cpp17_input_iterator<const int*>(an));
62   // Add 1 for implementations that dynamically allocate a container proxy.
63   test<std::vector<int, limited_allocator<int, 18 + 1> > >(
64       forward_iterator<const int*>(a), forward_iterator<const int*>(an));
65   test<std::vector<int, limited_allocator<int, 18 + 1> > >(
66       bidirectional_iterator<const int*>(a),
67       bidirectional_iterator<const int*>(an));
68   test<std::vector<int, limited_allocator<int, 18 + 1> > >(
69       random_access_iterator<const int*>(a),
70       random_access_iterator<const int*>(an));
71   test<std::vector<int, limited_allocator<int, 18 + 1> > >(a, an);
72 #if TEST_STD_VER >= 11
73   test<std::vector<int, min_allocator<int> > >(cpp17_input_iterator<const int*>(a),
74                                                cpp17_input_iterator<const int*>(an));
75   test<std::vector<int, min_allocator<int> > >(
76       forward_iterator<const int*>(a), forward_iterator<const int*>(an));
77   test<std::vector<int, min_allocator<int> > >(
78       bidirectional_iterator<const int*>(a),
79       bidirectional_iterator<const int*>(an));
80   test<std::vector<int, min_allocator<int> > >(
81       random_access_iterator<const int*>(a),
82       random_access_iterator<const int*>(an));
83   test<std::vector<int> >(a, an);
84 #endif
85 }
86 
emplaceable_concept_tests()87 TEST_CONSTEXPR_CXX20 void emplaceable_concept_tests() {
88 #if TEST_STD_VER >= 11
89   int arr1[] = {42};
90   int arr2[] = {1, 101, 42};
91   {
92     using T = EmplaceConstructible<int>;
93     using It = forward_iterator<int*>;
94     {
95       std::vector<T> v(It(arr1), It(std::end(arr1)));
96       assert(v[0].value == 42);
97     }
98     {
99       std::vector<T> v(It(arr2), It(std::end(arr2)));
100       assert(v[0].value == 1);
101       assert(v[1].value == 101);
102       assert(v[2].value == 42);
103     }
104   }
105   {
106     using T = EmplaceConstructibleAndMoveInsertable<int>;
107     using It = cpp17_input_iterator<int*>;
108     {
109       std::vector<T> v(It(arr1), It(std::end(arr1)));
110       assert(v[0].copied == 0);
111       assert(v[0].value == 42);
112     }
113     {
114       std::vector<T> v(It(arr2), It(std::end(arr2)));
115       //assert(v[0].copied == 0);
116       assert(v[0].value == 1);
117       //assert(v[1].copied == 0);
118       assert(v[1].value == 101);
119       assert(v[2].copied == 0);
120       assert(v[2].value == 42);
121     }
122   }
123 #endif
124 }
125 
test_ctor_under_alloc()126 void test_ctor_under_alloc() {
127 #if TEST_STD_VER >= 11
128   int arr1[] = {42};
129   int arr2[] = {1, 101, 42};
130   {
131     using C = TCT::vector<>;
132     using It = forward_iterator<int*>;
133     {
134       ExpectConstructGuard<int&> G(1);
135       C v(It(arr1), It(std::end(arr1)));
136     }
137     {
138       ExpectConstructGuard<int&> G(3);
139       C v(It(arr2), It(std::end(arr2)));
140     }
141   }
142   {
143     using C = TCT::vector<>;
144     using It = cpp17_input_iterator<int*>;
145     {
146       ExpectConstructGuard<int&> G(1);
147       C v(It(arr1), It(std::end(arr1)));
148     }
149     {
150       //ExpectConstructGuard<int&> G(3);
151       //C v(It(arr2), It(std::end(arr2)), a);
152     }
153   }
154 #endif
155 }
156 
157 // In C++03, you can't instantiate a template with a local type.
158 struct B1 { int x; };
159 struct B2 { int y; };
160 struct Der : B1, B2 { int z; };
161 
162 // Initialize a vector with a different value type.
test_ctor_with_different_value_type()163 TEST_CONSTEXPR_CXX20 void test_ctor_with_different_value_type() {
164   {
165     // Make sure initialization is performed with each element value, not with
166     // a memory blob.
167     float array[3] = {0.0f, 1.0f, 2.0f};
168     TEST_DIAGNOSTIC_PUSH
169     TEST_MSVC_DIAGNOSTIC_IGNORED(4244) // conversion from 'float' to 'int', possible loss of data
170     std::vector<int> v(array, array + 3);
171     TEST_DIAGNOSTIC_POP
172     assert(v[0] == 0);
173     assert(v[1] == 1);
174     assert(v[2] == 2);
175   }
176   {
177     Der z;
178     Der *array[1] = { &z };
179     // Though the types Der* and B2* are very similar, initialization still cannot
180     // be done with `memcpy`.
181     std::vector<B2*> v(array, array + 1);
182     assert(v[0] == &z);
183   }
184   {
185     // Though the types are different, initialization can be done with `memcpy`.
186     int32_t array[1] = { -1 };
187     std::vector<uint32_t> v(array, array + 1);
188     assert(v[0] == 4294967295U);
189   }
190 }
191 
tests()192 TEST_CONSTEXPR_CXX20 bool tests() {
193   basic_test_cases();
194   emplaceable_concept_tests(); // See PR34898
195   test_ctor_with_different_value_type();
196 
197   return true;
198 }
199 
main(int,char **)200 int main(int, char**)
201 {
202     tests();
203     test_ctor_under_alloc();
204 #if TEST_STD_VER > 17
205     static_assert(tests());
206 #endif
207     return 0;
208 }
209