1 //===----------------------------------------------------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <numeric>
10 // UNSUPPORTED: clang-8
11 // UNSUPPORTED: gcc-9
12 
13 // Became constexpr in C++20
14 // template <InputIterator Iter, MoveConstructible T>
15 //   requires HasPlus<T, Iter::reference>
16 //         && HasAssign<T, HasPlus<T, Iter::reference>::result_type>
17 //   T
18 //   accumulate(Iter first, Iter last, T init);
19 
20 #include <numeric>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 #include "test_iterators.h"
25 
26 template <class Iter, class T>
27 TEST_CONSTEXPR_CXX20 void
28 test(Iter first, Iter last, T init, T x)
29 {
30     assert(std::accumulate(first, last, init) == x);
31 }
32 
33 template <class Iter>
34 TEST_CONSTEXPR_CXX20 void
35 test()
36 {
37     int ia[] = {1, 2, 3, 4, 5, 6};
38     unsigned sa = sizeof(ia) / sizeof(ia[0]);
39     test(Iter(ia), Iter(ia), 0, 0);
40     test(Iter(ia), Iter(ia), 10, 10);
41     test(Iter(ia), Iter(ia+1), 0, 1);
42     test(Iter(ia), Iter(ia+1), 10, 11);
43     test(Iter(ia), Iter(ia+2), 0, 3);
44     test(Iter(ia), Iter(ia+2), 10, 13);
45     test(Iter(ia), Iter(ia+sa), 0, 21);
46     test(Iter(ia), Iter(ia+sa), 10, 31);
47 }
48 
49 TEST_CONSTEXPR_CXX20 bool
50 test()
51 {
52     test<input_iterator<const int*> >();
53     test<forward_iterator<const int*> >();
54     test<bidirectional_iterator<const int*> >();
55     test<random_access_iterator<const int*> >();
56     test<const int*>();
57 
58     return true;
59 }
60 
61 int main(int, char**)
62 {
63     test();
64 #if TEST_STD_VER > 17
65     static_assert(test());
66 #endif
67     return 0;
68 }
69