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