accumulate.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. //
  14. // The LLVM Compiler Infrastructure
  15. //
  16. // This file is dual licensed under the MIT and the University of Illinois Open
  17. // Source Licenses. See LICENSE.TXT for details.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include <range/v3/core.hpp>
  21. #include <range/v3/numeric/accumulate.hpp>
  22. #include "../simple_test.hpp"
  23. #include "../test_iterators.hpp"
  24. struct S
  25. {
  26. int i;
  27. S add(int j) const
  28. {
  29. return S{i + j};
  30. }
  31. };
  32. template<class Iter, class Sent = Iter>
  33. void test()
  34. {
  35. int ia[] = {1, 2, 3, 4, 5, 6};
  36. constexpr auto sc = ranges::size(ia);
  37. CHECK(ranges::accumulate(Iter(ia), Sent(ia), 0) == 0);
  38. CHECK(ranges::accumulate(Iter(ia), Sent(ia), 10) == 10);
  39. CHECK(ranges::accumulate(Iter(ia), Sent(ia+1), 0) == 1);
  40. CHECK(ranges::accumulate(Iter(ia), Sent(ia+1), 10) == 11);
  41. CHECK(ranges::accumulate(Iter(ia), Sent(ia+2), 0) == 3);
  42. CHECK(ranges::accumulate(Iter(ia), Sent(ia+2), 10) == 13);
  43. CHECK(ranges::accumulate(Iter(ia), Sent(ia+sc), 0) == 21);
  44. CHECK(ranges::accumulate(Iter(ia), Sent(ia+sc), 10) == 31);
  45. using ranges::make_subrange;
  46. CHECK(ranges::accumulate(make_subrange(Iter(ia), Sent(ia)), 0) == 0);
  47. CHECK(ranges::accumulate(make_subrange(Iter(ia), Sent(ia)), 10) == 10);
  48. CHECK(ranges::accumulate(make_subrange(Iter(ia), Sent(ia+1)), 0) == 1);
  49. CHECK(ranges::accumulate(make_subrange(Iter(ia), Sent(ia+1)), 10) == 11);
  50. CHECK(ranges::accumulate(make_subrange(Iter(ia), Sent(ia+2)), 0) == 3);
  51. CHECK(ranges::accumulate(make_subrange(Iter(ia), Sent(ia+2)), 10) == 13);
  52. CHECK(ranges::accumulate(make_subrange(Iter(ia), Sent(ia+sc)), 0) == 21);
  53. CHECK(ranges::accumulate(make_subrange(Iter(ia), Sent(ia+sc)), 10) == 31);
  54. }
  55. int main()
  56. {
  57. test<InputIterator<const int*> >();
  58. test<ForwardIterator<const int*> >();
  59. test<BidirectionalIterator<const int*> >();
  60. test<RandomAccessIterator<const int*> >();
  61. test<const int*>();
  62. test<InputIterator<const int*>, Sentinel<const int*> >();
  63. test<ForwardIterator<const int*>, Sentinel<const int*> >();
  64. test<BidirectionalIterator<const int*>, Sentinel<const int*> >();
  65. test<RandomAccessIterator<const int*>, Sentinel<const int*> >();
  66. return ::test_result();
  67. }