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

source code of libcxx/test/std/numerics/numeric.ops/reduce/reduce.pass.cpp