accumulate_op.pass.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <numeric>
  10. // template <InputIterator Iter, MoveConstructible T,
  11. // Callable<auto, const T&, Iter::reference> BinaryOperation>
  12. // requires HasAssign<T, BinaryOperation::result_type>
  13. // && CopyConstructible<BinaryOperation>
  14. // T
  15. // accumulate(Iter first, Iter last, T init, BinaryOperation binary_op);
  16. #include <numeric>
  17. #include <functional>
  18. #include <cassert>
  19. #include "../iterators.h"
  20. template <class Iter, class T>
  21. void
  22. test(Iter first, Iter last, T init, T x)
  23. {
  24. assert(std::accumulate(first, last, init, std::multiplies<T>()) == x);
  25. }
  26. template <class Iter>
  27. void
  28. test()
  29. {
  30. int ia[] = {1, 2, 3, 4, 5, 6};
  31. unsigned sa = sizeof(ia) / sizeof(ia[0]);
  32. test(Iter(ia), Iter(ia), 1, 1);
  33. test(Iter(ia), Iter(ia), 10, 10);
  34. test(Iter(ia), Iter(ia+1), 1, 1);
  35. test(Iter(ia), Iter(ia+1), 10, 10);
  36. test(Iter(ia), Iter(ia+2), 1, 2);
  37. test(Iter(ia), Iter(ia+2), 10, 20);
  38. test(Iter(ia), Iter(ia+sa), 1, 720);
  39. test(Iter(ia), Iter(ia+sa), 10, 7200);
  40. }
  41. int main()
  42. {
  43. test<input_iterator<const int*> >();
  44. test<forward_iterator<const int*> >();
  45. test<bidirectional_iterator<const int*> >();
  46. test<random_access_iterator<const int*> >();
  47. test<const int*>();
  48. }