xref: /llvm-project/libcxx/test/std/numerics/numeric.ops/reduce/reduce_init_op.pass.cpp (revision eb9b063539c34d0d4dd14e8516eeb77bb8b9e4bd)
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 
12 // Became constexpr in C++20
13 // template<class InputIterator, class T, class BinaryOperation>
14 //   T reduce(InputIterator first, InputIterator last, T init, BinaryOperation op);
15 
16 #include <numeric>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "test_iterators.h"
21 
22 template <class Iter, class T, class Op>
23 TEST_CONSTEXPR_CXX20 void
24 test(Iter first, Iter last, T init, Op op, T x)
25 {
26     static_assert( std::is_same_v<T, decltype(std::reduce(first, last, init, op))>, "" );
27     assert(std::reduce(first, last, init, op) == x);
28 }
29 
30 template <class Iter>
31 TEST_CONSTEXPR_CXX20 void
32 test()
33 {
34     int ia[] = {1, 2, 3, 4, 5, 6};
35     unsigned sa = sizeof(ia) / sizeof(ia[0]);
36     test(Iter(ia), Iter(ia), 0, std::plus<>(), 0);
37     test(Iter(ia), Iter(ia), 1, std::multiplies<>(), 1);
38     test(Iter(ia), Iter(ia+1), 0, std::plus<>(), 1);
39     test(Iter(ia), Iter(ia+1), 2, std::multiplies<>(), 2);
40     test(Iter(ia), Iter(ia+2), 0, std::plus<>(), 3);
41     test(Iter(ia), Iter(ia+2), 3, std::multiplies<>(), 6);
42     test(Iter(ia), Iter(ia+sa), 0, std::plus<>(), 21);
43     test(Iter(ia), Iter(ia+sa), 4, std::multiplies<>(), 2880);
44 }
45 
46 template <typename T, typename Init>
47 TEST_CONSTEXPR_CXX20 void
48 test_return_type()
49 {
50     T *p = nullptr;
51     static_assert( std::is_same_v<Init, decltype(std::reduce(p, p, Init{}, std::plus<>()))>, "" );
52 }
53 
54 TEST_CONSTEXPR_CXX20 bool
55 test()
56 {
57     test_return_type<char, int>();
58     test_return_type<int, int>();
59     test_return_type<int, unsigned long>();
60     test_return_type<float, int>();
61     test_return_type<short, float>();
62     test_return_type<double, char>();
63     test_return_type<char, double>();
64 
65     test<input_iterator<const int*> >();
66     test<forward_iterator<const int*> >();
67     test<bidirectional_iterator<const int*> >();
68     test<random_access_iterator<const int*> >();
69     test<const int*>();
70 
71 //  Make sure the math is done using the correct type
72     {
73     auto v = {1, 2, 3, 4, 5, 6, 7, 8};
74     unsigned res = std::reduce(v.begin(), v.end(), 1U, std::multiplies<>());
75     assert(res == 40320);       // 8! will not fit into a char
76     }
77 
78     return true;
79 }
80 
81 int main(int, char**)
82 {
83     test();
84 #if TEST_STD_VER > 17
85     static_assert(test());
86 #endif
87     return 0;
88 }
89