accumulate.pass.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // requires HasPlus<T, Iter::reference>
  12. // && HasAssign<T, HasPlus<T, Iter::reference>::result_type>
  13. // T
  14. // accumulate(Iter first, Iter last, T init);
  15. #include <numeric>
  16. #include <cassert>
  17. #include "../iterators.h"
  18. template <class Iter, class T>
  19. void
  20. test(Iter first, Iter last, T init, T x)
  21. {
  22. assert(std::accumulate(first, last, init) == x);
  23. }
  24. template <class Iter>
  25. void
  26. test()
  27. {
  28. int ia[] = {1, 2, 3, 4, 5, 6};
  29. unsigned sa = sizeof(ia) / sizeof(ia[0]);
  30. test(Iter(ia), Iter(ia), 0, 0);
  31. test(Iter(ia), Iter(ia), 10, 10);
  32. test(Iter(ia), Iter(ia+1), 0, 1);
  33. test(Iter(ia), Iter(ia+1), 10, 11);
  34. test(Iter(ia), Iter(ia+2), 0, 3);
  35. test(Iter(ia), Iter(ia+2), 10, 13);
  36. test(Iter(ia), Iter(ia+sa), 0, 21);
  37. test(Iter(ia), Iter(ia+sa), 10, 31);
  38. }
  39. int main()
  40. {
  41. test<input_iterator<const int*> >();
  42. test<forward_iterator<const int*> >();
  43. test<bidirectional_iterator<const int*> >();
  44. test<random_access_iterator<const int*> >();
  45. test<const int*>();
  46. }