generate.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <vector>
  20. #include <range/v3/core.hpp>
  21. #include <range/v3/algorithm/generate.hpp>
  22. #include <range/v3/iterator/insert_iterators.hpp>
  23. #include <range/v3/view/counted.hpp>
  24. #include "../simple_test.hpp"
  25. #include "../test_utils.hpp"
  26. #include "../test_iterators.hpp"
  27. struct gen_test
  28. {
  29. int i_;
  30. constexpr gen_test()
  31. : i_{}
  32. {}
  33. constexpr gen_test(int i)
  34. : i_(i)
  35. {}
  36. constexpr int operator()()
  37. {
  38. return i_++;
  39. }
  40. };
  41. template<class Iter, class Sent = Iter>
  42. void
  43. test()
  44. {
  45. const unsigned n = 4;
  46. int ia[n] = {0};
  47. ranges::generate_result<Iter, gen_test> res = ranges::generate(Iter(ia), Sent(ia + n), gen_test(1));
  48. CHECK(ia[0] == 1);
  49. CHECK(ia[1] == 2);
  50. CHECK(ia[2] == 3);
  51. CHECK(ia[3] == 4);
  52. CHECK(res.out == Iter(ia + n));
  53. CHECK(res.fun.i_ == 5);
  54. auto rng = ::MakeTestRange(Iter(ia), Sent(ia + n));
  55. res = ranges::generate(rng, res.fun);
  56. CHECK(ia[0] == 5);
  57. CHECK(ia[1] == 6);
  58. CHECK(ia[2] == 7);
  59. CHECK(ia[3] == 8);
  60. CHECK(res.out == Iter(ia + n));
  61. CHECK(res.fun.i_ == 9);
  62. auto res2 = ranges::generate(std::move(rng), res.fun);
  63. CHECK(ia[0] == 9);
  64. CHECK(ia[1] == 10);
  65. CHECK(ia[2] == 11);
  66. CHECK(ia[3] == 12);
  67. CHECK(::is_dangling(res2.out));
  68. CHECK(res2.fun.i_ == 13);
  69. }
  70. void test2()
  71. {
  72. // Test ranges::generate with a genuine output range
  73. std::vector<int> v;
  74. auto rng = ranges::views::counted(ranges::back_inserter(v), 5);
  75. ranges::generate(rng, gen_test(1));
  76. CHECK(v.size() == 5u);
  77. CHECK(v[0] == 1);
  78. CHECK(v[1] == 2);
  79. CHECK(v[2] == 3);
  80. CHECK(v[3] == 4);
  81. CHECK(v[4] == 5);
  82. }
  83. template<class Iter, class Sent = Iter>
  84. constexpr bool test_constexpr()
  85. {
  86. const unsigned n = 4;
  87. int ia[n] = {0};
  88. const auto res = ranges::generate(Iter(ia), Sent(ia + n), gen_test(1));
  89. STATIC_CHECK_RETURN(ia[0] == 1);
  90. STATIC_CHECK_RETURN(ia[1] == 2);
  91. STATIC_CHECK_RETURN(ia[2] == 3);
  92. STATIC_CHECK_RETURN(ia[3] == 4);
  93. STATIC_CHECK_RETURN(res.out == Iter(ia + n));
  94. STATIC_CHECK_RETURN(res.fun.i_ == 5);
  95. auto rng = ranges::make_subrange(Iter(ia), Sent(ia + n));
  96. const auto res2 = ranges::generate(rng, res.fun);
  97. STATIC_CHECK_RETURN(ia[0] == 5);
  98. STATIC_CHECK_RETURN(ia[1] == 6);
  99. STATIC_CHECK_RETURN(ia[2] == 7);
  100. STATIC_CHECK_RETURN(ia[3] == 8);
  101. STATIC_CHECK_RETURN(res2.out == Iter(ia + n));
  102. STATIC_CHECK_RETURN(res2.fun.i_ == 9);
  103. const auto res3 = ranges::generate(std::move(rng), res2.fun);
  104. STATIC_CHECK_RETURN(ia[0] == 9);
  105. STATIC_CHECK_RETURN(ia[1] == 10);
  106. STATIC_CHECK_RETURN(ia[2] == 11);
  107. STATIC_CHECK_RETURN(ia[3] == 12);
  108. STATIC_CHECK_RETURN(res3.out == Iter(ia + n));
  109. STATIC_CHECK_RETURN(res3.fun.i_ == 13);
  110. return true;
  111. }
  112. int main()
  113. {
  114. test<ForwardIterator<int*> >();
  115. test<BidirectionalIterator<int*> >();
  116. test<RandomAccessIterator<int*> >();
  117. test<int*>();
  118. test<ForwardIterator<int*>, Sentinel<int*> >();
  119. test<BidirectionalIterator<int*>, Sentinel<int*> >();
  120. test<RandomAccessIterator<int*>, Sentinel<int*> >();
  121. test2();
  122. STATIC_CHECK(test_constexpr<ForwardIterator<int *>>());
  123. STATIC_CHECK(test_constexpr<BidirectionalIterator<int *>>());
  124. STATIC_CHECK(test_constexpr<RandomAccessIterator<int *>>());
  125. STATIC_CHECK(test_constexpr<int *>());
  126. STATIC_CHECK(test_constexpr<ForwardIterator<int *>, Sentinel<int *>>());
  127. STATIC_CHECK(test_constexpr<BidirectionalIterator<int *>, Sentinel<int *>>());
  128. STATIC_CHECK(test_constexpr<RandomAccessIterator<int *>, Sentinel<int *>>());
  129. return ::test_result();
  130. }