replace_if.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <utility>
  24. #include <vector>
  25. #include <range/v3/core.hpp>
  26. #include <range/v3/algorithm/replace_if.hpp>
  27. #include "../simple_test.hpp"
  28. #include "../test_utils.hpp"
  29. #include "../test_iterators.hpp"
  30. template<typename Iter, typename Sent = Iter>
  31. void test_iter()
  32. {
  33. int ia[] = {0, 1, 2, 3, 4};
  34. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  35. Iter i = ranges::replace_if(Iter(ia), Sent(ia+sa), [](int j){return j==2;}, 5);
  36. CHECK(ia[0] == 0);
  37. CHECK(ia[1] == 1);
  38. CHECK(ia[2] == 5);
  39. CHECK(ia[3] == 3);
  40. CHECK(ia[4] == 4);
  41. CHECK(base(i) == ia + sa);
  42. }
  43. template<typename Iter, typename Sent = Iter>
  44. void test_rng()
  45. {
  46. int ia[] = {0, 1, 2, 3, 4};
  47. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  48. auto rng = ranges::make_subrange(Iter(ia), Sent(ia+sa));
  49. Iter i = ranges::replace_if(rng, [](int j){return j==2;}, 5);
  50. CHECK(ia[0] == 0);
  51. CHECK(ia[1] == 1);
  52. CHECK(ia[2] == 5);
  53. CHECK(ia[3] == 3);
  54. CHECK(ia[4] == 4);
  55. CHECK(base(i) == ia + sa);
  56. }
  57. constexpr bool equals_two(int i)
  58. {
  59. return i == 2;
  60. }
  61. constexpr bool test_constexpr()
  62. {
  63. using namespace ranges;
  64. int ia[] = {0, 1, 2, 3, 4};
  65. constexpr auto sa = ranges::size(ia);
  66. auto r = ranges::replace_if(ia, equals_two, 42);
  67. STATIC_CHECK_RETURN(r == ia + sa);
  68. STATIC_CHECK_RETURN(ia[0] == 0);
  69. STATIC_CHECK_RETURN(ia[1] == 1);
  70. STATIC_CHECK_RETURN(ia[2] == 42);
  71. STATIC_CHECK_RETURN(ia[3] == 3);
  72. STATIC_CHECK_RETURN(ia[4] == 4);
  73. return true;
  74. }
  75. int main()
  76. {
  77. test_iter<ForwardIterator<int*> >();
  78. test_iter<BidirectionalIterator<int*> >();
  79. test_iter<RandomAccessIterator<int*> >();
  80. test_iter<int*>();
  81. test_iter<ForwardIterator<int*>, Sentinel<int*> >();
  82. test_iter<BidirectionalIterator<int*>, Sentinel<int*> >();
  83. test_iter<RandomAccessIterator<int*>, Sentinel<int*> >();
  84. test_rng<ForwardIterator<int*> >();
  85. test_rng<BidirectionalIterator<int*> >();
  86. test_rng<RandomAccessIterator<int*> >();
  87. test_rng<int*>();
  88. test_rng<ForwardIterator<int*>, Sentinel<int*> >();
  89. test_rng<BidirectionalIterator<int*>, Sentinel<int*> >();
  90. test_rng<RandomAccessIterator<int*>, Sentinel<int*> >();
  91. // test projection
  92. {
  93. using P = std::pair<int,std::string>;
  94. P ia[] = {{0,"0"}, {1,"1"}, {2,"2"}, {3,"3"}, {4,"4"}};
  95. P *i = ranges::replace_if(ia, [](int j){return j==2;}, std::make_pair(42,"42"),
  96. &std::pair<int,std::string>::first);
  97. CHECK(ia[0] == P{0,"0"});
  98. CHECK(ia[1] == P{1,"1"});
  99. CHECK(ia[2] == P{42,"42"});
  100. CHECK(ia[3] == P{3,"3"});
  101. CHECK(ia[4] == P{4,"4"});
  102. CHECK(i == ranges::end(ia));
  103. }
  104. // test rvalue ranges
  105. {
  106. using P = std::pair<int,std::string>;
  107. P ia[] = {{0,"0"}, {1,"1"}, {2,"2"}, {3,"3"}, {4,"4"}};
  108. auto i = ranges::replace_if(std::move(ia), [](int j){return j==2;}, std::make_pair(42,"42"),
  109. &std::pair<int,std::string>::first);
  110. #ifndef RANGES_WORKAROUND_MSVC_573728
  111. CHECK(::is_dangling(i));
  112. #endif // RANGES_WORKAROUND_MSVC_573728
  113. CHECK(ia[0] == P{0,"0"});
  114. CHECK(ia[1] == P{1,"1"});
  115. CHECK(ia[2] == P{42,"42"});
  116. CHECK(ia[3] == P{3,"3"});
  117. CHECK(ia[4] == P{4,"4"});
  118. }
  119. {
  120. using P = std::pair<int,std::string>;
  121. std::vector<P> ia{{0,"0"}, {1,"1"}, {2,"2"}, {3,"3"}, {4,"4"}};
  122. auto i = ranges::replace_if(std::move(ia), [](int j){return j==2;}, std::make_pair(42,"42"),
  123. &std::pair<int,std::string>::first);
  124. CHECK(::is_dangling(i));
  125. CHECK(ia[0] == P{0,"0"});
  126. CHECK(ia[1] == P{1,"1"});
  127. CHECK(ia[2] == P{42,"42"});
  128. CHECK(ia[3] == P{3,"3"});
  129. CHECK(ia[4] == P{4,"4"});
  130. }
  131. {
  132. STATIC_CHECK(test_constexpr());
  133. }
  134. return ::test_result();
  135. }