1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <queue>
11 // UNSUPPORTED: c++98, c++03, c++11, c++14
12 // UNSUPPORTED: libcpp-no-deduction-guides
13 
14 // template<class Compare, class Container>
15 // priority_queue(Compare, Container)
16 //     -> priority_queue<typename Container::value_type, Container, Compare>;
17 //
18 // template<class InputIterator,
19 //          class Compare = less<typename iterator_traits<InputIterator>::value_type>,
20 //          class Container = vector<typename iterator_traits<InputIterator>::value_type>>
21 // priority_queue(InputIterator, InputIterator, Compare = Compare(), Container = Container())
22 //     -> priority_queue<typename iterator_traits<InputIterator>::value_type, Container, Compare>;
23 //
24 // template<class Compare, class Container, class Allocator>
25 // priority_queue(Compare, Container, Allocator)
26 //     -> priority_queue<typename Container::value_type, Container, Compare>;
27 
28 
29 #include <queue>
30 #include <vector>
31 #include <iterator>
32 #include <cassert>
33 #include <cstddef>
34 #include <climits> // INT_MAX
35 
36 #include "test_macros.h"
37 #include "test_iterators.h"
38 #include "test_allocator.h"
39 
40 struct A {};
41 
main()42 int main()
43 {
44 
45 //  Test the explicit deduction guides
46     {
47     std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
48     std::priority_queue pri(std::greater<int>(), v); // priority_queue(Compare, Container)
49 
50     static_assert(std::is_same_v<decltype(pri), std::priority_queue<int, std::vector<int>, std::greater<int>>>, "");
51     assert(pri.size() == v.size());
52     assert(pri.top() == 0);
53     }
54 
55     {
56     std::vector<long, test_allocator<long>> v{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
57     std::priority_queue pri(std::greater<long>(), v, test_allocator<long>(2)); // priority_queue(Compare, Container, Allocator)
58 
59     static_assert(std::is_same_v<decltype(pri),
60                                  std::priority_queue<long, std::vector<long, test_allocator<long>>, std::greater<long>>>, "");
61     assert(pri.size() == v.size());
62     assert(pri.top() == 10);
63     }
64 
65     {
66     std::vector<short> v{10, 11, 12, 13, 14, 15, 28, 17, 18, 19 };
67     std::priority_queue pri(v.begin(), v.end()); // priority_queue(Iter, Iter)
68 
69     static_assert(std::is_same_v<decltype(pri), std::priority_queue<short>>, "");
70     assert(pri.size() == v.size());
71     assert(pri.top() == 28);
72     }
73 
74     {
75     std::vector<double> v{10, 11, 12, 13, 6, 15, 28, 17, 18, 19 };
76     std::priority_queue pri(v.begin(), v.end(), std::greater<double>()); // priority_queue(Iter, Iter, Comp)
77 
78     static_assert(std::is_same_v<decltype(pri), std::priority_queue<double, std::vector<double>, std::greater<double>>>, "");
79     assert(pri.size() == v.size());
80     assert(pri.top() == 6);
81     }
82 
83     {
84     std::vector<double> v{10, 6, 15, 28, 4, 18, 19 };
85     std::deque<double> deq;
86     std::priority_queue pri(v.begin(), v.end(), std::greater<double>(), deq); // priority_queue(Iter, Iter, Comp, Container)
87 
88     static_assert(std::is_same_v<decltype(pri), std::priority_queue<double, std::deque<double>, std::greater<double>>>, "");
89     assert(pri.size() == v.size());
90     assert(pri.top() == 4);
91     }
92 
93 //  Test the implicit deduction guides
94     {
95 //  We don't expect this one to work - no way to implicitly get value_type
96 //  std::priority_queue pri(std::allocator<int>()); // queue (allocator &)
97     }
98 
99     {
100     std::priority_queue<float> source;
101     std::priority_queue pri(source); // priority_queue(priority_queue &)
102     static_assert(std::is_same_v<decltype(pri)::value_type, float>, "");
103     static_assert(std::is_same_v<decltype(pri)::container_type, std::vector<float>>, "");
104     assert(pri.size() == 0);
105     }
106 
107     {
108 //  This one is odd - you can pass an allocator in to use, but the allocator
109 //  has to match the type of the one used by the underlying container
110     typedef long double T;
111     typedef std::greater<T> Comp;
112     typedef test_allocator<T> A;
113     typedef std::deque<T, A> Cont;
114 
115     Cont c{2,3,0,1};
116     std::priority_queue<T, Cont, Comp> source(Comp(), c);
117     std::priority_queue pri(source, A(2)); // queue(queue &, allocator)
118     static_assert(std::is_same_v<decltype(pri)::value_type, T>, "");
119     static_assert(std::is_same_v<decltype(pri)::container_type, Cont>, "");
120     assert(pri.size() == 4);
121     assert(pri.top() == 0);
122     }
123 }
124