replace.cpp 4.4 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. // 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.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(Iter(ia), Sent(ia+sa), 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(rng, 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 test_constexpr()
  58. {
  59. using namespace ranges;
  60. int ia[] = {0, 1, 2, 3, 4};
  61. constexpr auto sa = ranges::size(ia);
  62. auto r = ranges::replace(ia, 2, 42);
  63. STATIC_CHECK_RETURN(r == ia + sa);
  64. STATIC_CHECK_RETURN(ia[0] == 0);
  65. STATIC_CHECK_RETURN(ia[1] == 1);
  66. STATIC_CHECK_RETURN(ia[2] == 42);
  67. STATIC_CHECK_RETURN(ia[3] == 3);
  68. STATIC_CHECK_RETURN(ia[4] == 4);
  69. return true;
  70. }
  71. int main()
  72. {
  73. test_iter<ForwardIterator<int*> >();
  74. test_iter<BidirectionalIterator<int*> >();
  75. test_iter<RandomAccessIterator<int*> >();
  76. test_iter<int*>();
  77. test_iter<ForwardIterator<int*>, Sentinel<int*> >();
  78. test_iter<BidirectionalIterator<int*>, Sentinel<int*> >();
  79. test_iter<RandomAccessIterator<int*>, Sentinel<int*> >();
  80. test_rng<ForwardIterator<int*> >();
  81. test_rng<BidirectionalIterator<int*> >();
  82. test_rng<RandomAccessIterator<int*> >();
  83. test_rng<int*>();
  84. test_rng<ForwardIterator<int*>, Sentinel<int*> >();
  85. test_rng<BidirectionalIterator<int*>, Sentinel<int*> >();
  86. test_rng<RandomAccessIterator<int*>, Sentinel<int*> >();
  87. // test projection
  88. {
  89. using P = std::pair<int,std::string>;
  90. P ia[] = {{0,"0"}, {1,"1"}, {2,"2"}, {3,"3"}, {4,"4"}};
  91. P *i = ranges::replace(ia, 2, std::make_pair(42,"42"), &std::pair<int,std::string>::first);
  92. CHECK(ia[0] == P{0,"0"});
  93. CHECK(ia[1] == P{1,"1"});
  94. CHECK(ia[2] == P{42,"42"});
  95. CHECK(ia[3] == P{3,"3"});
  96. CHECK(ia[4] == P{4,"4"});
  97. CHECK(i == ranges::end(ia));
  98. }
  99. // test rvalue ranges
  100. {
  101. using P = std::pair<int,std::string>;
  102. P ia[] = {{0,"0"}, {1,"1"}, {2,"2"}, {3,"3"}, {4,"4"}};
  103. auto i = ranges::replace(std::move(ia), 2, std::make_pair(42,"42"), &std::pair<int,std::string>::first);
  104. #ifndef RANGES_WORKAROUND_MSVC_573728
  105. CHECK(::is_dangling(i));
  106. #endif // RANGES_WORKAROUND_MSVC_573728
  107. CHECK(ia[0] == P{0,"0"});
  108. CHECK(ia[1] == P{1,"1"});
  109. CHECK(ia[2] == P{42,"42"});
  110. CHECK(ia[3] == P{3,"3"});
  111. CHECK(ia[4] == P{4,"4"});
  112. }
  113. {
  114. using P = std::pair<int,std::string>;
  115. std::vector<P> ia{{0,"0"}, {1,"1"}, {2,"2"}, {3,"3"}, {4,"4"}};
  116. auto i = ranges::replace(std::move(ia), 2, std::make_pair(42,"42"), &std::pair<int,std::string>::first);
  117. CHECK(::is_dangling(i));
  118. CHECK(ia[0] == P{0,"0"});
  119. CHECK(ia[1] == P{1,"1"});
  120. CHECK(ia[2] == P{42,"42"});
  121. CHECK(ia[3] == P{3,"3"});
  122. CHECK(ia[4] == P{4,"4"});
  123. }
  124. {
  125. STATIC_CHECK(test_constexpr());
  126. }
  127. return ::test_result();
  128. }