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 // <queue>
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: libcpp-no-deduction-guides
12 
13 // template<class Compare, class Container>
14 // priority_queue(Compare, Container)
15 //     -> priority_queue<typename Container::value_type, Container, Compare>;
16 //
17 // template<class InputIterator,
18 //          class Compare = less<typename iterator_traits<InputIterator>::value_type>,
19 //          class Container = vector<typename iterator_traits<InputIterator>::value_type>>
20 // priority_queue(InputIterator, InputIterator, Compare = Compare(), Container = Container())
21 //     -> priority_queue<typename iterator_traits<InputIterator>::value_type, Container, Compare>;
22 //
23 // template<class Compare, class Container, class Allocator>
24 // priority_queue(Compare, Container, Allocator)
25 //     -> priority_queue<typename Container::value_type, Container, Compare>;
26 
27 
28 #include <queue>
29 #include <vector>
30 #include <iterator>
31 #include <cassert>
32 #include <cstddef>
33 #include <climits> // INT_MAX
34 
35 #include "test_macros.h"
36 #include "test_iterators.h"
37 #include "test_allocator.h"
38 
39 struct A {};
40 
41 int main(int, char**)
42 {
43 
44 //  Test the explicit deduction guides
45     {
46     std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
47     std::priority_queue pri(std::greater<int>(), v); // priority_queue(Compare, Container)
48 
49     static_assert(std::is_same_v<decltype(pri), std::priority_queue<int, std::vector<int>, std::greater<int>>>, "");
50     assert(pri.size() == v.size());
51     assert(pri.top() == 0);
52     }
53 
54     {
55     std::vector<long, test_allocator<long>> v{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
56     std::priority_queue pri(std::greater<long>(), v, test_allocator<long>(2)); // priority_queue(Compare, Container, Allocator)
57 
58     static_assert(std::is_same_v<decltype(pri),
59                                  std::priority_queue<long, std::vector<long, test_allocator<long>>, std::greater<long>>>, "");
60     assert(pri.size() == v.size());
61     assert(pri.top() == 10);
62     }
63 
64     {
65     std::vector<short> v{10, 11, 12, 13, 14, 15, 28, 17, 18, 19 };
66     std::priority_queue pri(v.begin(), v.end()); // priority_queue(Iter, Iter)
67 
68     static_assert(std::is_same_v<decltype(pri), std::priority_queue<short>>, "");
69     assert(pri.size() == v.size());
70     assert(pri.top() == 28);
71     }
72 
73     {
74     std::vector<double> v{10, 11, 12, 13, 6, 15, 28, 17, 18, 19 };
75     std::priority_queue pri(v.begin(), v.end(), std::greater<double>()); // priority_queue(Iter, Iter, Comp)
76 
77     static_assert(std::is_same_v<decltype(pri), std::priority_queue<double, std::vector<double>, std::greater<double>>>, "");
78     assert(pri.size() == v.size());
79     assert(pri.top() == 6);
80     }
81 
82     {
83     std::vector<double> v{10, 6, 15, 28, 4, 18, 19 };
84     std::deque<double> deq;
85     std::priority_queue pri(v.begin(), v.end(), std::greater<double>(), deq); // priority_queue(Iter, Iter, Comp, Container)
86 
87     static_assert(std::is_same_v<decltype(pri), std::priority_queue<double, std::deque<double>, std::greater<double>>>, "");
88     assert(pri.size() == v.size());
89     assert(pri.top() == 4);
90     }
91 
92 //  Test the implicit deduction guides
93     {
94 //  We don't expect this one to work - no way to implicitly get value_type
95 //  std::priority_queue pri(std::allocator<int>()); // queue (allocator &)
96     }
97 
98     {
99     std::priority_queue<float> source;
100     std::priority_queue pri(source); // priority_queue(priority_queue &)
101     static_assert(std::is_same_v<decltype(pri)::value_type, float>, "");
102     static_assert(std::is_same_v<decltype(pri)::container_type, std::vector<float>>, "");
103     assert(pri.size() == 0);
104     }
105 
106     {
107         typedef short T;
108         typedef std::greater<T> Comp;
109         typedef test_allocator<T> Alloc;
110         typedef std::deque<T, Alloc> Cont;
111         typedef test_allocator<int> ConvertibleToAlloc;
112         static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
113                       !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
114 
115         {
116         Comp comp;
117         Cont cont;
118         std::priority_queue pri(comp, cont, Alloc(2));
119         static_assert(std::is_same_v<decltype(pri), std::priority_queue<T, Cont, Comp>>);
120         }
121 
122         {
123         Comp comp;
124         Cont cont;
125         std::priority_queue pri(comp, cont, ConvertibleToAlloc(2));
126         static_assert(std::is_same_v<decltype(pri), std::priority_queue<T, Cont, Comp>>);
127         }
128 
129         {
130         Comp comp;
131         Cont cont;
132         std::priority_queue pri(comp, std::move(cont), Alloc(2));
133         static_assert(std::is_same_v<decltype(pri), std::priority_queue<T, Cont, Comp>>);
134         }
135 
136         {
137         Comp comp;
138         Cont cont;
139         std::priority_queue pri(comp, std::move(cont), ConvertibleToAlloc(2));
140         static_assert(std::is_same_v<decltype(pri), std::priority_queue<T, Cont, Comp>>);
141         }
142     }
143 
144     {
145         typedef short T;
146         typedef std::greater<T> Comp;
147         typedef test_allocator<T> Alloc;
148         typedef std::deque<T, Alloc> Cont;
149         typedef test_allocator<int> ConvertibleToAlloc;
150         static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
151                       !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
152 
153         {
154         std::priority_queue<T, Cont, Comp> source;
155         std::priority_queue pri(source, Alloc(2));
156         static_assert(std::is_same_v<decltype(pri), std::priority_queue<T, Cont, Comp>>);
157         }
158 
159         {
160         std::priority_queue<T, Cont, Comp> source;
161         std::priority_queue pri(source, ConvertibleToAlloc(2));
162         static_assert(std::is_same_v<decltype(pri), std::priority_queue<T, Cont, Comp>>);
163         }
164 
165         {
166         std::priority_queue<T, Cont, Comp> source;
167         std::priority_queue pri(std::move(source), Alloc(2));
168         static_assert(std::is_same_v<decltype(pri), std::priority_queue<T, Cont, Comp>>);
169         }
170 
171         {
172         std::priority_queue<T, Cont, Comp> source;
173         std::priority_queue pri(std::move(source), ConvertibleToAlloc(2));
174         static_assert(std::is_same_v<decltype(pri), std::priority_queue<T, Cont, Comp>>);
175         }
176     }
177 
178     return 0;
179 }
180