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