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 
main(int,char **)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 //  This one is odd - you can pass an allocator in to use, but the allocator
108 //  has to match the type of the one used by the underlying container
109     typedef long double T;
110     typedef std::greater<T> Comp;
111     typedef test_allocator<T> Alloc;
112     typedef std::deque<T, Alloc> Cont;
113 
114     Cont c{2,3,0,1};
115     std::priority_queue<T, Cont, Comp> source(Comp(), c);
116     std::priority_queue pri(source, Alloc(2)); // queue(queue &, allocator)
117     static_assert(std::is_same_v<decltype(pri)::value_type, T>, "");
118     static_assert(std::is_same_v<decltype(pri)::container_type, Cont>, "");
119     assert(pri.size() == 4);
120     assert(pri.top() == 0);
121     }
122 
123   return 0;
124 }
125