mismatch.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // The LLVM Compiler Infrastructure
  14. //
  15. // This file is dual licensed under the MIT and the University of Illinois Open
  16. // Source Licenses. See LICENSE.TXT for details.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include <memory>
  20. #include <algorithm>
  21. #include <range/v3/core.hpp>
  22. #include <range/v3/algorithm/mismatch.hpp>
  23. #include "../array.hpp"
  24. #include "../simple_test.hpp"
  25. #include "../test_utils.hpp"
  26. #include "../test_iterators.hpp"
  27. RANGES_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS
  28. template<typename Iter, typename Sent = Iter>
  29. void test_iter()
  30. {
  31. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  32. constexpr auto sa = ranges::size(ia);
  33. int ib[] = {0, 1, 2, 3, 0, 1, 2, 3};
  34. using Res = ranges::mismatch_result<Iter, Iter>;
  35. CHECK(ranges::mismatch(Iter(ia), Sent(ia + sa), Iter(ib)) ==
  36. Res{Iter(ia+3),Iter(ib+3)});
  37. CHECK(ranges::mismatch(Iter(ia),Sent(ia + sa),Iter(ib),Sent(ib + sa)) ==
  38. Res{Iter(ia+3),Iter(ib+3)});
  39. CHECK(ranges::mismatch(Iter(ia),Sent(ia + sa),Iter(ib),Sent(ib + 2)) ==
  40. Res{Iter(ia+2),Iter(ib+2)});
  41. CHECK(ranges::mismatch(Iter(ia),Sent(ia + sa),Iter(ib),std::equal_to<int>()) ==
  42. Res{Iter(ia+3),Iter(ib+3)});
  43. CHECK(ranges::mismatch(Iter(ia),Sent(ia + sa),Iter(ib),Sent(ib + sa),std::equal_to<int>()) ==
  44. Res{Iter(ia+3),Iter(ib+3)});
  45. CHECK(ranges::mismatch(Iter(ia), Sent(ia + sa), Iter(ib), Sent(ib + 2), std::equal_to<int>()) ==
  46. Res{Iter(ia+2),Iter(ib+2)});
  47. }
  48. template<typename Iter, typename Sent = Iter>
  49. void test_range()
  50. {
  51. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  52. constexpr auto sa = ranges::size(ia);
  53. int ib[] = {0, 1, 2, 3, 0, 1, 2, 3};
  54. using Res = ranges::mismatch_result<Iter, Iter>;
  55. auto rng1 = ::MakeTestRange(Iter(ia), Sent(ia + sa));
  56. CHECK(ranges::mismatch(rng1, Iter(ib)) ==
  57. Res{Iter(ia+3),Iter(ib+3)});
  58. auto r1 = ranges::mismatch(std::move(rng1), Iter(ib));
  59. CHECK(::is_dangling(r1.in1));
  60. CHECK(r1.in2 == Iter(ib+3));
  61. auto rng2 = ::MakeTestRange(Iter(ia),Sent(ia + sa));
  62. auto rng3 = ::MakeTestRange(Iter(ib),Sent(ib + sa));
  63. CHECK(ranges::mismatch(rng2,rng3) ==
  64. Res{Iter(ia+3),Iter(ib+3)});
  65. auto r2 = ranges::mismatch(std::move(rng2), std::move(rng3));
  66. CHECK(::is_dangling(r2.in1));
  67. CHECK(::is_dangling(r2.in2));
  68. auto r3 = ranges::mismatch(rng2, std::move(rng3));
  69. CHECK(r3.in1 == Iter(ia+3));
  70. CHECK(::is_dangling(r3.in2));
  71. auto r4 = ranges::mismatch(std::move(rng2), rng3);
  72. CHECK(::is_dangling(r4.in1));
  73. CHECK(r4.in2 == Iter(ib+3));
  74. auto rng4 = ::MakeTestRange(Iter(ia),Sent(ia + sa));
  75. auto rng5 = ::MakeTestRange(Iter(ib),Sent(ib + 2));
  76. CHECK(ranges::mismatch(rng4,rng5) ==
  77. Res{Iter(ia+2),Iter(ib+2)});
  78. auto rng6 = ::MakeTestRange(Iter(ia),Sent(ia + sa));
  79. CHECK(ranges::mismatch(rng6,Iter(ib),std::equal_to<int>()) ==
  80. Res{Iter(ia+3),Iter(ib+3)});
  81. auto rng7 = ::MakeTestRange(Iter(ia),Sent(ia + sa));
  82. auto rng8 = ::MakeTestRange(Iter(ib),Sent(ib + sa));
  83. CHECK(ranges::mismatch(rng7,rng8,std::equal_to<int>()) ==
  84. Res{Iter(ia+3),Iter(ib+3)});
  85. auto rng9 = ::MakeTestRange(Iter(ia), Sent(ia + sa));
  86. auto rng10 = ::MakeTestRange(Iter(ib), Sent(ib + 2));
  87. CHECK(ranges::mismatch(rng9,rng10,std::equal_to<int>()) ==
  88. Res{Iter(ia+2),Iter(ib+2)});
  89. }
  90. struct S
  91. {
  92. int i;
  93. };
  94. int main()
  95. {
  96. test_iter<InputIterator<const int*>>();
  97. test_iter<ForwardIterator<const int*>>();
  98. test_iter<BidirectionalIterator<const int*>>();
  99. test_iter<RandomAccessIterator<const int*>>();
  100. test_iter<const int*>();
  101. test_iter<InputIterator<const int*>, Sentinel<const int*>>();
  102. test_iter<ForwardIterator<const int*>, Sentinel<const int*>>();
  103. test_iter<BidirectionalIterator<const int*>, Sentinel<const int*>>();
  104. test_iter<RandomAccessIterator<const int*>, Sentinel<const int*>>();
  105. test_range<InputIterator<const int*>>();
  106. test_range<ForwardIterator<const int*>>();
  107. test_range<BidirectionalIterator<const int*>>();
  108. test_range<RandomAccessIterator<const int*>>();
  109. test_range<const int*>();
  110. test_range<InputIterator<const int*>, Sentinel<const int*>>();
  111. test_range<ForwardIterator<const int*>, Sentinel<const int*>>();
  112. test_range<BidirectionalIterator<const int*>, Sentinel<const int*>>();
  113. test_range<RandomAccessIterator<const int*>, Sentinel<const int*>>();
  114. // Works with projections?
  115. S s1[] = {S{1},S{2},S{3},S{4},S{-4},S{5},S{6},S{40},S{7},S{8},S{9}};
  116. int const i1[] = {1,2,3,4,5,6,7,8,9};
  117. ranges::mismatch_result<S const *, int const *> ps1
  118. = ranges::mismatch(s1, i1, std::equal_to<int>(), &S::i);
  119. CHECK(ps1.in1->i == -4);
  120. CHECK(*ps1.in2 == 5);
  121. S s2[] = {S{1},S{2},S{3},S{4},S{5},S{6},S{40},S{7},S{8},S{9}};
  122. ranges::mismatch_result<S const *, S const *> ps2
  123. = ranges::mismatch(s1, s2, std::equal_to<int>(), &S::i, &S::i);
  124. CHECK(ps2.in1->i == -4);
  125. CHECK(ps2.in2->i == 5);
  126. constexpr auto r1 = test::array<int, 11>{{1, 2, 3, 4, -4, 5, 6, 40, 7, 8, 9}};
  127. constexpr auto r11 = test::array<int, 9>{{1, 2, 3, 4, 5, 6, 7, 8, 9}};
  128. constexpr auto r2 = test::array<int, 10>{{1, 2, 3, 4, 5, 6, 40, 7, 8, 9}};
  129. STATIC_CHECK(*ranges::mismatch(r1, r11, std::equal_to<int>{}).in1 == -4);
  130. STATIC_CHECK(*ranges::mismatch(r1, r11, std::equal_to<int>{}).in2 == 5);
  131. STATIC_CHECK(*ranges::mismatch(r1, r2, std::equal_to<int>{}).in1 == -4);
  132. STATIC_CHECK(*ranges::mismatch(r1, r2, std::equal_to<int>{}).in2 == 5);
  133. return test_result();
  134. }