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