blob: 2a14a7d8e5e8211bd1c149cc85cb5973d0b08c6e [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnant3e519522010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
8
9// <numeric>
10
Howard Hinnanta0eaf602010-08-22 00:42:21 +000011// template <InputIterator Iter, MoveConstructible T>
12// requires HasPlus<T, Iter::reference>
13// && HasAssign<T, HasPlus<T, Iter::reference>::result_type>
Howard Hinnant3e519522010-05-11 19:42:16 +000014// T
15// accumulate(Iter first, Iter last, T init);
16
17#include <numeric>
18#include <cassert>
19
Marshall Clow32227082013-01-05 03:21:01 +000020#include "test_iterators.h"
Howard Hinnant3e519522010-05-11 19:42:16 +000021
22template <class Iter, class T>
23void
24test(Iter first, Iter last, T init, T x)
25{
26 assert(std::accumulate(first, last, init) == x);
27}
28
29template <class Iter>
30void
31test()
32{
33 int ia[] = {1, 2, 3, 4, 5, 6};
34 unsigned sa = sizeof(ia) / sizeof(ia[0]);
35 test(Iter(ia), Iter(ia), 0, 0);
36 test(Iter(ia), Iter(ia), 10, 10);
37 test(Iter(ia), Iter(ia+1), 0, 1);
38 test(Iter(ia), Iter(ia+1), 10, 11);
39 test(Iter(ia), Iter(ia+2), 0, 3);
40 test(Iter(ia), Iter(ia+2), 10, 13);
41 test(Iter(ia), Iter(ia+sa), 0, 21);
42 test(Iter(ia), Iter(ia+sa), 10, 31);
43}
44
45int main()
46{
47 test<input_iterator<const int*> >();
48 test<forward_iterator<const int*> >();
49 test<bidirectional_iterator<const int*> >();
50 test<random_access_iterator<const int*> >();
51 test<const int*>();
52}