make_heap.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014-present
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. //
  12. // Copyright 2005 - 2007 Adobe Systems Incorporated
  13. // Distributed under the MIT License(see accompanying file LICENSE_1_0_0.txt
  14. // or a copy at http://stlab.adobe.com/licenses.html)
  15. //===----------------------------------------------------------------------===//
  16. //
  17. // The LLVM Compiler Infrastructure
  18. //
  19. // This file is dual licensed under the MIT and the University of Illinois Open
  20. // Source Licenses. See LICENSE.TXT for details.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #include <memory>
  24. #include <random>
  25. #include <algorithm>
  26. #include <functional>
  27. #include <range/v3/core.hpp>
  28. #include <range/v3/algorithm/heap_algorithm.hpp>
  29. #include "../array.hpp"
  30. #include "../simple_test.hpp"
  31. #include "../test_utils.hpp"
  32. #include "../test_iterators.hpp"
  33. RANGES_DIAGNOSTIC_IGNORE_GLOBAL_CONSTRUCTORS
  34. RANGES_DIAGNOSTIC_IGNORE_SIGN_CONVERSION
  35. namespace
  36. {
  37. std::mt19937 gen;
  38. void test_1(int N)
  39. {
  40. int* ia = new int [N];
  41. for (int i = 0; i < N; ++i)
  42. ia[i] = i;
  43. std::shuffle(ia, ia+N, gen);
  44. CHECK(ranges::make_heap(ia, ia+N) == ia+N);
  45. CHECK(std::is_heap(ia, ia+N));
  46. delete [] ia;
  47. }
  48. void test_2(int N)
  49. {
  50. int* ia = new int [N];
  51. for (int i = 0; i < N; ++i)
  52. ia[i] = i;
  53. std::shuffle(ia, ia+N, gen);
  54. CHECK(ranges::make_heap(ia, Sentinel<int*>(ia+N)) == ia+N);
  55. CHECK(std::is_heap(ia, ia+N));
  56. delete [] ia;
  57. }
  58. void test_3(int N)
  59. {
  60. int* ia = new int [N];
  61. for (int i = 0; i < N; ++i)
  62. ia[i] = i;
  63. std::shuffle(ia, ia+N, gen);
  64. CHECK(ranges::make_heap(ranges::make_subrange(ia, ia+N)) == ia+N);
  65. CHECK(std::is_heap(ia, ia+N));
  66. std::shuffle(ia, ia+N, gen);
  67. CHECK(::is_dangling(ranges::make_heap(::MakeTestRange(ia, ia+N))));
  68. CHECK(std::is_heap(ia, ia+N));
  69. delete [] ia;
  70. }
  71. void test_4(int N)
  72. {
  73. int* ia = new int [N];
  74. for (int i = 0; i < N; ++i)
  75. ia[i] = i;
  76. std::shuffle(ia, ia+N, gen);
  77. CHECK(ranges::make_heap(ranges::make_subrange(ia, Sentinel<int*>(ia+N))) == ia+N);
  78. CHECK(std::is_heap(ia, ia+N));
  79. std::shuffle(ia, ia+N, gen);
  80. CHECK(::is_dangling(ranges::make_heap(::MakeTestRange(ia, Sentinel<int*>(ia+N)))));
  81. CHECK(std::is_heap(ia, ia+N));
  82. delete [] ia;
  83. }
  84. void test_5(int N)
  85. {
  86. int* ia = new int [N];
  87. for (int i = 0; i < N; ++i)
  88. ia[i] = i;
  89. std::shuffle(ia, ia+N, gen);
  90. CHECK(ranges::make_heap(ia, ia+N, std::greater<int>()) == ia+N);
  91. CHECK(std::is_heap(ia, ia+N, std::greater<int>()));
  92. delete [] ia;
  93. }
  94. void test_6(int N)
  95. {
  96. int* ia = new int [N];
  97. for (int i = 0; i < N; ++i)
  98. ia[i] = i;
  99. std::shuffle(ia, ia+N, gen);
  100. CHECK(ranges::make_heap(ia, Sentinel<int*>(ia+N), std::greater<int>()) == ia+N);
  101. CHECK(std::is_heap(ia, ia+N, std::greater<int>()));
  102. delete [] ia;
  103. }
  104. void test_7(int N)
  105. {
  106. int* ia = new int [N];
  107. for (int i = 0; i < N; ++i)
  108. ia[i] = i;
  109. std::shuffle(ia, ia+N, gen);
  110. CHECK(ranges::make_heap(ranges::make_subrange(ia, ia+N), std::greater<int>()) == ia+N);
  111. CHECK(std::is_heap(ia, ia+N, std::greater<int>()));
  112. std::shuffle(ia, ia+N, gen);
  113. CHECK(::is_dangling(ranges::make_heap(::MakeTestRange(ia, ia+N), std::greater<int>())));
  114. CHECK(std::is_heap(ia, ia+N, std::greater<int>()));
  115. delete [] ia;
  116. }
  117. void test_8(int N)
  118. {
  119. int* ia = new int [N];
  120. for (int i = 0; i < N; ++i)
  121. ia[i] = i;
  122. std::shuffle(ia, ia+N, gen);
  123. CHECK(ranges::make_heap(ranges::make_subrange(ia, Sentinel<int*>(ia+N)), std::greater<int>()) == ia+N);
  124. CHECK(std::is_heap(ia, ia+N, std::greater<int>()));
  125. std::shuffle(ia, ia+N, gen);
  126. CHECK(::is_dangling(ranges::make_heap(::MakeTestRange(ia, Sentinel<int*>(ia+N)), std::greater<int>())));
  127. CHECK(std::is_heap(ia, ia+N, std::greater<int>()));
  128. delete [] ia;
  129. }
  130. struct indirect_less
  131. {
  132. template<class P>
  133. bool operator()(const P& x, const P& y)
  134. {return *x < *y;}
  135. };
  136. void test_9(int N)
  137. {
  138. std::unique_ptr<int>* ia = new std::unique_ptr<int> [N];
  139. for (int i = 0; i < N; ++i)
  140. ia[i].reset(new int(i));
  141. std::shuffle(ia, ia+N, gen);
  142. CHECK(ranges::make_heap(ia, ia+N, indirect_less()) == ia+N);
  143. CHECK(std::is_heap(ia, ia+N, indirect_less()));
  144. delete [] ia;
  145. }
  146. struct S
  147. {
  148. int i;
  149. };
  150. void test_10(int N)
  151. {
  152. int* ia = new int [N];
  153. S* ib = new S [N];
  154. for (int i = 0; i < N; ++i)
  155. ib[i].i = i;
  156. std::shuffle(ia, ia+N, gen);
  157. CHECK(ranges::make_heap(ib, ib+N, std::less<int>(), &S::i) == ib+N);
  158. std::transform(ib, ib+N, ia, std::mem_fn(&S::i));
  159. CHECK(std::is_heap(ia, ia+N));
  160. delete [] ia;
  161. delete [] ib;
  162. }
  163. void test_all(int N)
  164. {
  165. test_1(N);
  166. test_2(N);
  167. test_3(N);
  168. test_4(N);
  169. test_5(N);
  170. test_6(N);
  171. test_7(N);
  172. test_8(N);
  173. }
  174. constexpr bool test_constexpr()
  175. {
  176. using namespace ranges;
  177. constexpr int N = 100;
  178. test::array<int, N> ia{{0}};
  179. for(int i = 0; i < N; ++i)
  180. ia[i] = N - 1 - i;
  181. STATIC_CHECK_RETURN(make_heap(begin(ia), end(ia), std::less<int>{}) == end(ia));
  182. STATIC_CHECK_RETURN(is_heap(begin(ia), end(ia)));
  183. return true;
  184. }
  185. }
  186. int main()
  187. {
  188. test_all(0);
  189. test_all(1);
  190. test_all(2);
  191. test_all(3);
  192. test_all(10);
  193. test_all(1000);
  194. test_9(1000);
  195. test_10(1000);
  196. {
  197. STATIC_CHECK(test_constexpr());
  198. }
  199. return test_result();
  200. }