includes.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <functional>
  21. #include <range/v3/core.hpp>
  22. #include <range/v3/algorithm/set_algorithm.hpp>
  23. #include "../simple_test.hpp"
  24. #include "../test_utils.hpp"
  25. #include "../test_iterators.hpp"
  26. RANGES_DIAGNOSTIC_IGNORE_GLOBAL_CONSTRUCTORS
  27. namespace
  28. {
  29. auto const true_ = [](bool b){CHECK(b);};
  30. auto const false_ = [](bool b){CHECK(!b);};
  31. template<class Iter1, class Iter2>
  32. void
  33. test_iter()
  34. {
  35. int ia[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
  36. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  37. int ib[] = {2, 4};
  38. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  39. int ic[] = {1, 2};
  40. int id[] = {3, 3, 3, 3};
  41. auto includes = make_testable_2<true, true>(ranges::includes);
  42. includes(Iter1(ia), Iter1(ia), Iter2(ib), Iter2(ib)).check(true_);
  43. includes(Iter1(ia), Iter1(ia), Iter2(ib), Iter2(ib+1)).check(false_);
  44. includes(Iter1(ia), Iter1(ia+1), Iter2(ib), Iter2(ib)).check(true_);
  45. includes(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+sa)).check(true_);
  46. includes(Iter1(ia), Iter1(ia+sa), Iter2(ib), Iter2(ib+sb)).check(true_);
  47. includes(Iter1(ib), Iter1(ib+sb), Iter2(ia), Iter2(ia+sa)).check(false_);
  48. includes(Iter1(ia), Iter1(ia+2), Iter2(ic), Iter2(ic+2)).check(true_);
  49. includes(Iter1(ia), Iter1(ia+2), Iter2(ib), Iter2(ib+2)).check(false_);
  50. includes(Iter1(ia), Iter1(ia+sa), Iter2(id), Iter2(id+1)).check(true_);
  51. includes(Iter1(ia), Iter1(ia+sa), Iter2(id), Iter2(id+2)).check(true_);
  52. includes(Iter1(ia), Iter1(ia+sa), Iter2(id), Iter2(id+3)).check(true_);
  53. includes(Iter1(ia), Iter1(ia+sa), Iter2(id), Iter2(id+4)).check(false_);
  54. }
  55. template<class Iter1, class Iter2>
  56. void
  57. test_comp()
  58. {
  59. int ia[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
  60. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  61. int ib[] = {2, 4};
  62. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  63. int ic[] = {1, 2};
  64. int id[] = {3, 3, 3, 3};
  65. auto includes = make_testable_2<true, true>(ranges::includes);
  66. includes(Iter1(ia), Iter1(ia), Iter2(ib), Iter2(ib), std::less<int>()).check(true_);
  67. includes(Iter1(ia), Iter1(ia), Iter2(ib), Iter2(ib+1), std::less<int>()).check(false_);
  68. includes(Iter1(ia), Iter1(ia+1), Iter2(ib), Iter2(ib), std::less<int>()).check(true_);
  69. includes(Iter1(ia), Iter1(ia+sa), Iter2(ia), Iter2(ia+sa), std::less<int>()).check(true_);
  70. includes(Iter1(ia), Iter1(ia+sa), Iter2(ib), Iter2(ib+sb), std::less<int>()).check(true_);
  71. includes(Iter1(ib), Iter1(ib+sb), Iter2(ia), Iter2(ia+sa), std::less<int>()).check(false_);
  72. includes(Iter1(ia), Iter1(ia+2), Iter2(ic), Iter2(ic+2), std::less<int>()).check(true_);
  73. includes(Iter1(ia), Iter1(ia+2), Iter2(ib), Iter2(ib+2), std::less<int>()).check(false_);
  74. includes(Iter1(ia), Iter1(ia+sa), Iter2(id), Iter2(id+1), std::less<int>()).check(true_);
  75. includes(Iter1(ia), Iter1(ia+sa), Iter2(id), Iter2(id+2), std::less<int>()).check(true_);
  76. includes(Iter1(ia), Iter1(ia+sa), Iter2(id), Iter2(id+3), std::less<int>()).check(true_);
  77. includes(Iter1(ia), Iter1(ia+sa), Iter2(id), Iter2(id+4), std::less<int>()).check(false_);
  78. }
  79. template<class Iter1, class Iter2>
  80. void test()
  81. {
  82. test_iter<Iter1, Iter2>();
  83. test_comp<Iter1, Iter2>();
  84. }
  85. struct S
  86. {
  87. int i;
  88. };
  89. struct T
  90. {
  91. int j;
  92. };
  93. }
  94. int main()
  95. {
  96. test<InputIterator<const int*>, InputIterator<const int*> >();
  97. test<InputIterator<const int*>, ForwardIterator<const int*> >();
  98. test<InputIterator<const int*>, BidirectionalIterator<const int*> >();
  99. test<InputIterator<const int*>, RandomAccessIterator<const int*> >();
  100. test<InputIterator<const int*>, const int*>();
  101. test<ForwardIterator<const int*>, InputIterator<const int*> >();
  102. test<ForwardIterator<const int*>, ForwardIterator<const int*> >();
  103. test<ForwardIterator<const int*>, BidirectionalIterator<const int*> >();
  104. test<ForwardIterator<const int*>, RandomAccessIterator<const int*> >();
  105. test<ForwardIterator<const int*>, const int*>();
  106. test<BidirectionalIterator<const int*>, InputIterator<const int*> >();
  107. test<BidirectionalIterator<const int*>, ForwardIterator<const int*> >();
  108. test<BidirectionalIterator<const int*>, BidirectionalIterator<const int*> >();
  109. test<BidirectionalIterator<const int*>, RandomAccessIterator<const int*> >();
  110. test<BidirectionalIterator<const int*>, const int*>();
  111. test<RandomAccessIterator<const int*>, InputIterator<const int*> >();
  112. test<RandomAccessIterator<const int*>, ForwardIterator<const int*> >();
  113. test<RandomAccessIterator<const int*>, BidirectionalIterator<const int*> >();
  114. test<RandomAccessIterator<const int*>, RandomAccessIterator<const int*> >();
  115. test<RandomAccessIterator<const int*>, const int*>();
  116. test<const int*, InputIterator<const int*> >();
  117. test<const int*, ForwardIterator<const int*> >();
  118. test<const int*, BidirectionalIterator<const int*> >();
  119. test<const int*, RandomAccessIterator<const int*> >();
  120. test<const int*, const int*>();
  121. // Test projections
  122. {
  123. S ia[] = {{1}, {2}, {2}, {3}, {3}, {3}, {4}, {4}, {4}, {4}};
  124. T id[] = {{3}, {3}, {3}};
  125. CHECK(ranges::includes(ia, id, std::less<int>(), &S::i, &T::j));
  126. }
  127. {
  128. using IL = std::initializer_list<int>;
  129. STATIC_CHECK(ranges::includes(
  130. IL{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}, IL{3, 3, 3}, std::less<int>()));
  131. }
  132. return ::test_result();
  133. }