deduct.pass.cpp 4.5 KB

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