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