exclusive_scan.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Range v3 library
  2. //
  3. // Copyright Mitsutaka Takeda 2018-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. #include <vector>
  13. #include <range/v3/utility/copy.hpp>
  14. #include <range/v3/view/exclusive_scan.hpp>
  15. #include "../test_utils.hpp"
  16. int main()
  17. {
  18. using namespace ranges;
  19. {// For non empty range.
  20. int rgi[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  21. {
  22. auto rng = rgi | views::exclusive_scan(0);
  23. has_type<int &>(*begin(rgi));
  24. has_type<int>(*begin(rng));
  25. CPP_assert(view_<decltype(rng)>);
  26. CPP_assert(sized_range<decltype(rng)>);
  27. CPP_assert(forward_range<decltype(rng)>);
  28. CPP_assert(!bidirectional_range<decltype(rng)>);
  29. ::check_equal(rng, {0, 1, 3, 6, 10, 15, 21, 28, 36, 45});
  30. }
  31. {// Test exclusive_scan with a mutable lambda
  32. int cnt = 0;
  33. auto mutable_rng = views::exclusive_scan(rgi, 0, [cnt](int i, int j) mutable {return i + j + cnt++;});
  34. ::check_equal(mutable_rng, {0, 1, 4, 9, 16, 25, 36, 49, 64, 81});
  35. CPP_assert(view_<decltype(mutable_rng)>);
  36. CPP_assert(!view_<decltype(mutable_rng) const>);
  37. }
  38. }
  39. {// For an empty range.
  40. std::vector<int> rgi;
  41. auto rng = rgi | views::exclusive_scan(0);
  42. has_type<int>(*begin(rng));
  43. CPP_assert(view_<decltype(rng)>);
  44. CPP_assert(sized_range<decltype(rng)>);
  45. CPP_assert(forward_range<decltype(rng)>);
  46. CPP_assert(!bidirectional_range<decltype(rng)>);
  47. CHECK(empty(rng));
  48. }
  49. return test_result();
  50. }