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