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>
16 // typename iterator_traits<InputIterator>::value_type
17 // reduce(InputIterator first, InputIterator last);
18
19 #include <numeric>
20 #include <cassert>
21
22 #include "test_macros.h"
23 #include "test_iterators.h"
24
25 template <class Iter, class T>
26 TEST_CONSTEXPR_CXX20 void
test(Iter first,Iter last,T x)27 test(Iter first, Iter last, T x)
28 {
29 static_assert( std::is_same_v<typename std::iterator_traits<decltype(first)>::value_type,
30 decltype(std::reduce(first, last))> );
31 assert(std::reduce(first, last) == x);
32 }
33
34 template <class Iter>
35 TEST_CONSTEXPR_CXX20 void
test()36 test()
37 {
38 int ia[] = {1, 2, 3, 4, 5, 6};
39 unsigned sa = sizeof(ia) / sizeof(ia[0]);
40 test(Iter(ia), Iter(ia), 0);
41 test(Iter(ia), Iter(ia+1), 1);
42 test(Iter(ia), Iter(ia+2), 3);
43 test(Iter(ia), Iter(ia+sa), 21);
44 }
45
46 template <typename T>
47 TEST_CONSTEXPR_CXX20 void
test_return_type()48 test_return_type()
49 {
50 T *p = nullptr;
51 static_assert( std::is_same_v<T, decltype(std::reduce(p, p))> );
52 }
53
54 TEST_CONSTEXPR_CXX20 bool
test()55 test()
56 {
57 test_return_type<char>();
58 test_return_type<int>();
59 test_return_type<unsigned long>();
60 test_return_type<float>();
61 test_return_type<double>();
62
63 test<input_iterator<const int*> >();
64 test<forward_iterator<const int*> >();
65 test<bidirectional_iterator<const int*> >();
66 test<random_access_iterator<const int*> >();
67 test<const int*>();
68
69 return true;
70 }
71
main(int,char **)72 int main(int, char**)
73 {
74 test();
75 #if TEST_STD_VER > 17
76 static_assert(test());
77 #endif
78 return 0;
79 }
80