generate_n.cpp 3.1 KB

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