inplace_merge.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. //===----------------------------------------------------------------------===//
  13. //
  14. // The LLVM Compiler Infrastructure
  15. //
  16. // This file is dual licensed under the MIT and the University of Illinois Open
  17. // Source Licenses. See LICENSE.TXT for details.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include <algorithm>
  21. #include <random>
  22. #include <range/v3/core.hpp>
  23. #include <range/v3/algorithm/inplace_merge.hpp>
  24. #include "../simple_test.hpp"
  25. #include "../test_utils.hpp"
  26. #include "../test_iterators.hpp"
  27. RANGES_DIAGNOSTIC_IGNORE_GLOBAL_CONSTRUCTORS
  28. RANGES_DIAGNOSTIC_IGNORE_SIGN_CONVERSION
  29. namespace
  30. {
  31. std::mt19937 gen;
  32. template<class Iter, typename Sent = Iter>
  33. void
  34. test_one_iter(unsigned N, unsigned M)
  35. {
  36. RANGES_ENSURE(M <= N);
  37. int* ia = new int[N];
  38. for (unsigned i = 0; i < N; ++i)
  39. ia[i] = i;
  40. std::shuffle(ia, ia+N, gen);
  41. std::sort(ia, ia+M);
  42. std::sort(ia+M, ia+N);
  43. auto res = ranges::inplace_merge(Iter(ia), Iter(ia+M), Sent(ia+N));
  44. CHECK(res == Iter(ia+N));
  45. if(N > 0)
  46. {
  47. CHECK(ia[0] == 0);
  48. CHECK(ia[N-1] == (int)N-1);
  49. CHECK(std::is_sorted(ia, ia+N));
  50. }
  51. delete [] ia;
  52. }
  53. template<class Iter, typename Sent = Iter>
  54. void
  55. test_one_rng(unsigned N, unsigned M)
  56. {
  57. RANGES_ENSURE(M <= N);
  58. int* ia = new int[N];
  59. for (unsigned i = 0; i < N; ++i)
  60. ia[i] = i;
  61. std::shuffle(ia, ia+N, gen);
  62. std::sort(ia, ia+M);
  63. std::sort(ia+M, ia+N);
  64. auto res = ranges::inplace_merge(ranges::make_subrange(Iter(ia), Sent(ia+N)), Iter(ia+M));
  65. CHECK(res == Iter(ia+N));
  66. if(N > 0)
  67. {
  68. CHECK(ia[0] == 0);
  69. CHECK(ia[N-1] == (int)N-1);
  70. CHECK(std::is_sorted(ia, ia+N));
  71. }
  72. std::shuffle(ia, ia+N, gen);
  73. std::sort(ia, ia+M);
  74. std::sort(ia+M, ia+N);
  75. auto res2 = ranges::inplace_merge(::MakeTestRange(Iter(ia), Sent(ia+N)), Iter(ia+M));
  76. CHECK(::is_dangling(res2));
  77. if(N > 0)
  78. {
  79. CHECK(ia[0] == 0);
  80. CHECK(ia[N-1] == (int)N-1);
  81. CHECK(std::is_sorted(ia, ia+N));
  82. }
  83. delete [] ia;
  84. }
  85. template<class Iter>
  86. void
  87. test_one(unsigned N, unsigned M)
  88. {
  89. test_one_iter<Iter>(N, M);
  90. test_one_iter<Iter, typename sentinel_type<Iter>::type>(N, M);
  91. test_one_rng<Iter>(N, M);
  92. test_one_rng<Iter, typename sentinel_type<Iter>::type>(N, M);
  93. }
  94. template<class Iter>
  95. void
  96. test(unsigned N)
  97. {
  98. test_one<Iter>(N, 0);
  99. test_one<Iter>(N, N/4);
  100. test_one<Iter>(N, N/2);
  101. test_one<Iter>(N, 3*N/4);
  102. test_one<Iter>(N, N);
  103. }
  104. template<class Iter>
  105. void
  106. test()
  107. {
  108. test_one<Iter>(0, 0);
  109. test_one<Iter>(1, 0);
  110. test_one<Iter>(1, 1);
  111. test_one<Iter>(2, 0);
  112. test_one<Iter>(2, 1);
  113. test_one<Iter>(2, 2);
  114. test_one<Iter>(3, 0);
  115. test_one<Iter>(3, 1);
  116. test_one<Iter>(3, 2);
  117. test_one<Iter>(3, 3);
  118. test<Iter>(4);
  119. test<Iter>(100);
  120. test<Iter>(1000);
  121. }
  122. }
  123. int main()
  124. {
  125. // test<ForwardIterator<int*> >();
  126. test<BidirectionalIterator<int*> >();
  127. test<RandomAccessIterator<int*> >();
  128. test<int*>();
  129. return ::test_result();
  130. }