xref: /minix3/external/bsd/libc++/dist/libcxx/test/numerics/numeric.ops/accumulate/accumulate.pass.cpp (revision 4684ddb6aab0b36791c8099bc705d6140b3d05d0)
1*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
2*4684ddb6SLionel Sambuc //
3*4684ddb6SLionel Sambuc //                     The LLVM Compiler Infrastructure
4*4684ddb6SLionel Sambuc //
5*4684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*4684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*4684ddb6SLionel Sambuc //
8*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
9*4684ddb6SLionel Sambuc 
10*4684ddb6SLionel Sambuc // <numeric>
11*4684ddb6SLionel Sambuc 
12*4684ddb6SLionel Sambuc // template <InputIterator Iter, MoveConstructible T>
13*4684ddb6SLionel Sambuc //   requires HasPlus<T, Iter::reference>
14*4684ddb6SLionel Sambuc //         && HasAssign<T, HasPlus<T, Iter::reference>::result_type>
15*4684ddb6SLionel Sambuc //   T
16*4684ddb6SLionel Sambuc //   accumulate(Iter first, Iter last, T init);
17*4684ddb6SLionel Sambuc 
18*4684ddb6SLionel Sambuc #include <numeric>
19*4684ddb6SLionel Sambuc #include <cassert>
20*4684ddb6SLionel Sambuc 
21*4684ddb6SLionel Sambuc #include "test_iterators.h"
22*4684ddb6SLionel Sambuc 
23*4684ddb6SLionel Sambuc template <class Iter, class T>
24*4684ddb6SLionel Sambuc void
test(Iter first,Iter last,T init,T x)25*4684ddb6SLionel Sambuc test(Iter first, Iter last, T init, T x)
26*4684ddb6SLionel Sambuc {
27*4684ddb6SLionel Sambuc     assert(std::accumulate(first, last, init) == x);
28*4684ddb6SLionel Sambuc }
29*4684ddb6SLionel Sambuc 
30*4684ddb6SLionel Sambuc template <class Iter>
31*4684ddb6SLionel Sambuc void
test()32*4684ddb6SLionel Sambuc test()
33*4684ddb6SLionel Sambuc {
34*4684ddb6SLionel Sambuc     int ia[] = {1, 2, 3, 4, 5, 6};
35*4684ddb6SLionel Sambuc     unsigned sa = sizeof(ia) / sizeof(ia[0]);
36*4684ddb6SLionel Sambuc     test(Iter(ia), Iter(ia), 0, 0);
37*4684ddb6SLionel Sambuc     test(Iter(ia), Iter(ia), 10, 10);
38*4684ddb6SLionel Sambuc     test(Iter(ia), Iter(ia+1), 0, 1);
39*4684ddb6SLionel Sambuc     test(Iter(ia), Iter(ia+1), 10, 11);
40*4684ddb6SLionel Sambuc     test(Iter(ia), Iter(ia+2), 0, 3);
41*4684ddb6SLionel Sambuc     test(Iter(ia), Iter(ia+2), 10, 13);
42*4684ddb6SLionel Sambuc     test(Iter(ia), Iter(ia+sa), 0, 21);
43*4684ddb6SLionel Sambuc     test(Iter(ia), Iter(ia+sa), 10, 31);
44*4684ddb6SLionel Sambuc }
45*4684ddb6SLionel Sambuc 
main()46*4684ddb6SLionel Sambuc int main()
47*4684ddb6SLionel Sambuc {
48*4684ddb6SLionel Sambuc     test<input_iterator<const int*> >();
49*4684ddb6SLionel Sambuc     test<forward_iterator<const int*> >();
50*4684ddb6SLionel Sambuc     test<bidirectional_iterator<const int*> >();
51*4684ddb6SLionel Sambuc     test<random_access_iterator<const int*> >();
52*4684ddb6SLionel Sambuc     test<const int*>();
53*4684ddb6SLionel Sambuc }
54