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: c++03, c++11, c++14
11 // UNSUPPORTED: clang-8
12 // UNSUPPORTED: gcc-9
13 
14 // Became constexpr in C++20
15 // template<class InputIterator, class T>
16 //   T reduce(InputIterator first, InputIterator 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     static_assert( std::is_same_v<T, decltype(std::reduce(first, last, init))> );
29     assert(std::reduce(first, last, init) == x);
30 }
31 
32 template <class Iter>
33 TEST_CONSTEXPR_CXX20 void
test()34 test()
35 {
36     int ia[] = {1, 2, 3, 4, 5, 6};
37     unsigned sa = sizeof(ia) / sizeof(ia[0]);
38     test(Iter(ia), Iter(ia), 0, 0);
39     test(Iter(ia), Iter(ia), 1, 1);
40     test(Iter(ia), Iter(ia+1), 0, 1);
41     test(Iter(ia), Iter(ia+1), 2, 3);
42     test(Iter(ia), Iter(ia+2), 0, 3);
43     test(Iter(ia), Iter(ia+2), 3, 6);
44     test(Iter(ia), Iter(ia+sa), 0, 21);
45     test(Iter(ia), Iter(ia+sa), 4, 25);
46 }
47 
48 template <typename T, typename Init>
49 TEST_CONSTEXPR_CXX20 void
test_return_type()50 test_return_type()
51 {
52     T *p = nullptr;
53     static_assert( std::is_same_v<Init, decltype(std::reduce(p, p, Init{}))> );
54 }
55 
56 TEST_CONSTEXPR_CXX20 bool
test()57 test()
58 {
59     test_return_type<char, int>();
60     test_return_type<int, int>();
61     test_return_type<int, unsigned long>();
62     test_return_type<float, int>();
63     test_return_type<short, float>();
64     test_return_type<double, char>();
65     test_return_type<char, double>();
66 
67     test<input_iterator<const int*> >();
68     test<forward_iterator<const int*> >();
69     test<bidirectional_iterator<const int*> >();
70     test<random_access_iterator<const int*> >();
71     test<const int*>();
72 
73     return true;
74 }
75 
main(int,char **)76 int main(int, char**)
77 {
78     test();
79 #if TEST_STD_VER > 17
80     static_assert(test());
81 #endif
82     return 0;
83 }
84