is_partitioned.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <utility>
  25. #include <range/v3/core.hpp>
  26. #include <range/v3/algorithm/is_partitioned.hpp>
  27. #include "../simple_test.hpp"
  28. #include "../test_utils.hpp"
  29. #include "../test_iterators.hpp"
  30. struct is_odd
  31. {
  32. constexpr bool operator()(const int & i) const
  33. {
  34. return i & 1;
  35. }
  36. };
  37. template<class Iter, class Sent = Iter>
  38. void
  39. test_iter()
  40. {
  41. {
  42. const int ia[] = {1, 2, 3, 4, 5, 6};
  43. CHECK(!ranges::is_partitioned(Iter(ranges::begin(ia)),
  44. Sent(ranges::end(ia)),
  45. is_odd()));
  46. }
  47. {
  48. const int ia[] = {1, 3, 5, 2, 4, 6};
  49. CHECK( ranges::is_partitioned(Iter(ranges::begin(ia)),
  50. Sent(ranges::end(ia)),
  51. is_odd()));
  52. }
  53. {
  54. const int ia[] = {2, 4, 6, 1, 3, 5};
  55. CHECK(!ranges::is_partitioned(Iter(ranges::begin(ia)),
  56. Sent(ranges::end(ia)),
  57. is_odd()));
  58. }
  59. {
  60. const int ia[] = {1, 3, 5, 2, 4, 6, 7};
  61. CHECK(!ranges::is_partitioned(Iter(ranges::begin(ia)),
  62. Sent(ranges::end(ia)),
  63. is_odd()));
  64. }
  65. {
  66. const int ia[] = {1, 3, 5, 2, 4, 6, 7};
  67. CHECK( ranges::is_partitioned(Iter(ranges::begin(ia)),
  68. Sent(ranges::begin(ia)),
  69. is_odd()));
  70. }
  71. }
  72. template<class Iter, class Sent = Iter>
  73. void
  74. test_range()
  75. {
  76. {
  77. const int ia[] = {1, 2, 3, 4, 5, 6};
  78. CHECK(!ranges::is_partitioned(ranges::make_subrange(Iter(ranges::begin(ia)),
  79. Sent(ranges::end(ia))),
  80. is_odd()));
  81. }
  82. {
  83. const int ia[] = {1, 3, 5, 2, 4, 6};
  84. CHECK( ranges::is_partitioned(ranges::make_subrange(Iter(ranges::begin(ia)),
  85. Sent(ranges::end(ia))),
  86. is_odd()));
  87. }
  88. {
  89. const int ia[] = {2, 4, 6, 1, 3, 5};
  90. CHECK(!ranges::is_partitioned(ranges::make_subrange(Iter(ranges::begin(ia)),
  91. Sent(ranges::end(ia))),
  92. is_odd()));
  93. }
  94. {
  95. const int ia[] = {1, 3, 5, 2, 4, 6, 7};
  96. CHECK(!ranges::is_partitioned(ranges::make_subrange(Iter(ranges::begin(ia)),
  97. Sent(ranges::end(ia))),
  98. is_odd()));
  99. }
  100. {
  101. const int ia[] = {1, 3, 5, 2, 4, 6, 7};
  102. CHECK( ranges::is_partitioned(ranges::make_subrange(Iter(ranges::begin(ia)),
  103. Sent(ranges::begin(ia))),
  104. is_odd()));
  105. }
  106. }
  107. struct S
  108. {
  109. int i;
  110. };
  111. int main()
  112. {
  113. test_iter<InputIterator<const int*> >();
  114. test_iter<InputIterator<const int*>, Sentinel<const int*>>();
  115. test_range<InputIterator<const int*> >();
  116. test_range<InputIterator<const int*>, Sentinel<const int*>>();
  117. // Test projections
  118. const S ia[] = {S{1}, S{3}, S{5}, S{2}, S{4}, S{6}};
  119. CHECK( ranges::is_partitioned(ia, is_odd(), &S::i) );
  120. {
  121. using IL = std::initializer_list<int>;
  122. STATIC_CHECK(ranges::is_partitioned(IL{1, 3, 5, 2, 4, 6}, is_odd()));
  123. STATIC_CHECK(!ranges::is_partitioned(IL{1, 3, 1, 2, 5, 6}, is_odd()));
  124. }
  125. return ::test_result();
  126. }