push_heap.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // <algorithm>
  24. // template<random_access_iterator Iter>
  25. // requires ShuffleIterator<Iter>
  26. // && LessThanComparable<Iter::value_type>
  27. // void
  28. // push_heap(Iter first, Iter last);
  29. #include <memory>
  30. #include <random>
  31. #include <algorithm>
  32. #include <functional>
  33. #include <range/v3/core.hpp>
  34. #include <range/v3/algorithm/heap_algorithm.hpp>
  35. #include "../array.hpp"
  36. #include "../simple_test.hpp"
  37. #include "../test_utils.hpp"
  38. #include "../test_iterators.hpp"
  39. RANGES_DIAGNOSTIC_IGNORE_GLOBAL_CONSTRUCTORS
  40. RANGES_DIAGNOSTIC_IGNORE_SIGN_CONVERSION
  41. namespace
  42. {
  43. std::mt19937 gen;
  44. void test_basic(int N)
  45. {
  46. auto push_heap = make_testable_1(ranges::push_heap);
  47. int* ia = new int[N];
  48. for (int i = 0; i < N; ++i)
  49. ia[i] = i;
  50. std::shuffle(ia, ia+N, gen);
  51. for (int i = 0; i <= N; ++i)
  52. {
  53. push_heap(ia, ia+i).check([&](int *r){CHECK(r == ia + i);});
  54. CHECK(std::is_heap(ia, ia+i));
  55. }
  56. delete[] ia;
  57. }
  58. void test_comp(int N)
  59. {
  60. auto push_heap = make_testable_1(ranges::push_heap);
  61. int* ia = new int[N];
  62. for (int i = 0; i < N; ++i)
  63. ia[i] = i;
  64. std::shuffle(ia, ia+N, gen);
  65. for (int i = 0; i <= N; ++i)
  66. {
  67. push_heap(ia, ia+i, std::greater<int>()).check([&](int *r){CHECK(r == ia+i);});
  68. CHECK(std::is_heap(ia, ia+i, std::greater<int>()));
  69. }
  70. delete[] ia;
  71. }
  72. struct S
  73. {
  74. int i;
  75. };
  76. void test_proj(int N)
  77. {
  78. auto push_heap = make_testable_1(ranges::push_heap);
  79. S* ia = new S[N];
  80. int* ib = new int[N];
  81. for (int i = 0; i < N; ++i)
  82. ia[i].i = i;
  83. std::shuffle(ia, ia+N, gen);
  84. for (int i = 0; i <= N; ++i)
  85. {
  86. push_heap(ia, ia+i, std::greater<int>(), &S::i).check([&](S *r){CHECK(r == ia+i);});
  87. std::transform(ia, ia+i, ib, std::mem_fn(&S::i));
  88. CHECK(std::is_heap(ib, ib+i, std::greater<int>()));
  89. }
  90. delete[] ia;
  91. delete[] ib;
  92. }
  93. struct indirect_less
  94. {
  95. template<class P>
  96. bool operator()(const P& x, const P& y)
  97. {return *x < *y;}
  98. };
  99. void test_move_only(int N)
  100. {
  101. auto const push_heap = make_testable_1(ranges::push_heap);
  102. std::unique_ptr<int>* ia = new std::unique_ptr<int>[N];
  103. for (int i = 0; i < N; ++i)
  104. ia[i].reset(new int(i));
  105. std::shuffle(ia, ia+N, gen);
  106. for (int i = 0; i <= N; ++i)
  107. {
  108. push_heap(ia, ia+i, indirect_less()).check([&](std::unique_ptr<int> *r){CHECK(r == ia+i);});
  109. CHECK(std::is_heap(ia, ia+i, indirect_less()));
  110. }
  111. delete[] ia;
  112. }
  113. }
  114. constexpr bool test_constexpr()
  115. {
  116. using namespace ranges;
  117. constexpr int N = 100;
  118. test::array<int, N> ia{{0}};
  119. for(int i = 0; i < N; ++i)
  120. ia[i] = i;
  121. for(int i = 0; i <= N; ++i)
  122. {
  123. STATIC_CHECK_RETURN(push_heap(make_subrange(begin(ia), begin(ia) + i),
  124. std::greater<int>()) == begin(ia) + i);
  125. STATIC_CHECK_RETURN(is_heap(begin(ia), begin(ia) + i, std::greater<int>()));
  126. }
  127. return true;
  128. }
  129. int main()
  130. {
  131. test_basic(1000);
  132. test_comp(1000);
  133. test_proj(1000);
  134. test_move_only(1000);
  135. {
  136. int const N = 1000;
  137. S* ia = new S[N];
  138. int* ib = new int[N];
  139. for (int i = 0; i < N; ++i)
  140. ia[i].i = i;
  141. std::shuffle(ia, ia+N, gen);
  142. for (int i = 0; i <= N; ++i)
  143. {
  144. CHECK(ranges::push_heap(ranges::make_subrange(ia, ia+i), std::greater<int>(), &S::i) == ia+i);
  145. std::transform(ia, ia+i, ib, std::mem_fn(&S::i));
  146. CHECK(std::is_heap(ib, ib+i, std::greater<int>()));
  147. }
  148. delete[] ia;
  149. delete[] ib;
  150. }
  151. {
  152. STATIC_CHECK(test_constexpr());
  153. }
  154. return test_result();
  155. }