| 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 | // UNSUPPORTED: c++03, c++11, c++14 |
| 11 | |
| 12 | // template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>> |
| 13 | // vector(InputIterator, InputIterator, Allocator = Allocator()) |
| 14 | // -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>; |
| 15 | // |
| 16 | // template<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>> |
| 17 | // vector(from_range_t, R&&, Allocator = Allocator()) |
| 18 | // -> vector<ranges::range_value_t<R>, Allocator>; // C++23 |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <array> |
| 22 | #include <cassert> |
| 23 | #include <climits> // INT_MAX |
| 24 | #include <cstddef> |
| 25 | #include <iterator> |
| 26 | #include <type_traits> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "deduction_guides_sfinae_checks.h" |
| 30 | #include "test_macros.h" |
| 31 | #include "test_iterators.h" |
| 32 | #include "test_allocator.h" |
| 33 | |
| 34 | struct A {}; |
| 35 | |
| 36 | TEST_CONSTEXPR_CXX20 bool tests() { |
| 37 | // Test the explicit deduction guides |
| 38 | { |
| 39 | const int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 40 | std::vector vec(std::begin(arr: arr), std::end(arr: arr)); |
| 41 | |
| 42 | static_assert(std::is_same_v<decltype(vec), std::vector<int>>, "" ); |
| 43 | assert(std::equal(vec.begin(), vec.end(), std::begin(arr), std::end(arr))); |
| 44 | } |
| 45 | |
| 46 | { |
| 47 | const long arr[] = {INT_MAX, 1L, 2L, 3L}; |
| 48 | std::vector vec(std::begin(arr: arr), std::end(arr: arr), std::allocator<long>()); |
| 49 | static_assert(std::is_same_v<decltype(vec)::value_type, long>, "" ); |
| 50 | assert(vec.size() == 4); |
| 51 | assert(vec[0] == INT_MAX); |
| 52 | assert(vec[1] == 1L); |
| 53 | assert(vec[2] == 2L); |
| 54 | } |
| 55 | |
| 56 | // Test the implicit deduction guides |
| 57 | |
| 58 | { |
| 59 | // We don't expect this one to work. |
| 60 | // std::vector vec(std::allocator<int>()); // vector (allocator &) |
| 61 | } |
| 62 | |
| 63 | { |
| 64 | std::vector vec(1, A{}); // vector (size_type, T) |
| 65 | static_assert(std::is_same_v<decltype(vec)::value_type, A>, "" ); |
| 66 | static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<A>>, "" ); |
| 67 | assert(vec.size() == 1); |
| 68 | } |
| 69 | |
| 70 | { |
| 71 | std::vector vec(1, A{}, test_allocator<A>()); // vector (size_type, T, allocator) |
| 72 | static_assert(std::is_same_v<decltype(vec)::value_type, A>, "" ); |
| 73 | static_assert(std::is_same_v<decltype(vec)::allocator_type, test_allocator<A>>, "" ); |
| 74 | assert(vec.size() == 1); |
| 75 | } |
| 76 | |
| 77 | { |
| 78 | std::vector vec{1U, 2U, 3U, 4U, 5U}; // vector(initializer-list) |
| 79 | static_assert(std::is_same_v<decltype(vec)::value_type, unsigned>, "" ); |
| 80 | assert(vec.size() == 5); |
| 81 | assert(vec[2] == 3U); |
| 82 | } |
| 83 | |
| 84 | { |
| 85 | std::vector vec({1.0, 2.0, 3.0, 4.0}, test_allocator<double>()); // vector(initializer-list, allocator) |
| 86 | static_assert(std::is_same_v<decltype(vec)::value_type, double>, "" ); |
| 87 | static_assert(std::is_same_v<decltype(vec)::allocator_type, test_allocator<double>>, "" ); |
| 88 | assert(vec.size() == 4); |
| 89 | assert(vec[3] == 4.0); |
| 90 | } |
| 91 | |
| 92 | { |
| 93 | std::vector<long double> source; |
| 94 | std::vector vec(source); // vector(vector &) |
| 95 | static_assert(std::is_same_v<decltype(vec)::value_type, long double>, "" ); |
| 96 | static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<long double>>, "" ); |
| 97 | assert(vec.size() == 0); |
| 98 | } |
| 99 | |
| 100 | #if TEST_STD_VER >= 23 |
| 101 | { |
| 102 | { |
| 103 | std::vector c(std::from_range, std::array<int, 0>()); |
| 104 | static_assert(std::is_same_v<decltype(c), std::vector<int>>); |
| 105 | } |
| 106 | |
| 107 | { |
| 108 | using Alloc = test_allocator<int>; |
| 109 | std::vector c(std::from_range, std::array<int, 0>(), Alloc()); |
| 110 | static_assert(std::is_same_v<decltype(c), std::vector<int, Alloc>>); |
| 111 | } |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | // A couple of vector<bool> tests, too! |
| 116 | { |
| 117 | std::vector vec(3, true); // vector(initializer-list) |
| 118 | static_assert(std::is_same_v<decltype(vec)::value_type, bool>, "" ); |
| 119 | static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<bool>>, "" ); |
| 120 | assert(vec.size() == 3); |
| 121 | assert(vec[0] && vec[1] && vec[2]); |
| 122 | } |
| 123 | |
| 124 | { |
| 125 | std::vector<bool> source; |
| 126 | std::vector vec(source); // vector(vector &) |
| 127 | static_assert(std::is_same_v<decltype(vec)::value_type, bool>, "" ); |
| 128 | static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<bool>>, "" ); |
| 129 | assert(vec.size() == 0); |
| 130 | } |
| 131 | |
| 132 | { |
| 133 | typedef test_allocator<short> Alloc; |
| 134 | typedef test_allocator<int> ConvertibleToAlloc; |
| 135 | |
| 136 | { |
| 137 | std::vector<short, Alloc> source; |
| 138 | std::vector vec(source, Alloc(2)); |
| 139 | static_assert(std::is_same_v<decltype(vec), decltype(source)>); |
| 140 | } |
| 141 | |
| 142 | { |
| 143 | std::vector<short, Alloc> source; |
| 144 | std::vector vec(source, ConvertibleToAlloc(2)); |
| 145 | static_assert(std::is_same_v<decltype(vec), decltype(source)>); |
| 146 | } |
| 147 | |
| 148 | { |
| 149 | std::vector<short, Alloc> source; |
| 150 | std::vector vec(std::move(source), Alloc(2)); |
| 151 | static_assert(std::is_same_v<decltype(vec), decltype(source)>); |
| 152 | } |
| 153 | |
| 154 | { |
| 155 | std::vector<short, Alloc> source; |
| 156 | std::vector vec(std::move(source), ConvertibleToAlloc(2)); |
| 157 | static_assert(std::is_same_v<decltype(vec), decltype(source)>); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | SequenceContainerDeductionGuidesSfinaeAway<std::vector, std::vector<int>>(); |
| 162 | |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | int main(int, char**) { |
| 167 | tests(); |
| 168 | #if TEST_STD_VER > 17 |
| 169 | static_assert(tests()); |
| 170 | #endif |
| 171 | return 0; |
| 172 | } |
| 173 | |