[mlir][Vector] Fix `vector.shuffle` folder for poison indices (#124863)
[llvm-project.git] / libcxx / test / std / containers / sequences / vector / vector.cons / deduct.pass.cpp
blob322161d51750268231358671e785720931808c6b
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 //===----------------------------------------------------------------------===//
9 // <vector>
10 // UNSUPPORTED: c++03, c++11, c++14
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>;
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
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>
29 #include "deduction_guides_sfinae_checks.h"
30 #include "test_macros.h"
31 #include "test_iterators.h"
32 #include "test_allocator.h"
34 struct A {};
36 TEST_CONSTEXPR_CXX20 bool tests() {
38 // Test the explicit deduction guides
40 const int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
41 std::vector vec(std::begin(arr), std::end(arr));
43 static_assert(std::is_same_v<decltype(vec), std::vector<int>>, "");
44 assert(std::equal(vec.begin(), vec.end(), std::begin(arr), std::end(arr)));
48 const long arr[] = {INT_MAX, 1L, 2L, 3L };
49 std::vector vec(std::begin(arr), std::end(arr), std::allocator<long>());
50 static_assert(std::is_same_v<decltype(vec)::value_type, long>, "");
51 assert(vec.size() == 4);
52 assert(vec[0] == INT_MAX);
53 assert(vec[1] == 1L);
54 assert(vec[2] == 2L);
57 // Test the implicit deduction guides
60 // We don't expect this one to work.
61 // std::vector vec(std::allocator<int>()); // vector (allocator &)
65 std::vector vec(1, A{}); // vector (size_type, T)
66 static_assert(std::is_same_v<decltype(vec)::value_type, A>, "");
67 static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<A>>, "");
68 assert(vec.size() == 1);
72 std::vector vec(1, A{}, test_allocator<A>()); // vector (size_type, T, allocator)
73 static_assert(std::is_same_v<decltype(vec)::value_type, A>, "");
74 static_assert(std::is_same_v<decltype(vec)::allocator_type, test_allocator<A>>, "");
75 assert(vec.size() == 1);
79 std::vector vec{1U, 2U, 3U, 4U, 5U}; // vector(initializer-list)
80 static_assert(std::is_same_v<decltype(vec)::value_type, unsigned>, "");
81 assert(vec.size() == 5);
82 assert(vec[2] == 3U);
86 std::vector vec({1.0, 2.0, 3.0, 4.0}, test_allocator<double>()); // vector(initializer-list, allocator)
87 static_assert(std::is_same_v<decltype(vec)::value_type, double>, "");
88 static_assert(std::is_same_v<decltype(vec)::allocator_type, test_allocator<double>>, "");
89 assert(vec.size() == 4);
90 assert(vec[3] == 4.0);
94 std::vector<long double> source;
95 std::vector vec(source); // vector(vector &)
96 static_assert(std::is_same_v<decltype(vec)::value_type, long double>, "");
97 static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<long double>>, "");
98 assert(vec.size() == 0);
101 #if TEST_STD_VER >= 23
104 std::vector c(std::from_range, std::array<int, 0>());
105 static_assert(std::is_same_v<decltype(c), std::vector<int>>);
109 using Alloc = test_allocator<int>;
110 std::vector c(std::from_range, std::array<int, 0>(), Alloc());
111 static_assert(std::is_same_v<decltype(c), std::vector<int, Alloc>>);
114 #endif
116 // A couple of vector<bool> tests, too!
118 std::vector vec(3, true); // vector(initializer-list)
119 static_assert(std::is_same_v<decltype(vec)::value_type, bool>, "");
120 static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<bool>>, "");
121 assert(vec.size() == 3);
122 assert(vec[0] && vec[1] && vec[2]);
126 std::vector<bool> source;
127 std::vector vec(source); // vector(vector &)
128 static_assert(std::is_same_v<decltype(vec)::value_type, bool>, "");
129 static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<bool>>, "");
130 assert(vec.size() == 0);
134 typedef test_allocator<short> Alloc;
135 typedef test_allocator<int> ConvertibleToAlloc;
138 std::vector<short, Alloc> source;
139 std::vector vec(source, Alloc(2));
140 static_assert(std::is_same_v<decltype(vec), decltype(source)>);
144 std::vector<short, Alloc> source;
145 std::vector vec(source, ConvertibleToAlloc(2));
146 static_assert(std::is_same_v<decltype(vec), decltype(source)>);
150 std::vector<short, Alloc> source;
151 std::vector vec(std::move(source), Alloc(2));
152 static_assert(std::is_same_v<decltype(vec), decltype(source)>);
156 std::vector<short, Alloc> source;
157 std::vector vec(std::move(source), ConvertibleToAlloc(2));
158 static_assert(std::is_same_v<decltype(vec), decltype(source)>);
162 SequenceContainerDeductionGuidesSfinaeAway<std::vector, std::vector<int>>();
164 return true;
167 int main(int, char**) {
168 tests();
169 #if TEST_STD_VER > 17
170 static_assert(tests());
171 #endif
172 return 0;